NPC won't walk to point as intended [Pathfinding]

So basically I’m working on a tower defense game. And I’m using pathfinding to make my enemies walk to the end using the path. But I noticed that when a zombie is too slow they tend to move to the next point earlier / take shortcuts. Is there any way I can prevent this from happening?


Thats the code inside of the zombie:

local PathfindingService = game:GetService("PathfindingService")
local animTrack = script.Parent.Humanoid:LoadAnimation(game.Workspace.DeathWalk)
animTrack.Looped = true
animTrack:Play()

local zombie = script.Parent
local humanoid = zombie.Humanoid

spawn(function()
	for _,waypoints in pairs(game.Workspace.Waypoints:GetChildren()) do
		local path = PathfindingService:CreatePath()
		path:ComputeAsync(zombie.HumanoidRootPart.Position, waypoints.Position)
		local wp = path:GetWaypoints()
		for _, wp in pairs(wp) do
			humanoid:MoveTo(wp.Position)
			humanoid.MoveToFinished:Wait()
		end
	end
end)

Thanks in advance,
avellx (Avex).

1 Like

(post withdrawn by author, will be automatically deleted in 1 hour unless flagged)

Well thank you I guess :smiley:

1 Like

Could you show us where all your waypoints for the enemy to travel are located?


The blue neon parts are the waypoints.

Try repeating until the ai successfully moved into point…

repeat
humanoid:MoveTo(wp.Position)
wait(.1)
until
humanoid.MoveToFinished:Wait()

Adjust the wait tho

1 Like
local PathfindingService = game:GetService("PathfindingService")
local animTrack = script.Parent.Humanoid:LoadAnimation(game.Workspace.DeathWalk)
animTrack.Looped = true
animTrack:Play()

local zombie = script.Parent
local humanoid = zombie.Humanoid

spawn(function()
	for _,waypoints in pairs(game.Workspace.Waypoints:GetChildren()) do
		local path = PathfindingService:CreatePath()
		path:ComputeAsync(zombie.HumanoidRootPart.Position, waypoints.Position)
		local wp = path:GetWaypoints()
		for _, wp in pairs(wp) do
			repeat
				humanoid:MoveTo(wp.Position)
				wait(.1)
			until humanoid.MoveToFinished:Wait()
		end
	end
end)

So i changed it to this but now he doesn’t even move. The animation still plays though.

Try adjusting the normal wait function

Do you mean inside of the repeat loop or inside of MoveToFinished:Wait() ? I tried both and neither worked.

If it doesn’t work, revert the code to yours and try looking for other solution

It looks that you already created the waypoints in game.Workspace.Waypoints and in that case, you should not need to use pathfinding service. So something like this:

for _,waypoint in pairs(workspace.Waypoints:GetChildren()) do -- You can use workspace instead of game.Workspace
	repeat
		humanoid:MoveTo(waypoint.Position)
		-- I don't think that you need the wait here
	until humanoid.MoveToFinished:Wait()
end
3 Likes

Great, this seemed to work a lot better! Thank you.

No problem, glad I could help!