Module 5 · Repeating work with loops
Iterating through a simple collection
Preview arrays and visit each index-value pair.
What you will learn
- Read a simple array literal
- Distinguish an iteration's index and value
- collection
- A value that groups multiple related values.
- array
- An ordered table accessed with numeric positions.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
Coin Quest will hold several status messages and inventory items. A collection lets one loop process each value with the same behavior.
The mental model
Braces create a table; this ordered form is called an array. Each value has a numeric position beginning at one.
Generalized iteration gives both the current index and value. Tables receive a full module later.
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.
end is read as “end”: it closes a block such as a decision, loop, or function.
for is read as “for”: it starts a controlled repetition with a changing loop value.
{ } is read as “opening and closing braces”: it creates a table value around its entries.
= is read as “is assigned”: it evaluates the right side and stores that value in the name on the left.
A tiny example
--!strictlocal messages = { "Get ready", "Collect coins", "Reach the exit" }for index, message in messages do print(index, message)endLine by line
- 3
The array stores three ordered strings.
- 5
Iteration supplies each numeric index and its message value.
- 6
Both parts are printed for inspection.
- 7
end closes the repeated body.
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 array stores three ordered strings.
Line 5: Iteration supplies each numeric index and its message value.
Line 6: Both parts are printed for inspection.
Line 7: end closes the repeated body.
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: Every array value is visited once in its ordered position.
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 index belongs to Collect coins?
Try it
Mark each step after you have tried it.
Common mistake
for message in messages do print(message)endThe first iteration result is the index, not the value. Name both index and message when you need the stored text.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- Arrays group ordered values.
- Luau array positions begin at one.
- Iteration can provide both index and value.
Next: Next, you will combine loops and task.wait in a safe round countdown.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.