Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to get top player of the leaderboard?

Asked by
Xyternal 247 Moderation Voter
3 years ago

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

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

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
0
Thank you! I will check when I will be free Xyternal 247 — 3y
0
Can I ask why you like to sort it? Or do you just have to sort it? Xyternal 247 — 3y
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 OfficerBrah 494 — 3y
0
oh ok. Xyternal 247 — 3y
View all comments (3 more)
0
yes, one more question. Is there anyways to get the first player without using a for loop? Xyternal 247 — 3y
0
if you meant least points then do table[1] Xapelize 2658 — 3y
0
because it returns the 1st argument in table Xapelize 2658 — 3y
Ad

Answer this question