I’m trying to make an NPC repeat a loop that allows it to go from one part to another. It goes to the first part and then doesn’t do anything else. Any help greatly appreciated!
local NPC = script.Parent
local humanoid = NPC.Humanoid
local destination = workspace.destination
local pathfindingService = game:GetService("PathfindingService")
local function getPath(destination)
local path = pathfindingService:CreatePath()
path:ComputeAsync(NPC.Torso.Position, workspace.destination.Position)
return path
end
local function walkTo(destination)
local path = getPath(destination)
for index, waypoint in pairs(path:GetWaypoints())do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
while true do
walkTo(workspace.destination)
walkTo(workspace.destination2)
end
It seems like your .MoveToFinished:Wait() is never occuring. I would suggest having a custom function which checks the distance between the destination node and the AI, that way every couple of milliseconds it checks the distance and if it’s within an acceptable radius, you would break the function. This type of function would give you more control over your AI functionality.
To check if the second walkTo() call is occuring, add a print statement in between the calls to verify they are running.
Any path made using PathfindingService has limits, so it may not be able to navigate through complex maps and such. Try widening doorways/narrow paths, and see if that works.