Module 7 · Tables and Luau types
Adding, finding, and removing list values
Use table helpers and handle a search that finds nothing.
What you will learn
- Use table.insert, table.find, and table.remove
- Guard a missing search result
- table library
- Built-in helpers for working with tables.
- search result
- The index returned for a found value, or nil when absent.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
An inventory list changes as Coin Quest items are collected and used. Safe helpers preserve array order.
The mental model
table.insert adds while maintaining sequence shape. table.find returns an index or nil. table.remove needs a valid index.
Guard the nil result before removal so an absent item does not become an invalid position.
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 “not equal”: it asks whether two values differ.
if is read as “if”: it starts a decision by evaluating a condition.
then is read as “then”: it marks the body that can run when the condition is truthy.
end is read as “end”: it closes a block such as a decision, loop, or function.
{ } is read as “opening and closing braces”: it creates a table value around its entries.
A tiny example
--!strictlocal items = { "Map", "Key" }table.insert(items, "Lantern")local keyIndex = table.find(items, "Key")if keyIndex ~= nil then table.remove(items, keyIndex)endprint(#items)Line by line
- 3
items begins as a two-value array.
- 4
table.insert appends Lantern.
- 6
table.find returns Key's index or nil.
- 7
The guard proves an index exists.
- 8
table.remove closes the sequence gap.
- 11
Two items remain.
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: items begins as a two-value array.
Line 4: table.insert appends Lantern.
Line 6: table.find returns Key's index or nil.
Line 7: The guard proves an index exists.
Line 8: table.remove closes the sequence gap.
Line 11: Two items remain.
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: Missing and present searches both finish without an invalid removal.
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
How many items remain?
Try it
Mark each step after you have tried it.
Common mistake
table.remove(items, table.find(items, "Compass"))table.find may return nil. Store and guard the result before passing it as an index.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- Table helpers preserve list structure.
- Searches may return nil.
- Guard before using a possibly missing index.
Next: Next, you will read a small nested profile one path at a time.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.