What is network ownership?
Let’s first ask the more simple question - What is the part’s network owner and what does it do?
Network owner is the BasePart’s physics handler. Any movements the BasePart makes, is handled by the network owner. The network owner could either be the server, or one of the players.
Let’s say we have Part
in workspace
. Now if a LocalScript moves the part, it will only move it for the LocalScript’s LocalPlayer.
Here’s some shapes on a client’s screen:
After moving:
As you can see, the part was moved.
Now, let’s see what the server sees:
The part has not moved.
This is because of the filter between the server and the client, that makes it so clients’ actions don’t replicate to the server.
So how do i make it replicate?
This is where network ownership comes in.
Let’s set the part’s network owner to the player.
local Part = workspace.Part1 -- The gray part on the image
Part:SetNetworkOwner(game.Players.Player1) -- Gives Player1 the network ownership
At first you might not notice anything.
But if a LocalScript in Player1’s client moves the part now, it will replicate to the server.
NOTE: YOU CAN ONLY SET NETWORK OWNERSHIP ON PARTS THAT AREN’T ANCHORED
So what can this be useful for?
Well, it’s quite simple really. Handling physics on the server may make the server slower and overall make gameplay more delayed and scuffed.
Let’s imagine a pirate ship game. Every player has it’s own pirate ship with cannonballs. Now imagine that the server has to handle the physics of every single cannonball fired. That would really make the server slow and even unplayable if someone spams the cannonballs.
This is where you can use the BasePart:SetNetworkOwner()
function.
Whenever the cannonball is fired, the server gives the ownership to the player and makes the player handle the physics.
SERVER HANDLING PHYSICS:
CLIENT HANDLING PHYSICS:
As you can tell, the movement on the client is much smoother, and it doesn’t put work on the server.
Here’s the code if anyone is wondering:
local Cannon = script.Parent -- The cannon model
local Barrel = Cannon.Barrel -- The cannon's barrel
local ClickDetector = Cannon.Fire.ClickDetector -- ClickDetector for the cannon's trigger
local force = 1000 -- Cannon ball's velocity.
local cannonball = Instance.new("Part")
cannonball.Shape = Enum.PartType.Ball
cannonball.Size = Vector3.new(2,2,2)
cannonball.CFrame = Barrel.CFrame*CFrame.new(-Barrel.Size.X/2,0,0)
function clicked(player) -- player is the clicker
local ball = cannonball:Clone()
ball.Velocity = (Barrel.CFrame*CFrame.Angles(0,-math.pi/2,0)).LookVector*force
ball.Parent = workspace
ball:SetNetworkOwner(player) -- Set's network owner to the clicker
end
ClickDetector.MouseClick:Connect(clicked) -- Connects trigger function to trigger
CONCLUSION:
Network ownership is a good way to make the server run faster and make the players’ experiences much better.