NPC Lagging when using pathfinding

Hi! I am trying to use pathfinding on NPCS but for some reason they lag. Yes. I set network ownership to nil and it still does not work. I can’t tell if I’m using it correctly.

Here is the script btw!

This Is The NetworkOwnership
script.Parent.HumanoidRootPart:SetNetworkOwner(nil)

This is the pathfinding

function WalkTo(Position)
	local pathfindingService = game:GetService("PathfindingService")
	local body = script.Parent:FindFirstChild("HumanoidRootPart") or script.Parent:FindFirstChild("UpperTorso")
	local path = pathfindingService:CreatePath()
	path:ComputeAsync(body.Position, Position.Position)
	local waypoints = path:GetWaypoints()
	for k, waypoint in pairs(waypoints) do
		if STOP_01 then return end
		Humanoid:MoveTo(waypoint.Position)
		Humanoid.MoveToFinished:Wait()
	end
	Humanoid:MoveTo(Position.Position)
end

If anyone could help, then please respond!

4 Likes

so it depends What is the lag that you are experiencing, is it going to the path Waypoint and just stopping for a brief second, or is it lagging your game.

1 Like

Its not lagging my game, the NPC itself walks for a second then stops for a half a second then walks and it does it forever.

1 Like

Your code worked fine when I tested it. It might have something to do with the variable STOP_01 that might be causing the NPC to prematurely cancel walking.

1 Like

Its been doing that way before I used the variable stop

1 Like

I made a test AI that uses the code but I have a hunch it has to do something with the Humanoid.WalkToFinished:Wait() Because after a while the humanoid starts to stutter when walking. I think it may just be lag due to Roblox’s functions not being optimized somehow.
Maybe try a different method of telling when the humanoid is in the spot

1 Like

there was one thing I forgot to do and it was set the network owner, Now it works perfectly fine, I have no idea what your issue could be, Did you set the network owner of the HumanoidRootPart?

1 Like

Yes, But I’m having second thoughts that it could just be the game itself, since the ping is pretty high. Is that another reason why its doing that?

1 Like

I had this problem recently, I set all the NPC’s parts NetworkOwnership to the server and it worked for me. Not just the HumanoidRootPart

	for _, descendant in pairs(model:GetDescendants()) do
		if descendant:IsA("BasePart") then
			descendant:SetNetworkOwner(nil)
		end
	end

yes I do believe that it could be an issue with the fact that Roblox Automatically sends ownership of a parts physics to the client, And with that the check is waiting for the client to respond with the new data. And there is a high chance that ping has a lot to say in this issue

Do not set NPC client-ownership to the Server,
Replicate the NPC to each client instead.

Try this. script.Parent.HumanoidRootPart:SetNetworkOwner(nil)

That would might work. If it didn’t work, then it doesn’t get connected.

So I fixed the ping and it SLIGHTLY fixed it!

1 Like