Help with finding player with most of a stat

Basically I need help finding the player with the most of a Stat, since I have no clue how to do this. I’m trying to make this system where it will change their team if they have the most, can anyone help me?

How is the code layout?

I don’t have code because I don’t know how to write this. I have never done it before.

Iterate theough all the stats and store the highest value in a variable, then check to see if the current value is greater than the highest.

You would add players in a table like this

``````{
geometricalC2123 = 9999,
noob = 999
}
``````

And then math.max selects the most stats, so you would do

``````math.max(table.unpack(urtable))
``````

Might not work as there’s arrays, if it dosent then I have no idea.

Or, another way, you could use a loop with the highest variable

``````local s = {
geometricalC2123 = 9999,
noob = 999
}
local highest,plrhighest = 0,"no one"
for i,v in pairs(s) do
if (v > highest) then
highest = v
plrhighest = i
end
end
print(plrhighest,highest) -- geometricalC2123 9999``````
2 Likes

Would this work if instead of putting names in the local s = could I do,
`local s = game.Players:GetPlayers().leaderstats`

1 Like

No, you will have to manually insert these into the table. Sorry for that 15 day reply

Thats ok I found out how to do it after some research.