Script timeout: exhausted allowed execution time

I am getting this error, please help!

The line that has the error:humanoid:MoveTo(waypoint.Position)

Script:

local tiger = script.Parent
local humanoid = script.Parent.Humanoid

local PathfindingService = game:GetService("PathfindingService")
tiger.PrimaryPart:SetNetworkOwner(nil)
local attackAnim = humanoid:LoadAnimation(script.Attack)
local function canSeeTarget(target)
	local origin = tiger.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - tiger.HumanoidRootPart.Position).Unit * 40
	local ray = Ray.new(origin, direction)
	local hit, pos = workspace:FindPartOnRay(ray, tiger)
	
	
	if hit then
		if hit:IsDescendantOf(target) then
			return true
		end
	else
		return false
	end
end

local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = 40
	local nearestTarget
	
	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (tiger.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
			
			if distance < maxDistance and canSeeTarget(target) then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end
	
	return nearestTarget
end

local function getPath(destination)
	
	local pathParams = {
		["AgentHeight"] = 12,
		["AgentRadius"] = 6,
		["AgentCanJump"] = false
	}
	
	local path = PathfindingService:CreatePath(pathParams)
	

	path:ComputeAsync(tiger.HumanoidRootPart.Position, destination.Position)
	
	return path
end

local function attack(target)
	local distance = (tiger.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
	if distance > 8 then
		humanoid:MoveTo(target.HumanoidRootPart.Position)
	else
		tiger.Head.AttackSound:Play()
		local playerDeath = game.ReplicatedStorage:WaitForChild("playerdeath")
		local player = game.Players:GetPlayerFromCharacter(target)
		playerDeath:FireClient(player,tiger)

		attackAnim:Play()
		attackAnim.Stopped:Wait()
		target.Humanoid.Health = 0
	end
end


local function walkTo(destination)
	local path = getPath(destination)
	
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = findTarget()
			if target and target.Humanoid.Health > 0 then
				attack(target)
				break
			else
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		humanoid:MoveTo(destination.Position - (tiger.HumanoidRootPart.CFrame.LookVector * 10))
	end
end

local function patrol()
	local waypoints = workspace.waypoints:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum])
end

while wait(0.5) do

	patrol()
end

Error:

Workspace.Tiger.Ai:86: Script timeout: exhausted allowed execution time 
2 Likes

Edit:I know why because of while wait(0.5) do but how do I fix it?

I dont see anything wrong with this code and I’ve looked quite a while.
My suggestion:
Move

local pathParams = {
		["AgentHeight"] = 12,
		["AgentRadius"] = 6,
		["AgentCanJump"] = false
	}
	
	local path = PathfindingService:CreatePath(pathParams)

out of the function, no need to create it multiple times.

Also remove pairs(), you dont need it anymore.

Now the timeout itself might be fixed if u put a cooldown on the pathfinding? Maybe wait 8s before u can use pathfinding again?

where to put the cooldown and also removing pairs will make the for loop having an syntax error!

I think the issue is that you have no signals setup for the pathfinding. A path may become blocked and leave the function running forever. As you can see on the docs, there’s a signal for when the path is blocked that you can use.

Idiotic mistake by me oops

Something else causing an issue is also that you are running patrol every 0.5 seconds. That means you are adding patrol on top of patrol. Instead you should be sending a person out on patrol, then registrering them somewhere, wait for them to finish their patrol, then send them onto the next one. Otherwise you’ll there’ll be multiple instances of the walkTo function running on top of each other for the same humanoid.

The problems seems to be the while loop what can I do instead?

The wait function shouldn’t actually be your issue. I just saw that you don’t call any of the functions within asynchronously, which means that the while loop is blocked while the patrol and walkto functions are running.

And the moveto should be timing out by itself even if you don’t reach the waypoint soooo… huh I’m actually confused now why it’s breaking. Do you manage to get to any waypoints before the script times out or does it happen instantly?

EDIT: Whats your studio script timeout length set to? I have zero errors running your thing.

I don`t know what is it set to so how to fix it now or is this just a studio error?

You can check in file > studio settings and then in there search for timeout. But since you don’t know, it’s probably just set to the default which shouldn’t be an issue.

Honestly I have no idea how to fix this, your exact code only missing the animations,targeting and attacking runs for me without issues. Try and play around with it and disable parts to see what happens.

So it is only a studio issue or it is a game issue, because when it is a studio issue I actually don´t care

Script Timeout length:10, what to set it to like infinite lol

Nahh 10 is fine. Setting it higher is only going to keep you stuck in infinite loops for longer.

Well I can’t tell you if it’s a studio issue. If you think it might be, test it in game or restart studio.

I think this is only for debug and not really an actuall error, idk

It might be nothing but there is a waitforchild for the remoteevent in the attack script, just wondering if this part gets triggered? And whether that path is correct?

of course it get`s triggered ,why would I else my a variable for this