How to find the top 3 of a roblox leaderboard

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

2 Likes

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

Would this work?
It is returning nil for all positions in the winner’s array though. Maybe I messed something up.

    local players = game.Players:GetPlayers()
	local winners = {}
	local highest = 0
	
	for i=1, 3, 1 do
		for i, v in pairs(players) do
			if v.leaderstats.KOs.Value>highest then
				highest = v.leaderstats.KOs.Value
				table.insert(winners, v)
				table.remove(players, i)
			end
		end
	end
	print(winners[1], winners[2], winners[3])

It will say nil if you have no value in the leaderstats, if you add value then run it again it works and says their name.

One problem I can’t seem to figure out, when I’m trying to send the name somewhere else like a StringValue it wont send it says ‘nil’ do you know why this could be?

local function FindTopPlayers()
    local One = -1
    local Two = -1
    local Three = -1
    local Plr1, Plr2, Plr3 = "","","" --Gave them the default value of an empty string, so it isn't nil

    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.Name
            One = plr.leaderstats.KOs.Value
        elseif Two < plr.leaderstats.KOs.Value then
             Plr3 = Plr2
            Three = Two

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

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

FindTopPlayers()

I made it store the usernames of the players instead of the player object.
try now

Try this? Just wrote it because I had nothing better to do. It looks very intimidating but it’s fine I promise lol

Also please try to learn from this and not just copy + paste it

local getTop = function(statName:string,amount:number)
	local toSort,toReturn,total = {},{},0 -- define variables
	for _,player in pairs(players:GetPlayers()) do
		-- check and make sure each player has leaderstats & the required value
		if player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild(statName) then
			-- add this to the table we will sort next
			table.insert(toSort,{
				player = player,
				value = player.leaderstats:FindFirstChild(statName).Value
			})
		end
	end
	table.sort(toSort,function(a,b)
		-- sort the table by highest to lowest value
		return(a.value > b.value)
	end)
	for _,data in pairs(toSort) do
		-- check all the newly sorted values
		if total <= amount  then -- make sure they're in the top (whatever number you passed for amount)
            total += 1
			table.insert(toReturn,data.player)
		else 
			-- not needed to really continue any more so we'll break it
			break
		end
	end
	-- Tada! (it'll be nil if the place doesn't exist ftr)
	return toReturn
end

local top3 = getTop("KOs",3) --> stat name & amount you want to get
print(top3[1],top3[2],top3[3]) --> boom

I did fix up your script and commented on it

local function GetTop3()
	local players = game.Players:GetChildren() -- Getting the players

	local winners = {nil, nil, nil} -- Creating the winners list


	local iterValue = 3
	
	if #players < 3 then
		iterValue = #players -- For a playercount of less than 3
	end	


	local highest -- Creating the highest val variable

	for i = 1, iterValue do -- iterating for 3 (or less) players
		
		highest = 0

		for j, player in pairs(players) do -- Iterating the players
			
			if player.leaderstats.KOs.Value> highest and not table.find(winners, player) then 
			-- Checking if the player has a higher score and is not in the winners already

				highest = player.leaderstats.KOs.Value -- updating the highest score
				winners[i] = player -- updating the winner for that position

			end
			
		end

	end

	return winners -- returning the winners list
end

I called it like this and this was the output

local winners = GetTop3()

for i = 1, #winners do
	if winners[i] then
		print(winners[i], winners[i].leaderstats.KOs.Value)
	end
end

image

1 Like