Need help fixing pathfinding npc

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

1 Like

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()
1 Like

image
Getting an error

1 Like

Sorry. Replace those colons before MoveToFinished with periods.

1 Like

Its moveto.Movetofinished, Just Realized that

1 Like

Works good to get to the way point, but it is not path finding “BackHome”

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.