Module 12 · Modules, persistence, and the capstone
Debugging, testing, and profiling
Read errors from the first useful frame, isolate variables, test multiplayer, and measure before optimizing.
What you will learn
- Read an Output stack trace
- Isolate a bug with a small reproduction
- Profile a named section before optimizing
- stack trace
- A list of calls showing where an error traveled through code.
- profile
- Measure where runtime time or resources are spent.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
The final Coin Quest prototype combines many systems. A repeatable debugging process prevents random changes from hiding the actual cause.
The mental model
Read the error message and first relevant project frame. Reproduce with the smallest inputs, inspect boundaries, and change one factor at a time.
Use multiplayer Studio testing for networking and profile a named region before optimizing code that merely looks expensive.
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 “plus equals”: it reads the current value, adds, then stores the result back.
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.
else is read as “else”: it provides the fallback branch.
end is read as “end”: it closes a block such as a decision, loop, or function.
A tiny example
--!strictlocal function totalCoinValue(values: { number }): number debug.profilebegin("CoinQuest.TotalCoinValue") local total = 0 for _, value in values do total += value end debug.profileend() return totalendlocal ok, result = pcall(totalCoinValue, { 1, 2, 3 })if not ok then warn("Coin total test failed")else print(result)endLine by line
- 3
The calculation has a small typed boundary.
- 4
A named profile region begins immediately around measured work.
- 6
The loop is the behavior being isolated.
- 9
The profile region ends on the normal path.
- 13
pcall contains this deliberate test boundary.
- 15
Learner-facing output is safe and concise rather than a raw stack trace.
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 calculation has a small typed boundary.
Line 4: A named profile region begins immediately around measured work.
Line 6: The loop is the behavior being isolated.
Line 9: The profile region ends on the normal path.
Line 13: pcall contains this deliberate test boundary.
Line 15: Learner-facing output is safe and concise rather than a raw stack trace.
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: You can reproduce one issue, identify its boundary, and explain what evidence supports the fix.
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 should you inspect first after an error?
Try it
Mark each step after you have tried it.
Common mistake
Optimizing before measuring may complicate code without improving the actual bottleneck. Correctness and a reproducible case come first.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- Read errors from relevant frames.
- Isolate one variable at a time.
- Profile before optimizing and test networking with multiple clients.
Next: Next, you will assemble the complete Coin Quest vertical slice and verify its public behaviors.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.