Help organizing a table with two values from highest to lowest?

I am trying to display a folder of intvalues on guis to form a leaderboard.

all of the intvalues are in a folder inside serverscriptservice.

this script gets all of the int values and prints them out as a table.

the table has to have the name of the intvalues as well as the number value it carries.
I have the list I need, but I cant figure out how to organize them from highest to lowest.
here is my script:

local mytable = {}



function maketable()
local list = game.ServerScriptService.LeaderStats.List:GetChildren()
for index, IntValue in pairs(list)do
    mytable[IntValue.Name] = IntValue.Value -- add info to table
end

for playerName, value in pairs(mytable)do
    print(playerName, " ", value)
end 

end





while true do
	wait(3.1)
	maketable()
end
1 Like

instead of using:

for index, IntValue in pairs(list) do
–something
IntValue
end

use

for index = 1, IntValue #list do
–something
IntValue[index]
– the index is the number of the value
end

You can use table.sort:

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

a and b are elements in the list. The provided function evaluates all elements until the array is sorted (returns true for all elements going through the list once).

I tried using that but I couldnt figure out how to integrate it into my script, could you perhaps show me?

The code I provided should work perfectly if you put it at the end of your maketable function.

after adding the code it didnt change the arrangment of the values. heres the output:
Capturethingexample

there are two intvalues with a player name and a number that goes with it.
when I print both of them the first time both have 0, I thought if I increased “mantorok4866” to 4,
my name would be printed first then “player1”. but they remained in the same position.
does the output not show the table being organized?
here is the script I corrected:

local mytable = {}



function maketable()
local list = game.ServerScriptService.LeaderStats.List:GetChildren()
for i = 1, #list do
    mytable[list[i].Name] = list[i].Value -- add info to table
end

for playerName, value in pairs(mytable)do
    print(playerName, " ", value)
end 


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

end





while true do
	wait(3.1)
	maketable()
end

Try this, looks like you just need to add a slight edit to your code:

That’s odd… maybe instead of using that, put a “local count = 0” before the loop and add one for every time it loops, like this:

local list = game.ServerScriptService.LeaderStats.List:GetChildren()
local count = 0
for i = 1, #list do
count = count + 1
mytable[list[i].Name] = count – add info to table
end

It’s a bit more messy but it usually works for me. Also can you show me how to add the cool code effect to replies? I’m new here.

You can use the button that looks similar to this: </>.
You can also add three backticks before and after the code, like this:
```
code
```

1 Like

you can change code into what you see in my replies by putting three of these ``` above and below your code

--code here
2 Likes
local mytable = {}
local sortedTable = {}



function maketable()
local list = game.ServerScriptService.LeaderStats.List:GetChildren()
for index, IntValue in pairs(list)do
    mytable[IntValue.Name] = IntValue.Value -- add info to table
end


for q = 1, #mytable do
	local smallestSpot = 1
	for i = 2, #mytable do
		if mytable[i] < mytable[smallestSpot] then
			smallestSpot = i
		end
	end
	sortedTable[#sortedTable+1]=mytable[smallestSpot]
	mytable[smallestSpot] = math.huge
end
mytable = sortedTable


for playerName, value in pairs(mytable)do
    print(playerName, " ", value)
end 

end





while true do
	wait(3.1)
	maketable()
end
1 Like

I don’t think you can sort dictionaries, it needs to be in the form of an array.

I think im just gonna use the default leaderstats roblox offers, I dont know why making a custom leaderboard has to be so difficult.

I don’t think it’s that difficult.

In the case of a custom leaderboard, the only thing you’re probably going to be sorting is by a certain stat, not by names. You can just take a UIListLayout, slap it into a scrolling frame, change the SortOrder to LayoutOrder and make the LayoutOrder of each player frame the value of their first stat. This is almost exactly what the CoreGui leaderboard does, except it has other cases accounted for and uses table sorting rather than a list layout.

If you just want to reference the code behind the CoreGui player list and make your own custom leaderboard based off of that code, you can do that as well. Here is the script for the PlayerList. I’ve taken the liberty to highlight the main list sorting function: salvaging the rest of the code or searching for what you need here is up to you.

1 Like