Alright so, you’ll need to iterate through GetPlayers of Players service and add each player’s stat value to an array.
Then using table.sort you can sort it, no need to give a sorting function - the default function will order them acoordingly for you.
Putting that into practice:
--// Dependencies
local Players = game:GetService("Players")
--// Variables
local Array = {}
for _, player in ipairs(Players:GetPlayers()) do
table.insert(Array, player.Coins.Value)
end
table.sort(Array)
--// Now do what you'd like
If you’d like to maybe insert a dictionary into the array then you’ll be required to provide your own sorting function.
The sorting function must return a boolean if the second parameter is bigger than the other.
Now, that’d look like:
for _, player in ipairs(Players:GetPlayers()) do
table.insert(Array, {
Coins = player.Coins.Value
Player = player
})
end
table.sort(Array, function(a, b)
return a.Coins < b.Coins
end)
table.sort basically sorts a table in the order of the first entry / element to the last entry based on a function where it should return true if the first entry / element should come before the second. Here’s an example:
local tbl = {59, 42, 1, 4, 68, 32}
-- this sorts the table in the order of lowest to highest
-- alternatively, you can just leave the comp function blank because lua automatically sorts it from lowest to highest, but I did it just to show how to use the function
table.sort(tbl, function(a, b)
return a < b
end)
In your case, you can get all of the player’s coin’s values and put them in a table, along with the player’s name for easy referencing:
local TBL = {}
for _,v in pairs(game.Players:GetPlayers()) do
TBL[#tbl+1] = {v; v.Coins.Value}
end
table.sort(TBL)
print(unpack(TBL))