Is PathFindingService reliable for tracking players?

Okay so i’m currently making an NPC that will follow the nearest player in it’s proximity by using PathFindingService and its working alright-ish but i want it to be smooth.

now currently i just have a a function that checks whoever is the nearest player and i have another function that uses the pathfinding service that calls it and then looks at the coordinates then follows it, problem is that it just goes to the previous calculated position so its not a constant movement it stops in between.

now i’ve tried ways to make it quicker but i have failed it either stops moving at all or it wont move. i was looking into ray casting but i couldn’t figure it out how to get it to work

local function followNearestPlayer()
	local nearestPlayer = getNearestPlayer()
	if nearestPlayer and nearestPlayer.Character and nearestPlayer.Character:FindFirstChild("HumanoidRootPart") then
		-- If a player is found, set walk speed to normal and follow
		human.WalkSpeed = normalWalkSpeed
		local targetPosition = nearestPlayer.Character.HumanoidRootPart.Position
		local path = PathfindingService:CreatePath()
		path:ComputeAsync(torso.Position, targetPosition)
		local waypoints = path:GetWaypoints()

		path.Blocked:Connect(function()
			print("Blocked")
		end)

		for i, waypoint in waypoints do
		--[[	local part = Instance.new("Part")
			part.Shape = "Ball"
			part.Material = "Neon"
			part.Size = Vector3.new(0.6, 0.6, 0.6)
			part.Position = waypoint.Position + Vector3.new(0, 2, 0)
			part.Anchored = true
			part.CanCollide = false
			part.Parent = game.Workspace]]

			if waypoint.Action == Enum.PathWaypointAction.Jump then
				human:ChangeState(Enum.HumanoidStateType.Jumping)
			end

			human:MoveTo(waypoint.Position)
			human.MoveToFinished:Wait(1)
			-- part:Destroy()
			print("Moving to position")
		end

		human:MoveTo(targetPosition)
	else
		-- If no player is found, set walk speed to 0
		human.WalkSpeed = 0
		print("No players nearby, NPC stops.")
	end
end

(Maybe important to add, i have 2 other scripts on the NPC, one handles it attacking and another handles animation based on the speed of the NPC)

Why are you waiting another second in the :MoveToFinished? Try removing it

Tried it just now, same thing. it just takes a second in general to recalculate a path to the new position of the player and in between that it stops moving (removing that second there makes it stop while already having a path to go to, it then just stops and goes stops and goes)

You are calling the function above in a while loop right? Then it’s probably the other scripts that are causing the one second pause. Because it shouldn’t stop if there are players nearby

i only have followNearestPlayer() in a while loop since ofc i need it always running, the function that gets the players location is done by getting every player and calculating the distance thats in between the NPC and the player then storing it in a local which then gets called into followNearestPlayer() the thing i can think of that might cause it to stop moving in between the new paths is that the followNearestPlayer itself is a loop but i need it set in a loop since i need it to always be running

local function getNearestPlayer()
	local nearestPlayer = nil
	local shortestDistance = math.huge

	for _, player in Players:GetPlayers() do
		local character = player.Character
		if character and character:FindFirstChild("HumanoidRootPart") then
			local distance = (character.HumanoidRootPart.Position - torso.Position).Magnitude
			if distance < shortestDistance and distance <= detectionRange then
				shortestDistance = distance
				nearestPlayer = player
			end
		end
	end

	return nearestPlayer
end

Yea there’s nothing wrong with the function since the loop is mandatory. Like I said above, prob the other scripts. Does it prints NPC stops?

the entire script technically works as how its suppose to but its just scratching my brain why its slow.