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
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).
after adding the code it didnt change the arrangment of the values. heres the output:
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
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.
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
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.