Lag spikes in multiplayer game with physics using SetNetworkOwnershipAuto

I’m currently facing an issue with my game. Players experience significant lag when interacting with a ball object that has the SetNetworkOwnershipAuto() enabled. I’m wondering if there is a better way to make this smoother, or if there is an alternative function to use. I have tried having the server be the owner of the ball object, but this causes even more lag.


As shown in the videos I have provided, when two players interact with the object, there are noticeable lag spikes. I understand that this may be due to the time it takes for the game to transfer between networks, but I hope there is still a way to improve the performance. Any suggestions or solutions would be greatly appreciated.

Thank you.

Why dont you just check which player is closest to the ball and they will have network ownership of it , also really cool game!

Basically the ball will run smoothly for the client who is closest to it but also for other players aswell

if its client sided, try moving it to server side

if your trying to accelerate it as you run into it, try a lower mass (make it hollow using negative parts)

maybe dont use network ownership, and have the network ownership be the server

That could be a solution to the problem, but I believe it will still cause the same issues. When would you suggest doing this? When the ball is touched, or on some kind of fixed loop?

As previously stated, I attempted to assign network ownership to the server, but this causes even more lag.

On some loop when the game starts, just check which players is closest to the ball, you can have a table of the players in the round and then have their magnitude towards the ball stored and then sort the table from least to greatest so that meanss the first index will be the closest player and then just make set the networkownership of the ball to that player

How frequently would you recommend iterating this loop?

try using task.wait() instead of a full-on loop at the end of the loop (the last line of the loop or the beginning) to reduce lag

i know from experience, it took a hecc of a long time to load the server, -->> (not tested) -->> and probably lag

For best results, use something like a heartbeat loop that is binded to a connection so you can easily disconnect it when the round is over!

1 Like

if you dont want it to loop forever, use a true loop but replace the true with a boolvalue or something

while (boolvalue here) do
-- script here
task.wait()
end

He could just use a heartbeat loop with a connection then just disconnect it, which is much better

1 Like

im a beginner, only knowing enough to code simple core games, so im not the most efficient

1 Like

Well they are both efficient but connections allow you to disconenct them at any time and also reconnect them so they’re pretty powerful!

1 Like

i dont get it

i am filler characters so i can post

1 Like
local ball = script.Parent
local rs = game:GetService("RunService")

local players = game:GetService("Players")
local maxDistance = 10

rs.Heartbeat:Connect(function(step)
	
	local nearestPlayer, nearestDistance
	
	for _, player in pairs(players:GetPlayers()) do
		local character = player.Character
		local distance = player:DistanceFromCharacter(ball.Position)
		if not character or 
			distance > maxDistance or
			(nearestDistance and distance >= nearestDistance)
		then
			continue
		end
		nearestDistance = distance
		nearestPlayer = player
	end
	
	if nearestPlayer then
		ball:SetNetworkOwner(nearestPlayer)
	end
	
	task.wait()
	
end)

Here’s my response to the solution you provided; do you think this code is efficient enough?

1 Like

Try to use something like this, its less lines and efficient

Basically I have a table where i store the players name and distances, and then I create a new table for the sorted version, so all i need to do is access the first key in the sorted table and it will always be the player closest to the ball

local playerDistances = {
   ["Dexter"] = 100,
   ["Noob"] =10,
   ["Player2"] = 1
}

local sorted = {}

for playerName, distanceFromBall in pairs(playerDistances) do
   table.insert(sorted, {playerName = playerName, distanceFromBall = distanceFromBall})
end

table.sort(sorted, function(a,b)
   return a.distanceFromBall < b.distanceFromBall
end)

print(sorted)

Output
image

1 Like

I’m still not sure how to incorporate this into the loop, or how it would be more efficient given that the players don’t have fixed variables. Could you please evaluate your solution and tell me how to incorporate it into the loop?

I apologize if the solution is obvious in your response; I just can’t seem to grasp it after several attempts.

Well Im guessing when a round starts you put all the players in a table correct?

So If thats true, just also add their distance from the ball

Then in a loop while the round is running update all the respective players distances and create a dummy table to sort the original table and then get the first index of the dummy table which will tell you which player is the closest!

Yeah, I get that, but why is having a dummy table more efficient than just getting the values directly, since you have to update them anyway?

In my mind, that’s just a way of removing one of the loop’s iterations.