First, let's take a look at a basic list;
local myTable = {"Hello", "Hola", "Bonjour"}
This table holds three values. But how do we access these values? We do it in the same way you access your house, with a key. Remember, the key is not what you want. You want the house. They key is just how you get there.
But how do you know what key to use? For basic lists, like the one above, the keys are simple numbers. We can select the correct number just by counting.
local myTable = {
"Hello",
"Hola",
"Bonjour"
}
To use a key, just put the table's name, follow by brackets with the key inside. ( table[key] ). This will give you the value associated with that key.
print( myTable[1] )
print( myTable[2] )
print( myTable[3] )
In a dictionary, we can change the key to what we want it to be. We do this by putting the key in brackets, followed by an equal sign;
local myTable = {
["English"] = "Hello",
["Spanish"] = "Hola",
["French"] = "Bonjour"
}
There's also a short cut that's pretty nice. If the name of the key is a string, and is a valid Lua identifier, meaning it could theoretically also be used as the name of a variable, we can omit the brackets and quotes. Since all of our keys fit in to this category, we can do it.
local myTable = {
English = "Hello",
Spanish = "Hola",
French = "Bonjour"
}
We can use these keys, and access their values, the same way we used the numbers, with brackets and the key. But this time, we don't even need to count, since we wrote what we wanted the key to be.
print( myTable["English"] )
print( myTable["Spanish"] )
print( myTable["French"] )
Again, there's a very nice short cut we can use. If our key could be used a variable, we can access its value with a dot, just like workspace.Part
print( myTable.English )
print( myTable.Spanish )
print( myTable.French )
Comment with any questions.
4 comments
Let's say in a dictionary, there was a key with (still not used to proper words for this) ["Credit"] = false. How would you check "Credit"'s boolean value, in an example?
if dictionary["Credit"] then, or equivalently,if dictionary.Credit then. This answer explains it pretty well, read it!I did. Thank you so very much, the two of you! Upvoted and accepted, I've been waiting so long for someone to answer this and now finally someone has!
I will use this knowledge to create an awesome server suite (not admin) that is highly customizable (for the complete fun of it for me). Mainly focusing on datastores. Once again, thank you!