Compare one time to a bunch of other times?

I am currently working on a speedrun system that saves your lowest time.
At the moment, I am trying to make a script that compares your time to the times of other players to see if your time is the lowest. The problem is, I’m not really sure how I would go about doing this.
This is what the current setup for storing times looks like:

image

Any help would be much appreciated!

You can use table.sort to sort all the speedrun times you have, least to greatest. Then you can just pick the first time in the list, which will be the shortest one!

I’ve never actually used tables before, how can I translate the times into a table?

Well, something like this might work.

local player -- fill in the blank with a Player object
local Times = {}
for _, time in pairs(game.ServerStorage.RunData[player.Name]:GetChildren()) do
	table.insert(Times, time) -- Inserts the time instance in the Times list
end

table.sort(Times, function(a, b)
	-- a and b are time instances
	return a.Value < b.Value -- assuming they're NumberValues
end)

print("Shortest:", Times[1] and Times[1].Value or "None")```
1 Like

I found tables to be extremely challenging when first used them, so beware of the learning curve.

A table is essentially a container.
In other programmatic containers, a table could be formatted as an array, a dictionary, or tuple, triple, quadruple, a hashset, a hashmap, a vector, fortunately in lua a table can be any of these containers and more, a table could be an entire game.
However, it’s generally the same: a container, or a table, stores data.

Array is a container whose data is indexed by numbers
Dictionary is a container whose data is indexed by a key
A tuple is a two-set container that stores only two-types of data
A triple is a three-set container that stores only three-types of data
A quadruple… etc
A hashset or hashmap is rather ancient version of a dictionary.
A vector is basically an array with better optimizations and interaction.
Containers are very important in programming.

The syntax is as follows:
TableName = {} --// this is an empty table
Or, as an array: TableName = {
[1] = data,
[2] = data,
[3] = data
}
Or, as a dictionary: TableName = {
[“KeyName”] = data,
[“KeyName”] = data,
[“KeyName”] = data
}
And I believe the above can be simplified to: TableName = {
KeyName = data,
KeyName = data,
KeyName = data
}

Or, you don’t like that type of syntax than you may write:
TableName[KeyName] = data
As long as you’re assigning the data than lua automatically creates another area in that table containing the data, however, if you try to access a non-existent area it’ll throw an error.

In lua, a table can contain other tables, which may then contain other tables, which then could contain more other tables, and really, until you run out of RAM could contain as many tables as you can imagine.
So, to do something like that, you would write:

TableName = {
     NestedTableName_1 = {
          NestedTableName_2 = {
          }
     }
}

Then you could access the nested tables by accessing the TableName[] operator,
ie: TableName[NestedTableName_1] = “Bob”, “Susan”
What’s important to note is the difference of how to access the data stored in a table depends on how that data is stored. If it’s stored outside of a table, like above, than [1] would be “Bob” [2] would be “Susan”

When written like TableName[NestedTableName_1] = { Man = “Bob”, Woman = “Susan”}
Then you could use the dot operator on TableName[NestedTableName_1] to access the ‘dictionary’ key Man or Woman, like so TableName[NestedTableName_1].Man would retrieve “Bob” or TableName[NestedTableName_1].Woman would retrieve “Susan”

Alternatively, you could access the key directly via TableName[NestedTableName_1][“Man”] as long as it exists it’ll retrieve the data “Bob” but if it doesn’t exist it’ll throw an error

So, long story, the syntax is as follows:
TableName[NestedTableName_1][Data_Or_KeyName]

A table may store any primitive data such as number, string, etc, or any complex object such as Player or Model, or in your case
Example of a complex table:

TableName = {
     "Bob", 
     "Susan", 
     Men = {
         TheBuilder = "bob, the builder", 
         TheOtherBuilder = "jim, the builder"         
    },
    OtherMen = {
        "otherbob, the builder", "otherjim, the builder"
    },
} 
Wherein TableName[1] == "Bob"
TableName[2] == "Susan"
TableName["Men"][1] == "bob, the builder"
TableName["Men"].TheBuilder == "bob, the builder"
TableName["Men"]["TheOtherBuilder"] == "jim, the builder"
TableName["Men"].TheOtherBuilder == "jim, the builder"
TableName["OtherMen"][1] == "otherbob, the builder"
TableName["OtherMen"][2] == "otherjim, the builder"
3 Likes