How to fix npc moving to waypoint behind them?

The title says it all but I have a pathfinding npc killer who moves to your position if you are in their line of sight, but if you get out of their line of sight and they start to pathfind, the npc moves backwards as if there is a waypoint behind them. This makes it easy to distance yourself from the npc. Any help would be appreciated!

A common technique is to just skip the first 1 or 2 waypoints

How would I make the script skip a waypoint?

post your code chars chars chars

Moving to waypoints

for _, waypoint in ipairs(waypoints) do
			humanoidRootPart:SetNetworkOwner(game.Players:GetPlayerFromCharacter(target.Parent))
			if waypoints[4] then
				humanoid:MoveTo(waypoints[4].Position)
				distance = (humanoidRootPart.Position - waypoints[4].Position).Magnitude
				if checkSight(target) then
					repeat
						wait()
						humanoid:MoveTo(target.Position)
						if target == nil or target.Parent == nil or target.Parent.Humanoid == nil or target.Parent.Humanoid.Health < 1 or humanoid.Health < 1 then
							break
						end
					until not checkSight(target)
				end
				if distance <= 0.5 then
					pathfind(target)
				end
			end

Try

for i, waypoint in ipairs(waypoints) do
    if i <= 2 then continue end --change 2 to however many waypoints you want to skip
			humanoidRootPart:SetNetworkOwner(game.Players:GetPlayerFromCharacter(target.Parent))
			if waypoints[4] then
				humanoid:MoveTo(waypoints[4].Position)
				distance = (humanoidRootPart.Position - waypoints[4].Position).Magnitude
				if checkSight(target) then
					repeat
						wait()
						humanoid:MoveTo(target.Position)
						if target == nil or target.Parent == nil or target.Parent.Humanoid == nil or target.Parent.Humanoid.Health < 1 or humanoid.Health < 1 then
							break
						end
					until not checkSight(target)
				end
				if distance <= 0.5 then
					pathfind(target)
				end
			end
1 Like

You should always skip the first waypoint. Even if the humanoid reached the goal and you compute again, 2 waypoints will always be generated.

Thanks! 2 waypoints didn’t work so I increased it until it did, and skipping 4 waypoints fixed the problem.

Edit: It doesn’t work anymore for some reason and just causes bugs.

1 Like

Skipping 4 waypoints is a lot and this may cause the humanoid to get stuck if its in a tight place like a maze. Skip only the first waypoint.

From Roblox’s pathfinding tutorial:
“Initially move to second waypoint (first waypoint is path start; skip it)”

2 Likes

Ok, I’ll test with a bit of robloxs code as the bot pauses for a second when skipping 4 points, something I didn’t notice earlier

So now my bot is very stupid and will stop moving when pathfinding

I suggest you give my SimplePath module a try. You won’t have to face any of these issues.