My pathfinding script currently only lets my NPC walk to 1 waypoint. How do I make it walk to more waypoints?

char = script.Parent
pathFinder = game:GetService(“PathfindingService”)

path = pathFinder:CreatePath()

path:ComputeAsync(char.HumanoidRootPart.Position, workspace.Destination.Position)

for i, wayPoint in pairs(path:GetWaypoints()) do
char.Humanoid:MoveTo(wayPoint.Position)

if wayPoint.Action == Enum.PathWaypointAction.Jump then
	char.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end

char.Humanoid.MoveToFinished:wait()	
--script.Parent.Donelol.Value = true

end

What seems to be the issue? What you’re doing seems to be correct.

You can make a function, for example;

char = script.Parent
repeat wait() until char:FindFirstChild("HumanoidRootPart")  and char:FindFirstChild("Humanoid")
PathfindingService = game:GetService(“PathfindingService”)

local function walkTo(part)
   path = PathfindingService:CreatePath()
   path:ComputeAsync(char.HumanoidRootPart.Position, part.Position)
   for i, wayPoint in pairs(path:GetWaypoints()) do
      char.Humanoid:MoveTo(wayPoint.Position)
      if wayPoint.Action == Enum.PathWaypointAction.Jump then
	     char.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
      end
      char.Humanoid.MoveToFinished:wait()	
   end
end

walkTo(workspace.Destination)
1 Like

Yeah but I want my npc to walk the waypoints in an order so destination1 destination2 etc.

1 Like

how do i let the script know when my npc finished walking to the waypoint ? and then do the next one ?

1 Like

With that function you can do this,

walkTo(workspace.Destination1)
walkTo(workspace.Destination2)

It uses a for loop, script won’t continue until first function finishes. So script will wait until character walks to the destination1 and then will start going to the destination2.

thanks! I will try this! i will let you know if this works

1 Like


i have tried and it gave me an error message right now im trying to fix it but im not sure if I will be able to

1 Like

It is because script runs before HumanoidRootPart exists. I updated the function, please try again.

1 Like