Sorting players based on KOs

hey guys im a wee bit confused with how to do this. I’m trying to get all players and sort them by their KOs in leaderstats. I know about table.sort, but how would I make it traceable back to the player who got the score? Like I could use

table.insert(player.leaderstats.KOs.Value)

but then how would I find who’s player the score belonged to?

You can insert arrays for the players in one table, holding a reference to their client object and the amount of KOs they have, then sort the amount of KOs in the table:

local tbl = {}

for _, player in pairs(game.Players:GetPlayers()) do
    tbl[#tbl + 1] = {
        Player = player;
        Knockouts = player.leaderstats.KOs.Value
    }
end

table.sort(tbl, function(a, b)
    return a.Knockouts > b.Knockouts
end)

local mostKOs_Info = tbl[1]
print(mostKOs_Info.Player, mostKOs_Info.Knockouts)
3 Likes