Alright, so I created my first custom leaderboard, but I cannot figure out how to list the leaderboard from highest level players to lowest level players, I only currently have a code that only lists the players who join but they do not list from highest to lowest.
If you can help me here’s the code down below:
for i, v in pairs(players:GetChildren()) do
local ape = namething:Clone()
ape.Parent = list
ape.Name = v.Name
ape.Text = v.Name
ape.Position = position
position = position + UDim2.new(0, 0, 0.024, 0)
spawn(function()
wait(3)
ape.Level.Text = --(hidden the value, don't wanna reveal it)
end)
end
table.sort is a function which receives a table and then orders it according to the parameter function you provide. In this case the function makes it so that p1 will be ordered higher in the table if p1.Score > p2.Score.
Essentially table.sort is a function that sorts a table. The default is that it sorts the table by making the lowest value tab[1] and the highest value tab[#tab] (the last value in the table). If you specify a function as a parameter of the table.sort function then you can make it sort based on the contents of that function.
For example:
tab = {5,2,3,1,4}
table.sort(tab)
print(tab)
-- prints 1,2,3,4,5
So as I said, if you make a function the second parameter of the table.sort function it sorts the table based on that function. The parameter function itself takes two parameters, valueA and valueB which are members of the table you’re sorting.
tab will be sorted based on the score value of the players. This will allow you to have a table to loop through which has the players with the highest score first and the lowest score last.
i.e if p1.Score > p2.Score returns true then p1 will be higher in the order than p2.
That’s not the problem, problem is sorting out the players by greatest to lowest level
for i, v in pairs(players:GetChildren()) do
local ape = namething:Clone()
ape.Parent = list
ape.Name = v.Name
ape.Text = v.Name
ape.Position = position
position = position + UDim2.new(0, 0, 0.024, 0)
end
players = game:GetService('Players'):GetPlayers()
table.sort(players, function(Player1, Player2)
return Player1.LevelStuff.Level.Value > Player.LevelStuff.Level.Value
end)
for i, v in pairs(players) do
local ape = namething:Clone()
ape.Parent = list
ape.Name = v.Name
ape.Text = v.Name
ape.Position = position
position = position + UDim2.new(0, 0, 0.024, 0)
end