Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

local character = script.Parent local humanoid = character:WaitForChild("Humanoid")…

  1. User

    local character = script.Parent local humanoid = character:WaitForChild("Humanoid") local rootPart = character:WaitForChild("HumanoidRootPart") local head = character:WaitForChild("Head")

    local TweenService = game:GetService("TweenService") local PathfindingService = game:GetService("PathfindingService") local ReplicatedStorage = game:GetService("ReplicatedStorage")

    local currentMode = 1 local modeChangeCooldown = 5 -- seconds local soundPlayChance = 0.2 -- 20% chance to play a sound each iteration local lifespanDuration = 10 -- seconds local spawnTime = tick()

    local paused = false -- Global variable to indicate if the AI is paused local currentTween -- Variable to hold the current tween for cancellation local moveConnection -- Variable to hold the MoveToFinished connection for cancellation local movementInterrupted = false -- Flag to indicate if movement was interrupted

    local function shouldPauseAI() local accessory = character:FindFirstChild("Accessory") if accessory then local cap = accessory:FindFirstChild("Cap") if cap then local canGrab = cap:FindFirstChild("CanGrab") if canGrab and canGrab:IsA("StringValue") then return canGrab.Value ~= "" end end end return false end

    -- Function to update the paused state local function updatePausedState() paused = shouldPauseAI() end

    -- Connect a listener to the CanGrab ValueChanged event to update the paused state immediately local function connectCanGrabListener() local accessory = character:FindFirstChild("Accessory") if accessory then local cap = accessory:FindFirstChild("Cap") if cap then local canGrab = cap:FindFirstChild("CanGrab") if canGrab and canGrab:IsA("StringValue") then canGrab:GetPropertyChangedSignal("Value"):Connect(function() print("changed!!") updatePausedState() if paused then print("paused") -- Interrupt any ongoing actions if currentTween then currentTween:Cancel() end if moveConnection then moveConnection:Disconnect() end humanoid:Move(Vector3.new(0, 0, 0)) movementInterrupted = true end end) end end end end

    -- Call the function to set up the listener connectCanGrabListener() -- Initialize the paused state updatePausedState()

    local function getRandomPosition() local randomAngle = math.random() * math.pi * 2 local randomDistance = math.random(10, 30) local newX = rootPart.Position.X + math.cos(randomAngle) * randomDistance local newZ = rootPart.Position.Z + math.sin(randomAngle) * randomDistance return Vector3.new(newX, rootPart.Position.Y, newZ) end

    local function getNearbyPlayers(maxDistance) local nearbyPlayers = {} for _, player in pairs(game.Players:GetPlayers()) do if player.Character and player.Character ~= character then local hrp = player.Character:FindFirstChild("HumanoidRootPart") if hrp then local distance = (hrp.Position - rootPart.Position).Magnitude if distance <= maxDistance then table.insert(nearbyPlayers, player.Character) end end end end return nearbyPlayers end

    local function getNearestPlayer() local nearestPlayer = nil local nearestDistance = math.huge

    for _, player in pairs(game.Players:GetPlayers()) do
    	if player.Character and player.Character ~= character then
    		local hrp = player.Character:FindFirstChild("HumanoidRootPart")
    		if hrp then
    			local distance = (hrp.Position - rootPart.Position).Magnitude
    			if distance < nearestDistance then
    				nearestDistance = distance
    				nearestPlayer = player.Character
    			end
    		end
    	end
    end
    
    return nearestPlayer
    

    end

    local function moveToPosition(targetPosition) if paused then return end -- Stop immediately if paused local path = PathfindingService:CreatePath() pcall(function() -- Use pcall to handle any errors with pathfinding path:ComputeAsync(rootPart.Position, targetPosition) end)

    if path.Status == Enum.PathStatus.Success then
    	local waypoints = path:GetWaypoints()
    
    	for i, waypoint in ipairs(waypoints) do
    		if paused then
    			humanoid:Move(Vector3.new(0, 0, 0))
    			return
    		end
    		if waypoint.Action == Enum.PathWaypointAction.Jump then
    			humanoid.Jump = true
    		end
    		movementInterrupted = false
    		humanoid:MoveTo(waypoint.Position)
    		moveConnection = humanoid.MoveToFinished:Connect(function(reached)
    			moveConnection:Disconnect()
    		end)
    		repeat
    			if paused or movementInterrupted then
    				humanoid:Move(Vector3.new(0, 0, 0))
    				if moveConnection then
    					moveConnection:Disconnect()
    				end
    				return
    			end
    			wait()
    		until not humanoid.MoveToFinished.Connected
    	end
    end
    

    end

    local function tweenLookAt(targetPosition, duration) if paused then return end -- Stop immediately if paused local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.Out) local endCFrame = CFrame.new(rootPart.Position, Vector3.new(targetPosition.X, rootPart.Position.Y, targetPosition.Z))

    currentTween = TweenService:Create(rootPart, tweenInfo, {CFrame = endCFrame})
    currentTween:Play()
    
    local tweenComplete = false
    currentTween.Completed:Connect(function()
    	tweenComplete = true
    end)
    repeat
    	if paused then
    		currentTween:Cancel()
    		return
    	end
    	wait()
    until tweenComplete
    

    end

    local function lookAtRandomNearbyPlayer() local nearbyPlayers = getNearbyPlayers(50) -- Adjust the max distance as needed if #nearbyPlayers > 0 then local randomPlayer = nearbyPlayers[math.random(1, #nearbyPlayers)] tweenLookAt(randomPlayer.HumanoidRootPart.Position, 0.5) end end

    local function playRandomSound() if paused then return end -- Do not play sounds if paused if math.random() < soundPlayChance then local soundNumber = math.random(1, 4) local soundName = "Talk" .. soundNumber local sound = head:FindFirstChild(soundName) if sound and sound:IsA("Sound") then sound:Play() end end end

    local function stand() if paused then return end lookAtRandomNearbyPlayer() playRandomSound() local totalTime = 1 local elapsedTime = 0 while elapsedTime < totalTime do if paused then return end wait(0.1) elapsedTime = elapsedTime + 0.1 end end

    local function followNearestPlayer() if paused then return end local nearestPlayer = getNearestPlayer() if nearestPlayer then local targetPosition = nearestPlayer.HumanoidRootPart.Position local randomOffset = Vector3.new(math.random(-5, 5), 0, math.random(-5, 5)) moveToPosition(targetPosition + randomOffset) end playRandomSound() local totalTime = 1 local elapsedTime = 0 while elapsedTime < totalTime do if paused then return end wait(0.1) elapsedTime = elapsedTime + 0.1 end end

    local function randomMovement() if paused then return end local targetPosition = getRandomPosition() local midPoint1 = rootPart.Position:Lerp(targetPosition, 0.33) + Vector3.new(math.random(-5, 5), 0, math.random(-5, 5)) local midPoint2 = rootPart.Position:Lerp(targetPosition, 0.66) + Vector3.new(math.random(-5, 5), 0, math.random(-5, 5))

    -- Move to midPoint1
    moveToPosition(midPoint1)
    playRandomSound()
    local totalTime = 0.5
    local elapsedTime = 0
    while elapsedTime < totalTime do
    	if paused then return end
    	wait(0.1)
    	elapsedTime = elapsedTime + 0.1
    end
    
    -- Move to midPoint2
    moveToPosition(midPoint2)
    playRandomSound()
    totalTime = 0.5
    elapsedTime = 0
    while elapsedTime < totalTime do
    	if paused then return end
    	wait(0.1)
    	elapsedTime = elapsedTime + 0.1
    end
    
    -- Move to targetPosition
    moveToPosition(targetPosition)
    playRandomSound()
    totalTime = 0.5
    elapsedTime = 0
    while elapsedTime < totalTime do
    	if paused then return end
    	wait(0.1)
    	elapsedTime = elapsedTime + 0.1
    end
    
    lookAtRandomNearbyPlayer()
    playRandomSound()
    
    totalTime = 1 + math.random()
    elapsedTime = 0
    while elapsedTime < totalTime do
    	if paused then return end
    	wait(0.1)
    	elapsedTime = elapsedTime + 0.1
    end
    

    end

    local function isPlayerNearMushroom(mushroom) local nearbyPlayers = game.Players:GetPlayers() for _, player in ipairs(nearbyPlayers) do if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then local distance = (player.Character.HumanoidRootPart.Position - mushroom.PrimaryPart.Position).Magnitude if distance <= 10 then -- Adjust this distance as needed return true end end end return false end

    local function transformIntoMushroom() if paused then return end local mushroomModel = ReplicatedStorage:FindFirstChild("Mushroom") if mushroomModel then local mushroom = mushroomModel:Clone() mushroom.Parent = workspace mushroom:SetPrimaryPartCFrame(rootPart.Parent["Left Leg"].CFrame)

    	character.Parent = ReplicatedStorage
    
    	while true do
    		if isPlayerNearMushroom(mushroom) then
    			mushroom:Destroy()
    			character.Parent = workspace
    			spawnTime = tick() -- Reset the spawn time
    			print("Character respawned. 10 seconds until next transformation.")
    			return
    		end
    		if paused then
    			-- Wait while paused
    			repeat wait(1) until not paused
    		end
    		wait(1)
    	end
    else
    	warn("Mushroom model not found in ReplicatedStorage")
    end
    

    end

    -- Play death sound when the character dies humanoid.Died:Connect(function() local deathSound = head:FindFirstChild("Death") if deathSound and deathSound:IsA("Sound") then deathSound:Play() end end)

    local lastPrintTime = 0 local printInterval = 30 -- Print time left every 30 seconds

    while true do if paused then humanoid:Move(Vector3.new(0, 0, 0)) -- Wait while paused repeat wait(0.1) until not paused end

    local currentTime = tick()
    local timeElapsed = currentTime - spawnTime
    local timeLeft = lifespanDuration - timeElapsed
    
    if timeLeft <= 0 then
    	print("Transforming into mushroom...")
    	transformIntoMushroom()
    else
    	if currentTime - lastPrintTime >= printInterval then
    		print(string.format("Time left before mushrooming: %.0f seconds", timeLeft))
    		lastPrintTime = currentTime
    	end
    
    	if currentMode == 1 then
    		stand()
    	elseif currentMode == 2 then
    		followNearestPlayer()
    	elseif currentMode == 3 then
    		randomMovement()
    	end
    
    	if math.random() < 0.2 then
    		currentMode = math.random(1, 3)
    		local elapsedTime = 0
    		while elapsedTime < modeChangeCooldown do
    			if paused then break end
    			wait(0.1)
    			elapsedTime = elapsedTime + 0.1
    		end
    	end
    end
    
    wait(0.1) -- Small wait to prevent excessive looping
    

    end

    it doesn't detect any changes in the CanGrab.text can you fix that + enhance and rework the whole script

  2. Assistant

    Based on the code you've provided, the main issue is that the CanGrab object is not being properly detected or monitored for changes. Additionally, there are opportunities to enhance and rework the script for better reliability and maintainability. Below, I've addressed the problem and provided a reworked version of your script with improvements.

    Problem Explanation:

    1. Incorrect Property Access:

      • You mentioned CanGrab.text, which suggests that CanGrab is a GUI element like a TextLabel or TextBox, not a StringValue.
      • In your code, you're treating CanGrab as a StringValue and accessing canGrab.Value. If CanGrab is a GUI element, you should access canGrab.Text instead.
    2. Objects Not Present Initially:

      • The Accessory, Cap, or CanGrab objects might not be present in the character when the script runs. If they are added later, the script won't set up the necessary listeners.

    Solution:

    1. Adjust the Property Access:

      • Modify the code to check the class of CanGrab and access the correct property (Value for StringValue, Text for GUI elements).
    2. Set Up Dynamic Listeners:

      • Set up listeners to monitor when Accessory, Cap, or CanGrab are added to the character.
      • Use the ChildAdded event to detect when these parts are added after the script has started.
      • When CanGrab is added, set up a listener for its property changes.
    3. Enhance and Rework the Script:

      • Organize the code into functions and sections for better readability.
      • Add comments and print statements for debugging and clarity.
      • Handle paused state transitions more robustly.

    Reworked Script:

    -- Get references to character components
    local character = script.Parent
    local humanoid = character:WaitForChild("Humanoid")
    local rootPart = character:WaitForChild("HumanoidRootPart")
    local head = character:WaitForChild("Head")
    
    -- Services
    local TweenService = game:GetService("TweenService")
    local PathfindingService = game:GetService("PathfindingService")
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    
    -- Variables
    local currentMode = 1
    local modeChangeCooldown = 5 -- seconds
    local soundPlayChance = 0.2 -- 20% chance to play a sound each iteration
    local lifespanDuration = 10 -- seconds
    local spawnTime = tick()
    
    -- Paused state variables
    local paused = false -- Global variable to indicate if the AI is paused
    local currentTween -- Variable to hold the current tween for cancellation
    local moveConnection -- Variable to hold the MoveToFinished connection for cancellation
    local movementInterrupted = false -- Flag to indicate if movement was interrupted
    
    -- Function to determine if the AI should pause
    local function shouldPauseAI()
        local accessory = character:FindFirstChild("Accessory")
        if accessory then
            local cap = accessory:FindFirstChild("Cap")
            if cap then
                local canGrab = cap:FindFirstChild("CanGrab")
                if canGrab then
                    if canGrab:IsA("StringValue") then
                        return canGrab.Value ~= ""
                    elseif canGrab:IsA("TextLabel") or canGrab:IsA("TextBox") then
                        return canGrab.Text ~= ""
                    end
                end
            end
        end
        return false
    end
    
    -- Function to update the paused state
    local function updatePausedState()
        paused = shouldPauseAI()
    end
    
    -- Function to set up listeners for CanGrab
    local function setupCanGrabListener()
        local function onCanGrabAdded(canGrab)
            if canGrab:IsA("StringValue") or canGrab:IsA("TextLabel") or canGrab:IsA("TextBox") then
                local propertyToMonitor = canGrab:IsA("StringValue") and "Value" or "Text"
    
                canGrab:GetPropertyChangedSignal(propertyToMonitor):Connect(function()
                    print("CanGrab value changed!")
                    updatePausedState()
                    if paused then
                        print("AI is now paused.")
                        -- Interrupt any ongoing actions
                        if currentTween then
                            currentTween:Cancel()
                        end
                        if moveConnection then
                            moveConnection:Disconnect()
                        end
                        humanoid:Move(Vector3.new(0, 0, 0))
                        movementInterrupted = true
                    else
                        print("AI is now unpaused.")
                    end
                end)
    
                -- Update paused state initially
                updatePausedState()
            end
        end
    
        local function onCapAdded(cap)
            -- Monitor existing CanGrab
            local canGrab = cap:FindFirstChild("CanGrab")
            if canGrab then
                onCanGrabAdded(canGrab)
            end
            -- Monitor future CanGrab additions
            cap.ChildAdded:Connect(function(child)
                if child.Name == "CanGrab" then
                    onCanGrabAdded(child)
                end
            end)
        end
    
        local function onAccessoryAdded(accessory)
            -- Monitor existing Cap
            local cap = accessory:FindFirstChild("Cap")
            if cap then
                onCapAdded(cap)
            end
            -- Monitor future Cap additions
            accessory.ChildAdded:Connect(function(child)
                if child.Name == "Cap" then
                    onCapAdded(child)
                end
            end)
        end
    
        -- Monitor existing Accessory
        local accessory = character:FindFirstChild("Accessory")
        if accessory then
            onAccessoryAdded(accessory)
        end
        -- Monitor future Accessory additions
        character.ChildAdded:Connect(function(child)
            if child.Name == "Accessory" then
                onAccessoryAdded(child)
            end
        end)
    end
    
    -- Initialize the CanGrab listener
    setupCanGrabListener()
    
    -- Function to get a random position for movement
    local function getRandomPosition()
        local randomAngle = math.random() * math.pi * 2
        local randomDistance = math.random(10, 30)
        local newX = rootPart.Position.X + math.cos(randomAngle) * randomDistance
        local newZ = rootPart.Position.Z + math.sin(randomAngle) * randomDistance
        return Vector3.new(newX, rootPart.Position.Y, newZ)
    end
    
    -- Function to get nearby players within a certain distance
    local function getNearbyPlayers(maxDistance)
        local nearbyPlayers = {}
        for _, player in pairs(game.Players:GetPlayers()) do
            if player.Character and player.Character ~= character then
                local hrp = player.Character:FindFirstChild("HumanoidRootPart")
                if hrp then
                    local distance = (hrp.Position - rootPart.Position).Magnitude
                    if distance <= maxDistance then
                        table.insert(nearbyPlayers, player.Character)
                    end
                end
            end
        end
        return nearbyPlayers
    end
    
    -- Function to get the nearest player
    local function getNearestPlayer()
        local nearestPlayer = nil
        local nearestDistance = math.huge
    
        for _, player in pairs(game.Players:GetPlayers()) do
            if player.Character and player.Character ~= character then
                local hrp = player.Character:FindFirstChild("HumanoidRootPart")
                if hrp then
                    local distance = (hrp.Position - rootPart.Position).Magnitude
                    if distance < nearestDistance then
                        nearestDistance = distance
                        nearestPlayer = player.Character
                    end
                end
            end
        end
    
        return nearestPlayer
    end
    
    -- Function to move to a specified position
    local function moveToPosition(targetPosition)
        if paused then return end -- Stop immediately if paused
        local path = PathfindingService:CreatePath()
        pcall(function() -- Use pcall to handle any errors with pathfinding
            path:ComputeAsync(rootPart.Position, targetPosition)
        end)
    
        if path.Status == Enum.PathStatus.Success then
            local waypoints = path:GetWaypoints()
    
            for i, waypoint in ipairs(waypoints) do
                if paused then
                    humanoid:Move(Vector3.new(0, 0, 0))
                    return
                end
                if waypoint.Action == Enum.PathWaypointAction.Jump then
                    humanoid.Jump = true
                end
                movementInterrupted = false
                humanoid:MoveTo(waypoint.Position)
                moveConnection = humanoid.MoveToFinished:Connect(function(reached)
                    moveConnection:Disconnect()
                end)
                repeat
                    if paused or movementInterrupted then
                        humanoid:Move(Vector3.new(0, 0, 0))
                        if moveConnection then
                            moveConnection:Disconnect()
                        end
                        return
                    end
                    wait()
                until not humanoid.MoveToFinished.Connected
            end
        end
    end
    
    -- Function to rotate the character to look at a target position
    local function tweenLookAt(targetPosition, duration)
        if paused then return end -- Stop immediately if paused
        local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
        local endCFrame = CFrame.new(rootPart.Position, Vector3.new(targetPosition.X, rootPart.Position.Y, targetPosition.Z))
    
        currentTween = TweenService:Create(rootPart, tweenInfo, {CFrame = endCFrame})
        currentTween:Play()
    
        local tweenComplete = false
        currentTween.Completed:Connect(function()
            tweenComplete = true
        end)
        repeat
            if paused then
                currentTween:Cancel()
                return
            end
            wait()
        until tweenComplete
    end
    
    -- Function to make the character look at a random nearby player
    local function lookAtRandomNearbyPlayer()
        local nearbyPlayers = getNearbyPlayers(50) -- Adjust the max distance as needed
        if #nearbyPlayers > 0 then
            local randomPlayer = nearbyPlayers[math.random(1, #nearbyPlayers)]
            tweenLookAt(randomPlayer.HumanoidRootPart.Position, 0.5)
        end
    end
    
    -- Function to play a random sound
    local function playRandomSound()
        if paused then return end -- Do not play sounds if paused
        if math.random() < soundPlayChance then
            local soundNumber = math.random(1, 4)
            local soundName = "Talk" .. soundNumber
            local sound = head:FindFirstChild(soundName)
            if sound and sound:IsA("Sound") then
                sound:Play()
            end
        end
    end
    
    -- AI Behavior Functions
    local function stand()
        if paused then return end
        lookAtRandomNearbyPlayer()
        playRandomSound()
        wait(1)
    end
    
    local function followNearestPlayer()
        if paused then return end
        local nearestPlayer = getNearestPlayer()
        if nearestPlayer then
            local targetPosition = nearestPlayer.HumanoidRootPart.Position
            local randomOffset = Vector3.new(math.random(-5, 5), 0, math.random(-5, 5))
            moveToPosition(targetPosition + randomOffset)
        end
        playRandomSound()
        wait(1)
    end
    
    local function randomMovement()
        if paused then return end
        local targetPosition = getRandomPosition()
        local midPoint1 = rootPart.Position:Lerp(targetPosition, 0.33) + Vector3.new(math.random(-5, 5), 0, math.random(-5, 5))
        local midPoint2 = rootPart.Position:Lerp(targetPosition, 0.66) + Vector3.new(math.random(-5, 5), 0, math.random(-5, 5))
    
        -- Move to midPoint1
        moveToPosition(midPoint1)
        playRandomSound()
        wait(0.5)
    
        -- Move to midPoint2
        moveToPosition(midPoint2)
        playRandomSound()
        wait(0.5)
    
        -- Move to targetPosition
        moveToPosition(targetPosition)
        playRandomSound()
        wait(0.5)
    
        lookAtRandomNearbyPlayer()
        playRandomSound()
        wait(1 + math.random())
    end
    
    -- Function to check if any player is near the mushroom
    local function isPlayerNearMushroom(mushroom)
        local nearbyPlayers = game.Players:GetPlayers()
        for _, player in ipairs(nearbyPlayers) do
            if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
                local distance = (player.Character.HumanoidRootPart.Position - mushroom.PrimaryPart.Position).Magnitude
                if distance <= 10 then -- Adjust this distance as needed
                    return true
                end
            end
        end
        return false
    end
    
    -- Function to transform the character into a mushroom
    local function transformIntoMushroom()
        if paused then return end
        local mushroomModel = ReplicatedStorage:FindFirstChild("Mushroom")
        if mushroomModel then
            local mushroom = mushroomModel:Clone()
            mushroom.Parent = workspace
            mushroom:SetPrimaryPartCFrame(rootPart.CFrame)
    
            -- Hide the character
            character.Parent = ReplicatedStorage
    
            -- Wait until a player approaches the mushroom
            while true do
                if isPlayerNearMushroom(mushroom) then
                    mushroom:Destroy()
                    character.Parent = workspace
                    spawnTime = tick() -- Reset the spawn time
                    print("Character respawned. Timer reset.")
                    return
                end
                if paused then
                    -- Wait while paused
                    repeat wait(1) until not paused
                end
                wait(1)
            end
        else
            warn("Mushroom model not found in ReplicatedStorage")
        end
    end
    
    -- Play death sound when the character dies
    humanoid.Died:Connect(function()
        local deathSound = head:FindFirstChild("Death")
        if deathSound and deathSound:IsA("Sound") then
            deathSound:Play()
        end
    end)
    
    -- Main loop
    local lastPrintTime = 0
    local printInterval = 30 -- Print time left every 30 seconds
    
    while true do
        if paused then
            humanoid:Move(Vector3.new(0, 0, 0))
            -- Wait while paused
            repeat wait(0.1) until not paused
        end
    
        local currentTime = tick()
        local timeElapsed = currentTime - spawnTime
        local timeLeft = lifespanDuration - timeElapsed
    
        if timeLeft <= 0 then
            print("Transforming into mushroom...")
            transformIntoMushroom()
        else
            if currentTime - lastPrintTime >= printInterval then
                print(string.format("Time left before transforming: %.0f seconds", timeLeft))
                lastPrintTime = currentTime
            end
    
            if currentMode == 1 then
                stand()
            elseif currentMode == 2 then
                followNearestPlayer()
            elseif currentMode == 3 then
                randomMovement()
            end
    
            if math.random() < 0.2 then
                currentMode = math.random(1, 3)
                local elapsedTime = 0
                while elapsedTime < modeChangeCooldown do
                    if paused then break end
                    wait(0.1)
                    elapsedTime = elapsedTime + 0.1
                end
            end
        end
    
        wait(0.1) -- Small wait to prevent excessive looping
    end
    

    Key Changes and Enhancements:

    1. Dynamic Listener Setup:

      • The setupCanGrabListener function now dynamically listens for when Accessory, Cap, or CanGrab are added to the character.
      • It uses the ChildAdded event to set up listeners when these parts are added after the script starts.
    2. Proper Property Access:

      • Adjusted the code to check the class of CanGrab and access the appropriate property (Value for StringValue, Text for GUI elements like TextLabel or TextBox).
    3. Improved Paused State Handling:

      • The script now updates the paused state whenever CanGrab is added or its property changes.
      • Added print statements to indicate when the AI is paused or unpaused for easier debugging.
    4. Code Organization:

      • Organized the code into logical sections: initialization, utility functions, AI behavior functions, and the main loop.
      • Added comments throughout the code for clarity.
    5. Simplified AI Behavior Functions:

      • Cleaned up functions like stand, followNearestPlayer, and randomMovement for better readability.
      • Used wait() calls directly instead of custom loops where appropriate.
    6. Error Handling:

      • Added checks to ensure objects exist before attempting to access their properties.
      • Used warn statements to alert if expected models aren't found in ReplicatedStorage.

    Conclusion:

    This reworked script should correctly detect changes in the CanGrab object's text or value and pause or resume the AI accordingly. Additionally, the script has been enhanced for better reliability, readability, and maintainability.


    Note: Make sure to adjust the class checks and property names (StringValue, TextLabel, TextBox, Value, Text) based on the actual types used in your game. If CanGrab is another type of object or uses a different property, update the checks and property accesses accordingly.