How to find the top 3 of a roblox leaderboard

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