Pathfinding error

Trying to make an NPC follow the closest player. I got the NPC to follow the player, but the problem is that the movement is not smooth and I don’t know why.

I’ve been searching for solutions on the dev forum, nothing helped so far though

Feel free to ask further questions, thanks.

wait(3)

local pfs = game:GetService("PathfindingService") -- Pathfinding service

local hum = script.Parent:WaitForChild("Humanoid") -- npc humanoid
local torso = script.Parent:WaitForChild("Torso") -- npc torso

local players = game:GetService("Players")
local distance
local closestPlayer, closestDistance = nil, math.huge



function checkPlayerDistance() -- Function will loop through the players and get the nearest player
	local children = players:GetChildren()

	for i,v in pairs(children) do
		distance = (v.Character:FindFirstChild("HumanoidRootPart").Position - torso.Position).magnitude

		if distance <= closestDistance then
			closestDistance = distance
			closestPlayer = v
		end
	end

	return closestPlayer 
end



while wait() do -- NPC will follow the nearest player

	local returnedPlayer = checkPlayerDistance()

	local path = pfs:CreatePath()
	path:ComputeAsync(torso.Position, returnedPlayer.Character.HumanoidRootPart.Position)
	local waypoints = path:GetWaypoints()

	for i,waypoint in pairs(waypoints) do
		hum:MoveTo(waypoint.Position)
		hum.MoveToFinished:Wait()
		
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			hum.Jump = true
		end
	end


	closestDistance = math.huge
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

3 Likes

Maybe it could be a network ownership issue.

Try setting the primary part of the NPC to server ownership.

local NPC = workspace.NPC
NPC.PrimaryPart:SetNetworkOwner(nil)
1 Like

The NPC is constantly moving to where the player used to be.

1 Like

hey thanks for the reply, it kinda made it better and got rid of occasional lag, but still constantly stops

ah yeah makes sense, do you know any fix for it? Thanks

Store the target position used to create the party. Inside the waypoints loop, check the magnitude between the target position and the player’s current position, if it gets too big (i.e the player has moved away) break out of the waypoints loop and let it recalculate a new path.

1 Like

its hard to make ai like this smooth. As it has to check constantly for where the player is. If you want something even remotely smooth, try using HeartBeats and compairing if the player is at the same location or not.

1 Like

instead of “while wait() do” you can use heartbeat from runservice. i personaly use heathbeat when i got into somethink about positioning and vector stuff

1 Like

Hey, thanks for the reply. This works

Thanks, I tried it but the result was that NPC kinda turned back for a second, not sure why

Hey thanks for the response, I tried it but the result was that NPC turned back for a brief moment