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!
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
im a beginner, only knowing enough to code simple core games, so im not the most efficient
Well they are both efficient but connections allow you to disconenct them at any time and also reconnect them so they’re pretty powerful!
i dont get it
i am filler characters so i can post
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)
Output
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.
Well the dummy table is just used to sort the original table by giving it a copy of the original, if you want to see which method is better you can try both and see what performs best!
Okay I will do some research, thank you for your help!
I’ve started a new thread about this here- Best way to handle ball mechanics to prevent lag in a mulitplayer game?, I’m afraid it’s still not completely resolved.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.