How to insert multiple variables in order in a table

Hello, so basically I am doing a racing game where I want to have a podium at the end with the top 3 fastest players.

I want to have a table that looks like this or similar to it, not sure if this is correct:

local playersThatFinished = {
       Mutrix, 15.5
       Shedlestky, 17.3
       Roblox, 19.3
       Anyname, 23.5   
       Anyname2, 33.3    ​
}

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

local playersThatWon = {
       Mutrix, 15.5
       Shedlestky, 17.3
       Roblox, 19.3
}

I do want to have the first table with all the registered times so I can do a leaderboard afterwards

Any help is really appreciated!

1 Like

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.

1 Like

That last thing works too, sorry, I am not really experienced and don’t know how it should look like.
Its the same path to get this?

PlayerTimes = {Roblox = 10, Shedletsky = 11}
1 Like

You could get the player’s time by doing this:

print(PlayerTimes.Roblox)
-- prints Roblox's time
1 Like

So basically, in dictionaries, you aren’t able to sort things like that since

local t = {
    coolIndex = 321498213489123894129834;
    coolIndex2 = 1;
}

is the same as

local t = {
    coolIndex2 = 1;
    coolIndex = 321498213489123894129834;
}

So you’d probably have to sort it like this:

local playersThatFinished = {
    [1] = {
        ['Name'] = 'Mutrix';
        ['Time'] = 15.5;
    }
-- etc
}

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
4 Likes

Before sorting out the table, how can I insert the values into the playersThatFinished table first?

Because right now it prints it empty

That’s up to you to decide but I assume you’d just subtract the current time from the initial time.

What I mean is, here in the script you did at the start there is

local playersThatFinished = {
    Mutrix = 15.5;
    Shedletsky = 17.3;
    Roblox = 19.3;
}

How can I create this table? because the sortedTable works from this right? but I currently don’t have this values in the table if that makes sense

I already have both variables, the time and the player but how can I insert them in the table?

Oh I see.

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
1 Like

It is working perfectly! thank you very much for the help!

1 Like