Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to get top player of the leaderboard?

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!

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

7 comments

0

Thank you! I will check when I will be free

0

Can I ask why you like to sort it? Or do you just have to sort it?

0

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

0

oh ok.

0

yes, one more question. Is there anyways to get the first player without using a for loop?

0

if you meant least points then do table[1]

0

because it returns the 1st argument in table

This discussion is preserved as it was. New answers, comments, and votes are closed.