Determine 1st, 2nd, 3rd, ect based on values sorting system?

Hello, I’m currently making a custom leaderboard and want to show the players coming 1st, 2nd, 3rd, 4th, 5th, ect.
So what I have so far is every player has a value inside of them with their elimination count. It updates correctly and everything, I just need a way to sort and compare those values (so if the player has the largest value compared to all of the other player’s values then set their gui text to be 1st.)
Note: I have read other people’s topic’s on similar things but they answers never run people through what the script is actually doing and how to customize it to fit with your script.
If you know of any sorting algorithm I can implement or other methods, please let me know and run me through it. Thankyou.

i think you should try table.sort

use sortedasync with ordered data store

I’m going to answer this given the assumption that this is for a datastoring system. For most cases OrderedDatastores will work, however it can be annoying considering that :GetSortedAsync() returns paginated lists, and you can’t actually extract which player is at “n” index in the datastore. Workarounds for this specific case where you would like to find for example, the person who is at position 1000 efficiently are usually pretty tedious. For this you would want to rely on some kind of caching system, or finding different ways to store the data.

If you would like to learn more about caching I have an article here

Thankyou all for your reply, however I am not looking to use datastore in anyway as the values do not save, nor do I want them to.

But how would I link that to a player and also determine what is 1st, 2nd, ect?

you would have to include a custom sort function like this

local Table = {{"Bob", 50}, {"Jack", 100}}
table.sort(Table, function()
    return a[2] > b[2]
end)
print(Table[1][1]) -- Jack

note i did not test this yet

I see, ok I’ll see what I can do. Thankyou.

Ok, I’m getting there I just need to know how to find (v)'s position in the table:

for i,v in pairs(Table) do

end

why would you need to use the pairs() function

when I do this:

print(Table)

After sorting it, it just prints jibberish.

that’s intended behavior (30 cars)

v’s position in the table is just “i”.

1 Like

Ok, how would I find the player with the same value as ‘v’ because what I’ve done is put all player values into a table and then sorted it and now I need to match all the 'v’s with their player’s that hold the same value inside of them (which is of course a numbervalue)

I would suggest using a dictionary and using that to sort the values. This is because once you sort the values, there may be 2 players who have the same value. But in your case I guess you can just loop through game.Players:GetPlayers(), get the number value, and match that value with the value in your table.

Edit: If it is a scrolling frame and order doesn’t matter in your code, you can just use layout order to order the player values.

local sortTable = {}
for _,i in pairs(game.Players:GetPlayers()) do
	table.insert(sortTable,{Name = i.Name, Score = i:GetAttribute("EliminationCount") or 0})	
end

local resultTable = <<Some Sorting Function (like BubbleSort)>> (sortTable)

for n = 1,10 do
	print(resultTable[n].Name," : ",resultTable[n].Score)
end

Isn’t getattribute a colon instead of a dot?

Wait, I think I’ve figured it out,
All I need to do is something like this:

local mytab = {
	{"Test1",450};
	{"Test2",56};
	{"Test3",78};
}



table.sort(mytab, function(a,b)
	return a[2] > b[2]
end)


for i,v in pairs(mytab) do
	print(v[1], v[2])
end

except, how do I insert a value like this: {“Test1”,450} into a table using table.insert?

yeah, has typos, maybe i fixed them all

you would do

table.insert(Table, {"Test1", 450})
1 Like