Question about re-computing paths less often

Hey everyone!

Currently i’m trying for my NPCs to follow a moving target (a Player in this case) only using PathfindingService, I have done this already, but i can’t seem to find a way that i would say is “convenient”.

See code below:

local function followPath(goal)
	path:ComputeAsync(npcRootPart.Position, goal.Position)
	Waypoints = {}
	if path.Status == Enum.PathStatus.Success then
		Waypoints = path:GetWaypoints()
		for i = 1, #Waypoints do
			local waypoint = Waypoints[i]
			if waypoint.Action == Enum.PathWaypointAction.Walk then
				-- npcHumanoid.MoveToFinished:Wait()
				npcHumanoid.Jump = true
		warn("No path found.")

This works, however the NPC keeps bumping into obstacles and it’s also pretty clunky if i use MoveToFinished:Wait(), things change when i remove that part from the code (as seen above), without it the NPC becomes pretty responsive and actually never gets stuck.

As far as i’m aware, the responsiveness happens because it’s re-computing the path way too often, it’s important to the question to mention this is running in a while loop each heartbeat, so my main question is… Is there a good method to compute the path less often, while also having great responsiveness?

This is an example of the while loop i’m talking about:

while somerandomthing do
	local target = findClosestTarget()
	if target then

Just to clarify 1st, I’ve tried using a raycast to detect if the way is blocked and then enter pathfinding “mode”, however, i don’t like the innaccuracy of it. If the player is in an open area the NPC is pretty dumb.

Just to clarify 2ndly, I’m not asking to be spoon-fed, i’m just asking for someone to point me into the right direction, maybe using pseudo-code or just giving me an idea on how to do it, sorry if it sounds like i’m asking for someone to make me an script, i’m just saying this because english isn’t my first language, so i’m not sure if maybe this sounds too “direct” or something. Thanks in advance!

1 Like

I have a question what do you mean by responsiveness?(i just need some clarification). I am guessing it is the time it takes the npc to react to obstacles. Either way, I do think you are computing the path a little bit too often. I think wrapping the compute code in a while loop with a wait should be sufficient enough, I feel like using a heartbeat could be a little too much as far as pathfinding goes. Also how many Npc’s are consistently computing paths, as I see this could be potentially expensive, using heartbeat and a while loop.

1 Like

What i mean with responsiveness is that it doesn’t get suck, it can go through obstacles easily.

Also, i’ve tried using a wait, specifically wait(0.5) and wait(1), but i don’t like how the NPC just stops from all sudden and then keeps walking, it looks choppy, however, there should be a way for it to keep following the path, but just re-compute it each 1 second (for example) and never stop, do you think that’s possible using PathfindingService? I’m thinking about coroutines maybe but i don’t think it’s a viable solution, seems rather hacky

1 Like

Maybe, in a theoretical sense (meaning i personally haven’t tried it) you could compute a path before the npc traverses to the end of the current path it’s following and then join it with the current path, in other words, compute a path then once the npc is lets say moving to the 2nd to last node in the path, compute a new path, so in theory the npc will never stop(abruptly).

1 Like

I’ll see what i can do, it’s getting kind of late for me.

EDIT: Alright, so i kind of give up on this, because either way the NPC will look choppy, like someone was pulling them back, i know what you meant by the way, but paths will not always have two nodes specially if it’s close to the player and if i’ve implemented what you said correctly, it’s also choppy, what i’ll do for now is to follow Developer Hub article on Pathfinding, as it has something close to what i’m trying to make (take a look at the Handling Blocked Parts section), still, thank you

1 Like