Pathfinding Issue - NPCs going back and forth

I’ve been working on a pathfinding system for noobs in my game, when I found this weird bug were they would go back and forth. I fixed it by skipping the first waypoint but it didn’t completely solve it. Is there any way I can fix it?

Here is the code:

if path.Status == Enum.PathStatus.Success then
	self.pathCount = self.pathCount + 1
	local currentPathCount = self.pathCount

	for i,waypoint in ipairs(waypoints) do
		if currentPathCount ~= self.pathCount then
			return
		end

		if waypoint.Action == Enum.PathWaypointAction.Jump then
			human.Jump = true
		end

		human:MoveTo(waypoint.Position)
		local moveSuccess = human.MoveToFinished:Wait()
		if not moveSuccess then
			break
		end
	end
end

How often are you setting the goal position? Update it constantly so that they follow the player. You can use heartbeat and tick() for this.

Knowing that Pathfinding will find a way to get past any collision obstacles, they are probably doing it because they collide with each other, try disabling collision or work something on top of that.

I’m having the exact same issue,

Have you ever found a solution yet?

The project I’m talking about in this post was long abandoned and I’m no longer working on it. The only solution that mostly worked was just skipping the first waypoint. It might be because of collision groups disabling collision between the NPCs and the player but I’m not entirely sure.

You could also replace the MoveToFinished event with a repeat every 0.1 seconds that waits until the NPCs is like 4 studs away (you’ll probably need to adjust this) from the next position. MoveToFinished can be really buggy and janky.

1 Like

I’m confident the cause of your issue is not collisions, as I’ve tried removing collisions between my “NPCs”, and we’d still experience exactly identical issues.

Giving the repeat one a shot, and not going to lie, IT WORKS A LOT better than the MoveToFinished pause.