Understanding Tables in Luau
In Luau, the language used for Roblox scripting, tables are fundamental data structures. A table is a collection of data stored in pairs, where each pair consists of:
A key: a unique identifier.
A value: data associated with the key.
The table itself is wrapped in braces {}:
local myFirstTable = {
-- Table is empty
}
Entries in a table
How does an entry look? Well, quite plainly, it looks like this: ``` Each entry in a table looks like this:[key] = value
Entries are separated by commas, and keys must be unique, though values can repeat.
local myFirstTable = {
[key1] = value,
[key2] = value,
[key3] = value,
}
Keys and Strings
If a key is a string, you should wrap it in quotation marks. However, there's a shortcut:- If the string is a single word without spaces or periods, you can omit the brackets and quotation marks.
Keys can also be other data types, such as numbers or objects. Here’s an example:
local myFirstTable = {
['workspace'] = value , -- Here, workspace is a string 'workspace'
workspace = value, -- This would overwrite our previous entry, because these are the same key.
[workspace] = value -- Here, workspace actually references the workspace object.
}
Accessing/modifying values
So how can we get the value associated with a specific key? Well, we simply add the key in brackets after the table. Using this method, we can also change the value at a specific key.local myFirstTable = {
['workspace'] = value , -- Here, workspace is a string 'workspace'
}
-- myFirstTable[key]
print(myFirstTable['workspace']) -- value
myFirstTable['workspace'] = otherValue
print(myFirstTable['workspace']) -- otherValue
If no value exists at key, this will return nil. If we want to remove a value we can set it to nil.
local myFirstTable = {
['workspace'] = value
}
myFirstTable['workspace'] = nil
print(myFirstTable['workspace']) -- nil
Arrays
A special type of table where the keys are sequential numbers starting from 1 is called an array. The keys in an array are referred to as indices (or singular as index).
local myFirstArray = {
[1] = value,
[2] = value,
[3] = value,
}
Every other table is called a dictionary.
The special thing about Arrays is that we can add elements to the end, remove elements from specific index (and it’ll automatically update the indices to fix the order).
local myFirstArray = {
[1] = value1,
[2] = value2,
[3] = value3,
}
table.insert(myFirstArray, value4)
print(myFirstArray) -- {
[1] = value1,
[2] = value2,
[3] = value3,
[4] = value4
} - Edit
table.remove(myFirstArray, 2)
print(myFirstArray) -- {
[1] = value1,
[2] = value3,
[3] = value4
} - Edit
Iteration
You can iterate over a table, accessing each key-value pair or index-value pair.
Generic Iteration
for key, value in myTable do -- Most practical way of iterating over a table.
print(key, value)
end
Dictionaries
For dictionaries (tables with arbitrary keys), it’s common to use k and v as shorthand for “key” and “value”:
for k, v in pairs(myDictionary) do -- The standard way of iterating over a dictionary
print(k, v)
end
Arrays
For arrays, it’s common to use i and v for “index” and “value”:
for i, v in ipairs(myArray) do -- The standard way of iterating over an array
print(i, v)
end
Error: attempt to index nil with ___
This error occurs when you try to index a variable that is nil. Since nil is not a table, attempting to look up keys in it will fail.
local myTable = nil
print(myTable[key]) -- Error: attempt to index nil with 'key'
Ensure that your variable is properly initialized as a table before accessing it.