So I have searched over the internet for a tutorial, that shows the topmost player of the leaderboard after the round is over. But not a single website brings me satisfaction. So I thought that this website can help. Can you please teach me how to make the code? Thank you!
✓ ANSWER ACCEPTED
How to get top player of the leaderboard?
1 answer
✓ Accepted answer
this is the way I like to do it, by putting everyone's score in a table, sorting the numbers in the table from least to greatest, and checking to see whose score matches the highest score
local allScores = {} -- empty for now so that we can fill it later
for i, player in pairs(game.Players:GetPlayers()) do
table.insert(allScores, player.leaderstats.Points.Value)
end
table.sort(allScores)
-- now we have a table with all the scores in the game, sorted from least to greatest, so we can see what the highest score in the game was by finding the last value in the table
for i, player in pairs(game.Players:GetPlayers()) do
if player.leaderstats.Points.Value == allScores[#allScores] then -- #table is the last index in a table, so we can find the value at that index with table[#table]
-- this person won so do whatever code to reward them, and this will also work if people tie
end
end
This discussion is preserved as it was. New answers, comments, and votes are closed.

7 comments
Thank you! I will check when I will be free
Can I ask why you like to sort it? Or do you just have to sort it?
GetPlayers returns a table of players simply in the order that they joined the server, so without sorting the scores from least to greatest, the allScores table would be useless since the scores would be all scrambled and we wouldn't be able to know if the last value in the table is really the highest score
oh ok.
yes, one more question. Is there anyways to get the first player without using a for loop?
if you meant least points then do table[1]
because it returns the 1st argument in table