I’m working on some creatures for my game who just wonder around the map until they see a player using PathFindingService and :MoveTo()
Everything works fine, however after about a minute, :MoveTo() seems to stop working…
The mob walks from point to point smoothly then :MoveTo() begins failing and delaying at every point.
function AI.WalkToPoint(position)
NPC.Humanoid:MoveTo(position)
local doneWalking = false
local walktoFinish = nil
walktoFinish = NPC.Humanoid.MoveToFinished:Once(function() -- Fire when npc reaches finish
doneWalking = true
end)
-- Wait 8 seconds or until npc reaches finish
local startTick = tick()
repeat
task.wait()
until doneWalking or tick()-startTick >= 8
if (walktoFinish) then -- Clean up
walktoFinish:Disconnect()
end
if (not doneWalking) then -- If not at finish, try again
AI.WalkToPoint(position)
end
return true -- Return to walk to next point
end
I know :MoveTo() tries for 8 seconds then gives up, so my code allows for these 8 seconds, but waiting 8 seconds between each point isn’t exactly ideal.
I just don’t understand what is wrong, as I have tons of checks to makes sure the destination is always valid and it seems to work perfectly fine before randomly giving up.
Anyone experience this issue?