Why can i set the networkowner to the local player?

Hey guys. I have more of a question of a bit of code, as to why it works.

I have an NPC pathfinding system + ragdolling on death, which was lagging due to problems. I tried alot of different things such as setnetworkowner and a few other tacics. The way i solved this lagging was by adding SetNetworkowner to the local player.

module.SpawnNPC = function(pos, Class)
	local NPC = NPCFolder:FindFirstChild(Class):Clone()
	NPC:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(pos)
	NPC.Parent = workspace
	
	local decendants = NPC:GetDescendants()
	for i, v in decendants do
		if v:IsA("BasePart") then
			v:SetNetworkOwner(game.Players.LocalPlayer)
		end
	end
	
	return NPC
end
return module

Why did this solve my problem? I look through what setnetworkowner does, and it sets the owner to a single player. How can i set the networkowner to the localplayer? By the way, this does not reduce ping/incoming data, but fixed all my problems.

If i need to make myself more clear please comment.

So either a player or the server owns the physics for a unanchored part. If you set the network owner to the server, which it is by default in most cases, the server handles all the physics for that specific part. That means, that for your ragdoll, the server would handle the physics. So the server has to send a message to the client every X seconds, Telling the client where the ragdoll’s limbs are. The client then has to set the ragdoll’s limbs to that position, which may make it look jittery, as the client might have predicted a slightly different position, and applied that, and now has to change it to what it actually should be.

But, if the client has network ownership for a part, he does the physics calculations, which means he will always be right in determining where the ragdoll is. However, other players in the game would probably see an even laggier ragdoll, because the position of the ragdoll needs to be send from the Ownership’s device, to the server, to the other player, and by then, the ragdoll would be in a slightly different position, and now has to change it, which would make it look even more jittery.

So if the ragdoll is only seen by 1 player, and the sever, you can usually set Ownership to the player. But if multiple players are viewing the ragdoll, 1 player will see a smooth ragdoll, but for all the others, it will be more laggier than usual.

That makes sense, but by puttin the network owner to the local player, this is laggyness for other players and the server is fixed. Why?

Hmm, I’m not sure. Is your script a local script, or a regular script?

server script. Not really a point to putting network owner via a local script

That’s what I thought. But, if it is a regular script, how / why are you using .LocalPlayer? The server doesn’t have a player object, so game.Players.LocalPlayer would be nil.

setting v:SetNetworkOwner(nil) sets ownership to the server. And you want the network ownership to be for a player, correct? In that case, the best thing to do would to check which player is the closest using magnitude checks, and the closest player would get ownership. But I’m pretty sure the local player who is closest automatically gets ownership over physics related items within 20 studs. If you want to change that number, you can do →

local CurrentShortestDistance = 99999
for i, player in ipairs(game.Players:GetPlayers()) do
if (player.Character.HumanoidRootPart.Position-pos).Magnitude < CurrentShortestDistance then
CurrentShortestDistance = (player.Character.HumanoidRootPart.Position-pos).Magnitude
Npc:SetNetworkOwner(player)
end

This sets ownership to the player that is closest to the npc.
It’s not perfect, it will need to be optimized more, and be done every few seconds to work well, but that’s essentially the starting block.

huh. But when i set the network owner to a certain player ex. game.Players.ullest, it lags like crazy. And when i set it to nil, it fails stress tests

Hmm. What is the NPC doing in the game? Is it standing there? Does it walk around? Is it a custom rig?

How many NPC’s are you making?

Huh wierd, now that i run nil it works. Wierd. Thanks clarifying things thought!

Followup question. Is there any good way i can do the following?

– Server calculates path
– Server tells each player to calculate the physics and move the player
– Server gets data from random client and computes on the server

Any way to do this? When i have ex. 30 NPCs walking around, the sent data gets a bit much

I tried setting the value to a networkowner to a player, doesnt work

Hmm. A few questions.

Are you using pathfinding service to find and compute a path?
Does the character you are moving have a Humanoid?

Couldn’t you just the the server handle physics and movement, and not require each player to calculate it?

I am using pathfinding service, the character is a r6 with a humanoid.

If i let the server handle the physics, doesnt that create lag and unnessecary stress?

Yes, it would, But if each player is handling the physics, it will create more lag and stress, not for the server, but for the player.

Each player handling the physics for 30 NPC’s would have performance issues, especially on a small device like a phone or tablet.

On the server, it would probably have a better time running it, because it is more powerful, but also because it doesn’t have to check a random client every second or two. And who knows, maybe that client is hacking. If that did happen, the NPC, may not exist, or may the client may purposely move the Npc to an incorrect position. The server would then have to run the checks to make sure that is a correct position, compare it to other clients, and if any of those clients are having lag or issues, the whole system might collapse. It would be better, and easier to just have the server handle it.

If you want to decrease lag, humanoids are your worst enemy. They take a bunch of computing power, and haven’t been updated in a while I think.

There are some alternatives that work better than humanoids, and if you do decide to use humanoids, there are some methods to reduce lag.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.