Help with NPC pathfinding

A Pathfinding system like this, it follows a set path
image

I have a script for this, however, over time new path bricks get added into the world. The NPC only follows the path that were created prior to the new bricks. So basically , it only follows the old ones, not the newly generated ones.

Could anyone help me?

2 Likes

This isn’t path finding, since you defined the path. Use a while loop to make the humanoid continue to walk to the next point in line.

1 Like

Sorry about that, but I AM using a while loop.
The new parts get generated, and the NPC only stays on the first path. They don’t go to the new bricks.

The loop is a while true loop, is that why there’s a problem?

May you show your current code then? Hard to help you without it.

local NPC = script.Parent
local Waypoints = game.Workspace.Nodes -- folder that contains the waypoint bricks

while true do
	for Waypoint=1, #Waypoints:GetChildren() do
		NPC.Humanoid:MoveTo(Waypoints[Waypoint].Position)
		NPC.Humanoid.MoveToFinished:Wait()
	end
end

Here’s my code.

Quick question, what are the names of these waypoints?

1, 2, 3, 4, 5, 6
They are all numbered, it’s the order in which the NPC follows.

I think this should fix the problem:

local NPC = script.Parent
local waypoints = workspace.Nodes -- folder that contains the waypoint bricks

local index = 0
while true do
	index += 1
	local waypoint = waypoints:WaitForChild(tostring(index), 10) -- 10 is the amount of time to wait before giving up
	if waypoint then
		NPC.Humanoid:MoveTo(waypoint.Position)
		NPC.Humanoid.MoveToFinished:Wait()
	else break end
end

This will make the NPC follow points in order, and if there is none left, it waits 10 seconds for more points, otherwise it stops walking.

2 Likes

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