My pathfinding scripting is working but it is not finding the path. I am basically having an NPC go to one of 2 waypoints randomly. It goes to the points but does not path find. Here is the script
local pfs = game:GetService("PathfindingService")
local dummy = script.Parent
local Waypoint = game.Workspace.Waypoint
local parts = Waypoint:GetChildren()
local random = math.random(1, 2)
local chosenpart = parts[random]
local path = pfs:CreatePath()
path:ComputeAsync(dummy.HumanoidRootPart.Position, chosenpart.Position)
local BackHome = game.Workspace.BackHome
for _, waypoint in pairs(path:GetWaypoints()) do
dummy.Humanoid:MoveTo(chosenpart.Position)
wait(10)
dummy.Humanoid:MoveTo(BackHome.Position)
wait(10)
dummy:Destroy()
end
The reason you loop through waypoints is so you can move the NPC to each one, leading to the goal.
Try this:
for _, waypoint in pairs(path:GetWaypoints()) do
dummy.Humanoid:MoveTo(waypoint.Position)
dummy.Humanoid:MoveToFinished:Wait()
end
dummy.Humanoid:MoveTo(BackHome.Position)
dummy.Humanoid:MoveToFinished:Wait()
wait(10)
dummy:Destroy()