Constantly updating NPC path

Is there a better way in updating an NPC’s path to follow the player? Currently, whenever the player moves the NPC is unable to catch up to the player even though its humanoid walkspeed had been set to 50.
https://gyazo.com/b0a971b7714c76c0ecc8685de82997b2

Module script

function NPC_AIPathfinding.ChasePlayer(Guard, player)
	local PathfindingService = game:GetService("PathfindingService")
	
	while wait(0.2) do
		if Guard.Humanoid.Chasing.Value == false then
			break
		end
		local StartPosition = Guard.HumanoidRootPart.Position	-- Move NPC towards player previous position
		local EndPosition = player.Character.HumanoidRootPart.Position

		local Path = PathfindingService:CreatePath()
		Path:ComputeAsync(StartPosition, EndPosition)	
		
		local waypoints = Path:GetWaypoints()
		for _, waypoint in pairs(waypoints) do	-- Loop through waypoints
			Guard.Humanoid:MoveTo(waypoint.Position)
		end
	end
end

Pathfinder is not really quick, you’re creating a new path each 0.2 seconds you should better stick to just a simple follow script and detect when the npc is stuck somewhere.

I experimented a lot with the pathfinder algorithm and it seems like even very short paths will take as long as the longer ones and in your case you don’t really need one for such a close range

The way i would determine if the target is suck is by making an ETA approximation using magitude and dividing it with the walkspeed this way you will get the amount of time it will take to reach a simple point obviously you would have to expect longer or shorter times as well but for now i think it is a good solution

By target I assume you mean the NPC?
And if NPC was stuck, would I then calculate a new path to free them?

1 Like

I’ve messed about with a lot of different variations on pathfinding, most have been unsuccessful. The best I have found so far was from GnomeCode on YT

That should help you achieve what you want to do.