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
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 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.
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.
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?