[?] How would I go about listing custom leaderboard by levels?

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.

image

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

Appreciate it

1 Like

You could do a table of the players and their scores and then do a table.sort.

tab = players:GetChildren()

table.sort(tab, function(p1,p2) return p1.Score > p2.Score end)

for _, v in pairs(tab) do
   -- the rest is the same
3 Likes

Could you please explain the code you just wrote, just for educational purposes I don’t fully understand it

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.

table.sort is part of the Lua library:
https://www.lua.org/pil/19.3.html

Another example could be player distances from an object:

table.sort(tab, function(char1, char2) return (char1.HumanoidRootPart.Position - Object.Position).Magnitude <  (char2.HumanoidRootPart.Position - Object.Position).Magnitude end)
2 Likes

My bad but I still don’t understand it, maybe in easy words?

Could you specify what you don’t understand?

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
1 Like

I do understand table.sort but the function you brought up earlier didn’t really ring the bell for me with the pt.score

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.

In the code I suggested:

tab = players:GetChildren()

table.sort(tab, function(p1,p2) return p1.Score > p2.Score end)

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.

1 Like

hmm, well i tried it on my code but it didn’t sort it out

(hold on let me try something)

yeah no it didn’t sort it out :confused:

image

Show the code you are currently trying.

Show the code… Also use coroutine.wrap instead of spawn

2 Likes

It’s the same code, I just added the table.sort line with my values upon when player has joined

I could be wrong aswell,

Yes, but how do you expect us to help if we can’t decide what’s the problem.

1 Like

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

I already explained the problem, sorting out the players by greatest to lowest level

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

Fixed.

1 Like

???
image

Putting an incomplete if statement

Isn’t this the same code I used

image

I don’t know. You didn’t show it. Can you please test it now?

1 Like