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

How to find the player with the highest score, the second highest score, and so on?

Asked by 7 years ago
Edited 7 years ago

How to find the player with the highest score, the second highest score, and so on? I tried this random thing, but it obviously does not work.

for _, player in pairs(game.Players:GetPlayers()) do
        player.PlayerGui.ScreenGui.Frame.Visible = true
        local first = math.max(player.leaderstats.Score.Value)
        local second = math.min(player.leaderstats.Score.Value)
        player.PlayerGui.ScreenGui.Frame.First.Text = first
        player.PlayerGui.ScreenGui.Frame.Second.Text = second
    end

Thanks!

2 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago

The easiest way would be to sort the players by score using table.sort.

function higherScoreThan(playerA, playerB)
    return playerA.leaderstats.Score.Value > playerB.leaderstats.Score.Value
end

-- get the list of all the players playing the game that have a Score
local players = {}
for _, player in pairs(game.Players:GetPlayers()) do
    if player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Score") then
        table.insert(players, player)
    end
end

table.sort(players, higherScoreThan)

-- display the names
if players[1] then
    FirstPlace.Text = players[1].Name
    if players[2] then
        SecondPlayer.Text = players[2].Name
    else
        SecondPlayer.Text = "only one person was playing :("
    end
else
    FirstPlace.Text = "no one was playing :("
end

0
Ad
Log in to vote
0
Answered by 7 years ago

Use Dummiez Leader Board Block. Here is the link; https://www.roblox.com/library/157979393/High-Score-Board

You can easily configure it to a gui.

Answer this question