Tables, lesson 16
Arrays
Goal: Keep an ordered list of values.
about 5 min4 practice exercises
A table holds many values in one variable. The simplest kind is an array: an ordered list.
local inventory = { "Sword", "Shield", "Potion" }
print(inventory[1]) -- Sword
print(#inventory) -- 3
Luau counts from 1, not 0. inventory[1] is the first item, and #inventory is how many there are, so inventory[#inventory] is the last one.
Change the list with table.insert and table.remove:
table.insert(inventory, "Key") -- adds to the end
table.remove(inventory, 1) -- removes "Sword"; the rest move up
print(inventory[1]) -- Shield
print(#inventory) -- 3
Asking for a position that does not exist gives nil, not an error: inventory[10] is nil.
Sources
Checked against Roblox's documentation on . The lesson is in our own words; these pages are the source of truth.