Pathfinding service not working correctly

I was working on my Tower Defense Game, in the process of making a new map, when I found out my Pathfinding wasn’t working right.

The zombies in my game use PathFinding Service to know where to go, based on preset waypoints.
As you can see in the video below, the zombies don’t seem to follow the road: Instead, they go behind a building.

So far, I have tried the following:

  • Placing invisible walls behind the building (They take another path, but still out of the road)
  • Making a collision group so the zombies can’t collide with anything but the path (They still try to go behind the building, but they fall into the void)
  • Placing multiple waypoints close to eachother (They go up until the last waypoint and go back to take that path)

Here’s the code I used:

local pFS = game:GetService("PathfindingService")

			local success, errorm = pcall(function()
				for _, waypoints in pairs(script.Parent.Waypoints:GetChildren()) do
					local path = pFS:CreatePath()
					path:ComputeAsync(zombie.HumanoidRootPart.Position, waypoints.Position)
					local wp = path:GetWaypoints()
					for __, wp2 in pairs(wp) do
						zombie.Humanoid:MoveTo(wp2.Position)
						zombie.Humanoid.MoveToFinished:Wait()
					end
				end
			end)

Here’s a map of all the waypoints:

If anyone knows how to fix this / needs any more info, please tell me. Thanks in advance!

There must be a waypoint behind the building or something could be blocking the path somehow. Double check for things like this.

If you checked for these things and the zombies still follow the incorrect path, then it must have something to do with the order of the way points in the folder in workspace.

Go through each way point in that folder starting from the first item to the last and make sure it doesn’t skip any way points in between.

Hopefully this helps!

I dont think using GetChildren is the best option for your case since you want the zombies to follow a specific path step by step and the function may lead to them skipping waypoints, if you name the waypoints in a crescent numeric order something near this must be possible :

local WayPointsQuantity = #script.Parent.Waypoints:GetChildren()

for WayPointNumber = 1, WayPointsQuantity do
	local point = script.Parent.Waypoints[tostring(WayPointNumber)]
	local path = pFS:CreatePath()
	path:ComputeAsync(zombie.HumanoidRootPart.Position, point.Position)
	local wp = path:GetWaypoints()
	
	for _, wp2 in pairs(wp) do
		zombie.Humanoid:MoveTo(wp2.Position)
		zombie.Humanoid.MoveToFinished:Wait()
	end
end

I have checked for any waypoints in random spots such as that, but it seems that I have not accidentally placed more than I should’ve. Thanks for the tip anyways!

Thanks to this, I accidentally fixed another bug I was working on, but the zombies keep going around the building. Thanks again!