There’s an awesome reply that wonderfully explains how to use table.sort()
. But, I’ll do a quick demo here!
So, I shuffled the table you had, just to show how it works:
local playerTime = {['PlayerName3'] = 50, ['PlayerName1'] = 25 , ['PlayerName2'] = 40,}
table.sort(Table, Function) --table = playerTime, function = how to sort the table
By default, the function is like this (least to greatest):
local function sort(a, b)
if a < b then --the system takes two values at the time and compares them
return true --if a < b, then it returns true and puts "a" before "b"
end
end
Even though you don’t need this, this is the greatest to least function (the other way around):
local function sort(a, b)
if a > b then
return true --if a > b, then it returns true and puts "a" before "b"
end
end
Now let’s put this into action:
table.sort(playerTime) --no function needed because default is least to greatest
print(playerTime["PlayerName1"], playerTime["PlayerName2"], playerTime["PlayerName3"])
--output: 25, 40, 50 (it worked!)
I didn’t get any errors this way.
Hope that helps!