How to find the top 3 of a roblox leaderboard

I want to find the top 3 players on the leaderboard so I can give them rewards.
How do I go about doing that.
image

2 Likes

I think you need to create a global leaderboard. Search in YouTube for tutorial or in devforum.

2 Likes

You mean a datastore? I don’t want to store them globally. Only temporary per match and then give each of the “winners” a reward. Datastores are real sketch sometimes and sometimes don’t work.

1 Like

So you want the players to be given a reward after the match?

2 Likes

Yes. From the roblox ingame leaderboard.

2 Likes

You can do so by using tables. You would have to store the players in a table and iterate through the table until you find the player with the highest stat. Repeat this two more times and you’d have the top 3 players.

3 Likes

I’m not really a programmer, but can you create a table with awards and a function to give a prize to a player?

2 Likes

Alright I will try this and tell you if it works. :+1:

2 Likes

Thank you so much right now! :pray:

2 Likes

How would I know when iterating if the player is the highest stat and then sort them into 3 winners with the highest stats.

create a variable set to 0, and update it whenever a player has a higher stat than the variable. By the end of the loop you should have the player with the highest stat

1 Like

How would I get all 3 though and not just the highest.

You would repeat this 3 times to store the highest players in a table, basically a nested loop

Can’t you put them all in a table, and call table.sort(tableOfLeaderboard, function(a, b) return a > b end)?

You can create a dictionary, creating new key,values for every player iteration {“Player” = Stat}, then create an Array and iterate over your dictionary with a variable finding the highest stat and when you find the highest stat link it to the player with it an remove that player from the dictionary and add their name to the array and repeat that 3 times.
There is probably an easier method but this is how I would do it

You can’t sort a dictionary, so that wouldn’t work

That’s not what I said, read again.

whatever, my bad. I’m already writing a script for the player anyway so ehhh.

Also you can sort a dictionary just requires extra steps (if all values are numbers)

local function FindTopPlayers()
    local One = -1
    local Two = -1
    local Three = -1
    local Plr1, Plr2, Plr3

    for _, plr in ipairs(game.Players:GetPlayers()) do
        
        if One < plr.leaderstats.KOs.Value then
            Plr3 = Plr2
            Three = Two

            Plr2 = Plr1
            Two = One

            Plr1 = plr
            One = plr.leaderstats.KOs.Value
        elseif Two < plr.leaderstats.KOs.Value then
             Plr3 = Plr2
            Three = Two

            Plr2 = plr
            Two = plr.leaderstats.KOs.Value
        elseif Three < plr.leaderstats.KOs.Value then
            Plr3 = plr
            Three = plr.leaderstats.KOs.Value
        end
    end

   --Now you have 3 values for the top three players: Plr1, Plr2, Plr3
end

FindTopPlayers()
2 Likes