List values in one box, count them, and pull values out by number — your first data structure.
So far every box holds one thing. But a game is full of lists: the players in the server, the items in a shop, the enemies on a wave. Luau's answer is the table — one box that holds many things.
Curly braces make an empty table, or one with values already inside:
local empty = {}
local powerups = { "double", "shield", "speed" }
Each value sits in a numbered slot, starting at 1. To read one, use square brackets:
print(powerups[1]) --> double
print(powerups[2]) --> shield
print(powerups[3]) --> speed
Luau counts from 1, not 0. That trips up people coming from other languages — powerups[0] is nothing here.
local fruits = { 'apple', 'banana' } — what does fruits[2] give you?
A table can grow after you make it:
local powerups = { "double" }
table.insert(powerups, "shield")
table.insert(powerups, "speed")
print(#powerups) --> 3 (# is "how many slots")
table.insert appends a new value to the end. The # operator answers "how many things are in this table?" — you'll use it constantly.
Make a table named inventory and add the string 'sword' to it with table.insert.
-- build your inventory
When you need to look at every slot, loop with ipairs:
for index, value in ipairs(powerups) do
print(index .. ": " .. value)
end
Each trip through the loop gives you the slot number and its value. This is how you give every player a reward, check every enemy, or count every item.
local coins = { 5, 10, 15 }
local total = 0
for _, amount in ipairs(coins) do
total = total + amount
end
print(total) --> 30
The underscore _ just means "I don't need the index." This pattern — start a total, add every value, print the total — is the sum pattern you'll reuse forever.
local words = { "one", "two" }
for i, w in ipairs(words) do
print(i .. ": " .. w)
end
fruits[0] is nothing.ipairs needs a table. for i, v in ipairs(singleValue) errors — ipairs walks a table.powerups[99] gives nil, not an error. Check with # first.Next: pausing and scheduling with task.