Module 7 · Tables and Luau types
Arrays: an ordered list
Store related values at consecutive numeric positions.
What you will learn
- Create and index an array
- Use length only with a sequence-shaped array
- array
- A table used as an ordered sequence with consecutive numeric indices.
- index
- A position used to access a table value.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
Coin Quest can keep a route of checkpoint names in order rather than maintaining a separate local for every stop.
The mental model
Braces create a table. In an array, the first value is at index 1, the second at 2, and so on.
The # length operator is dependable for a sequence without nil holes. Do not treat it as a general count for sparse tables.
New words and symbols
--!strict asks Studio to check your code for type mistakes. It is an instruction for the editor and produces no Output message.
Matching quotes such as "hello" surround a string: a piece of text. The quotation marks tell Luau where the text starts and ends.
print(...) calls the built-in print function. Put the values to show inside its parentheses; separate multiple values with commas. Read the result in Studio's Output panel.
local is read as “local”: it creates a name that is available only in the current code scope.
{ } is read as “opening and closing braces”: it creates a table value around its entries.
[ ] is read as “opening and closing brackets”: it selects a table entry with an index or key.
= is read as “is assigned”: it evaluates the right side and stores that value in the name on the left.
A tiny example
--!strictlocal checkpoints = { "Meadow", "Bridge", "Tower" }local firstCheckpoint = checkpoints[1]local routeLength = #checkpointsprint(firstCheckpoint, routeLength)Line by line
- 3
The braces hold three ordered string values.
- 4
Index 1 reads the first array value.
- 5
# counts this consecutive sequence.
- 7
The selected value and sequence length are printed.
Read the code aloud
Read one statement at a time: name the value being read, what the operation does, and where its result goes. Keep identifiers exactly as written so you can find them again in the code.
Pause at calls, conditions, and loops. Explain when the body runs before moving to the next statement; use the line notes above when a symbol is unfamiliar.
Execution trace
Begin at the first executable statement. Comments and blank lines do not run; a branch chooses which block runs, and a loop may revisit its body.
Line 3: The braces hold three ordered string values.
Line 4: Index 1 reads the first array value.
Line 5: # counts this consecutive sequence.
Line 7: The selected value and sequence length are printed.
State before and after
Before running, identify the starting values and any existing Studio objects this example needs. List what you expect to change.
After your practice change, check this result: The array remains an ordered sequence with predictable indices and length.
Reading or printing a value leaves it unchanged. Look for assignments, calls that change state, and property updates to explain each change you observe.
Predict before running
Which value is checkpoints[2]?
Try it
Mark each step after you have tried it.
Common mistake
local firstCheckpoint = checkpoints[0]Luau arrays conventionally begin at index 1. Index zero has no value here.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- Arrays are ordered tables.
- Indices conventionally begin at one.
- Length assumes sequence-shaped data.
Next: Next, you will look up values by meaningful named keys.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.