I have both variables, the player and the time they took to finish the race, defined as player and playerTime. How can I insert this values in the table for it to look like above with both values and in order?
After that If you can give me hand, I want to have a new table with the Top 3, taking the first 3 values or players from the playersThatFinished table
I think the easiest way would be to insert the player’s names first and then add their times. You could add them in order by doing:
for _, v in pairs(timetable) do
pos = _ + 1
table.insert(playertime, pos)
end
You could probably get the player time by referencing the player’s name which would be v and having a value which is tied to something called/containing v.
I’m not sure why you don’t want to do something like this though:
PlayerTimes = {Roblox = 10, Shedletsky = 11}
edit:
You would also have to store the player’s names as strings if they aren’t instances already.
which can automatically be sorted using insertion sort.
local playersThatFinished = {
Mutrix = 15.5;
Shedletsky = 17.3;
Roblox = 19.3;
}
-- etc
local sortedTable = {}
for name,time in pairs(playersThatFinished) do
local hasInserted = false
for index,info in ipairs(sortedTable) do
if time < info.Time then
table.insert(sortedTable, index, {Name = name, Time = time})
hasInserted = true
break -- super important as not adding break will cause it to exhaust execution limit
end
end
if not hasInserted then
sortedTable[#sortedTable + 1] = {Name = name, Time = time})
end
end
You would just insert into a blank table with the index that is the player’s name and the value is the player’s time.
local playersThatFinished = {}
for i,player in pairs(game:GetService('Players'):GetPlayers()) do
playersThatFinished[player.Name] = player.Time.Value
end