Here’s a simple pattern I use to send an NPC through a series of waypoints. There are two flavors below:
- “Raw” MoveTo-through-Parts
- PathfindingService-powered (good if you need obstacle avoidance)
Pick one and compare it to your code. If it still doesn’t work, post the exact snippet you’re using (or describe the error), and we’ll zero in on the bug.
— — —
- RAW MoveTo Part-by-Part
This simply grabs Parts in a folder (sorted by name), then tells the Humanoid to MoveTo each one in turn.
local npc = workspace.NPC -- your NPC model
local waypointsF = workspace.Waypoints -- Folder containing Part1, Part2, …
-- 1) Collect & sort by numeric suffix
local waypoints = waypointsF:GetChildren()
table.sort(waypoints, function(a,b)
local na = tonumber(a.Name:match("%d+"))
local nb = tonumber(b.Name:match("%d+"))
return na < nb
end)
-- 2) Move NPC through them
local humanoid = npc:FindFirstChildOfClass("Humanoid")
spawn(function()
for i, wp in ipairs(waypoints) do
print("Going to waypoint "..wp.Name, wp.Position)
humanoid:MoveTo(wp.Position)
local success = humanoid.MoveToFinished:Wait() -- yields until it arrives (or fails)
if not success then
warn("Failed to reach "..wp.Name)
break
end
end
print("Done!")
end)
Things to check if it never moves:
- Is
npc pointing to the right Model?
- Does
npc have a Humanoid and a HumanoidRootPart (or Torso)?
- Are your waypoint parts reachable (not anchored inside walls)?
- Are they named in a sortable way (Part1, Part2, …)?
— — —
2) PATHFINDING SERVICE
If your map has obstacles, use PathfindingService to compute a navigable path:
local PathService = game:GetService("PathfindingService")
local npc = workspace.NPC
local hrp = npc:WaitForChild("HumanoidRootPart")
local humanoid = npc:FindFirstChildOfClass("Humanoid")
local waypointsF = workspace.Waypoints
local waypoints = waypointsF:GetChildren()
table.sort(waypoints, function(a,b)
return tonumber(a.Name:match("%d+")) < tonumber(b.Name:match("%d+"))
end)
spawn(function()
for _, target in ipairs(waypoints) do
print("Computing path to", target.Name)
local path = PathService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
AgentWalkableTypes = {Enum.Material.Grass, Enum.Material.Concrete}
})
path:ComputeAsync(hrp.Position, target.Position)
if path.Status == Enum.PathStatus.Success then
for _, waypoint in ipairs(path:GetWaypoints()) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
else
warn("Path failed:", path.Status)
break
end
end
print("All waypoints done!")
end)