Big delay when using PathfindingService for the first time?

So I haven’t used this service in ages lol, I think I’m figuring it out, but one thing I noticed is that there’s a considerably large delay when I attempt to move the player to each path node for the first time. After the delay, it starts performing more responsively.

Any clue as to what I missed here? Thanks :slight_smile:

Here’s a code snippet:

local function moveCharToNode(nodePos) --//Attempts to move the player to the specified node position
	local human = char.Humanoid
	local rootPart = char.PrimaryPart
	
	human:MoveTo(nodePos.Position)
	
	local TIMEOUT_TIME = 5
	local DISTANCE_TO_NODE = 3
	
	local timeElapsed = 0
	
	repeat --//Implement a timeout to prevent infinite yields
		local dt = runService.Heartbeat:Wait()
		local isAtNode = (rootPart.Position - nodePos.Position).Magnitude <= DISTANCE_TO_NODE
		
		timeElapsed += dt
	until not isCharActive() or timeElapsed >= TIMEOUT_TIME or isAtNode
end

local function moveCharToWaypoints() --//Attempts to the move the character to each waypoint in the path
	local waypoints = currentPath:GetWaypoints()
	
	for _, waypoint in ipairs(waypoints) do
		if not isCharActive() then return end
		
		moveCharToNode(waypoint)
	end
end

function pathfindingController:moveCharacterToPoint(targetLocation) --//Handles moving the character to the specified point
	char = player.Character
	
	if not isCharActive(char) or isRunning then return end --//Make sure the character exists
	
	isRunning = true
	
	setPlayerControlsEnabled(false)
	
	currentPath:ComputeAsync(char.PrimaryPart.Position, targetLocation)
	
	if currentPath.Status ~= Enum.PathStatus.Success then warn("Path was not made successfully") return end
	
	moveCharToWaypoints()
	
	isRunning = false
end

It was an issue with how I was checking to see if the player arrived to the waypoint. I made it a larger distance for each node and that worked good. However, I’m not sure what the most ideal way is to determine if they’ve arrived to the target node. I don’t want the player to get stuck on wall corners and stuff :confused:

This is likely to be the service creating the path itself, waypoints and all, analyzing the given obstacles.

1 Like