Module 7 · Tables and Luau types
Mini-project: A typed inventory
Create a typed item-quantity dictionary with focused add and query functions.
What you will learn
- Define an inventory type alias
- Add quantities through a typed function
- Query a missing item safely
- inventory
- A model of items owned and their quantities.
- exported API
- Functions or values returned for other scripts to use.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
Coin Quest needs a reliable inventory before doors, shops, and persistence can ask what a player owns.
The mental model
Use a dictionary from item names to numeric quantities. The add function owns updates; the query function turns an absent key into zero.
The ModuleScript returns only the focused API needed by callers.
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.
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.
return is read as “return”: it ends the current function call and sends a value to its caller.
{ } 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.
A colon in a declaration, such as coins: number, introduces a type annotation: the kind of value that name is allowed to hold.
A tiny example
--!strictexport type Inventory = { [string]: number }local InventoryModule = {}function InventoryModule.addItem(inventory: Inventory, itemName: string, amount: number) local current = inventory[itemName] or 0 inventory[itemName] = current + math.max(0, amount)endfunction InventoryModule.getQuantity(inventory: Inventory, itemName: string): number return inventory[itemName] or 0endreturn InventoryModuleLine by line
- 3
The exported alias maps string keys to numeric quantities.
- 5
The API table will hold focused functions.
- 7
addItem names all three input roles.
- 8
A missing item begins at zero.
- 9
Negative additions are prevented.
- 12
The query promises a number even for a missing key.
- 16
The module returns its API without running side effects.
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
Start with the statements outside function bodies. A function definition creates a callable value; its body runs when a call, event, or scheduled task reaches it.
Trace the arguments at each call and the branch or loop inside that call. Return to the caller when the function returns. For an event, use the action named in Try it to trigger the callback.
Line 3: The exported alias maps string keys to numeric quantities.
Line 5: The API table will hold focused functions.
Line 7: addItem names all three input roles.
Line 8: A missing item begins at zero.
Line 9: Negative additions are prevented.
Line 12: The query promises a number even for a missing key.
Line 16: The module returns its API without running side effects.
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 module returns predictable quantities for every disclosed case and exports no unrelated state.
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
What does getQuantity return for an absent item?
Try it
Mark each step after you have tried it.
Common mistake
Returning the internal inventory from a hidden module singleton would share state between callers. This API receives the inventory explicitly, so ownership stays with the caller.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- A typed dictionary models item quantities.
- Focused functions own updates and queries.
- A ModuleScript should return an API without surprise side effects.
Next: Module 8 connects Luau values and functions to Roblox's object hierarchy.
Studio challenge
A typed inventory
Checks are formative feedback, not cheat-proof certification. You can finish this lesson without the plugin.
- Target
- ReplicatedStorage/LearningLuau/Inventory
- Check mode
- source and output
Challenge state: idle
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.