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.
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?
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
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?
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)
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!