Lesson 4 of 6
Debugging basics: Output, stack traces and breakpoints
Debug Luau scripts in Roblox Studio. Use print, warn and error, read Output and stack traces, and pause Beacon Yard with breakpoints, Watch, Call Stack and stepping.
By the end you can
- Choose between print, warn and error, and predict how each changes Output and script execution
- Read an Output line and stack trace to find the script, the line and the caller that passed a bad value
- Pause Beacon Yard in Studio with a breakpoint and inspect Watch, Call Stack and stepping controls
Before this: Events, signals and the task library
1Three ways to talk to Output
At m02-the-pad, Beacon Yard printed every touch on Pad1 with print. One milestone later, DataService.get refuses a missing player profile with error:
function DataService.get(player: Player): Profile
local profile = profiles[player]
if not profile then
error("No profile for " .. player.Name, 2)
end
Luau and Roblox give you three calls that write to Studio's Output window:
print(...)writes a plain line and keeps running1. Use it for temporary checks.warn(...)writes a yellow warning and keeps running2. Use it for recoverable problems, such as a missing optional setting.error(message, level)writes a red error, prints a stack traceStack traceThe list of active function calls printed under a Luau error, from the failing line back to what called it.Think of it as the chain of doors the smoke came through to reach the alarm., and stops the current thread1. Passing2aslevelpoints the line number at the caller instead of inside the helper.
Run this cell to watch print, warn and error behave side by side. Notice whether the final line runs.
local function takeCoins(balance, cost)
print("Checking cost:", cost)
if balance == nil then
error("Missing balance", 2)
end
if cost > balance then
warn("Not enough coins:", balance)
return balance
end
return balance - cost
end
print("Result 1:", takeCoins(10, 25))
print("Result 2:", takeCoins(nil, 5))
print("This line never runs")
Match each Beacon Yard situation to the Output call that fits best.
Answer
Confirming the server script booted during m02: print; Noting that an optional asset id is still 0 while the game keeps running: warn; Stopping immediately because DataService.get was given a player with no profile: error. print logs normal progress, warn flags a recoverable issue in yellow without stopping, and error halts the current thread in red.
Open this step in lesson mode · Link to this step · This confused me
2Reading Output and a stack trace
Studio's Output window opens from the Window menu or the Script tab toolbar3. Its top bar lets you filter by message type (Error, Warning, Information), by context (Server or Client), or by search text, and you can clear old noise with Ctrl+K on Windows or Cmd+K on macOS3.
When error fires, Output prints the red message followed by a stack traceStack traceThe list of active function calls printed under a Luau error, from the failing line back to what called it.Think of it as the chain of doors the smoke came through to reach the alarm.: Stack Begin, each active call from innermost to outermost, and Stack End.
Think of the fire alarm panel by a building's front door. The red line names the room (script), zone (line number) and problem (message); the stack trace lists the doors the call passed through to get there.
Think of it as The panel by a building's front door, which names the room and the zone and lists the doors the smoke came through
Both sidesRemember: An error message is the panel by a building's front door, which names the room and the zone and lists the doors the smoke came through.
DataService.get calls error("No profile for " .. player.Name, 2). What does the second argument, 2, change in Output?
Answer
It points the error line at the script that called DataService.get, instead of inside DataService.get. Passing level 2 blames the caller that passed an unknown player, so the red line sends you straight to the broken call site.
Open this step in lesson mode · Link to this step · This confused me
3Where the alarm picture breaks
A smoke alarm rings where the smoke hits the sensor, not where the toaster caught fire. A Luau error is the same: it fires on the line that tries to use nil, even if an earlier line created that nil. Try to answer before opening the breakdown.
Where does it break?“The panel by a building's front door, which names the room and the zone and lists the doors the smoke came through” is a good picture of an error message. Where does it stop being true?
It breaks here. A smoke detector sounds close to the fire, but an error sounds at the line that used a bad value, which can be far from the line that made it, and the trace lists only the calls still running.
The true model. The virtual machine raises an error at the instruction that fails, and the stack trace lists the active calls at that moment, innermost first. The cause can be any earlier code that produced the bad value.
Run this Beacon Yard coin helper. Read the error line, find where the bad value actually entered, and fix the bug so both tests pass.
local COIN_VALUE = 5
local function awardCoins(profile, pickups)
profile.coins += pickups * COIN_VALUE
return profile.coins
end
local profiles = {
Ada = { coins = 10 },
}
-- Fix the bug below so Ada's balance updates to 25.
local playerProfile = profiles["ada"]
print(awardCoins(playerProfile, 3))
In the broken starter cell above, which line throws the error, and which line caused it?
local function awardCoins(profile, pickups) -- line 3
profile.coins += pickups * COIN_VALUE -- line 4
end
local playerProfile = profiles["ada"] -- line 13
awardCoins(playerProfile, 3) -- line 14
Answer
Line 4 throws attempt to index nil with 'coins', because line 13 looked up "ada" instead of "Ada". Table keys are case-sensitive. Line 13 quietly returns nil, and the crash only happens on line 4 when awardCoins reads profile.coins.
Open this step in lesson mode · Link to this step · This confused me
4Pause the script with a breakpoint
Prints tell you what happened after the fact. A BreakpointBreakpointA mark on a line of code that pauses the script when that line is about to run.Think of it as a pause button glued to one line. pauses the script right before a line runs so you can inspect live state while the game is frozen4.
In Studio's Script Editor, click in the gutter just to the right of a line number to insert a breakpoint (a red circle appears)4. When Play reaches that line, execution pauses and a yellow arrow marks the line about to run. Open Watch and Call Stack from the Script tab toolbar or Window > Debug:
- Watch shows every local variable in scope under Variables, and lets you type custom expressions under My Watches4.
- Call StackCall StackThe Studio debugger window showing every active function call while paused at a breakpoint.Think of it as a live breadcrumb trail of who called whom to reach the paused line. lists every active call from the current line back to the event or script that started it4.
- Studio's mezzanine stepping controls move one line at a time: Step Over (
F10), Step Into (F11), Step Out (Shift+F11), and Resume Scripts4.

Select a numbered pin, or a row below, to read what it marks.
A breakpoint paused in CoinService.luau: red circle and yellow arrow in the gutter, Watch inspecting amount, and Call Stack showing who triggered it.
Match each Studio debugger control to what it does while paused.
Answer
Step Over (F10): Run the current line without entering functions it calls; Step Into (F11): Move inside the function called on the current line; Step Out (Shift+F11): Finish the current function and pause back in its caller; Resume Scripts: Keep running normally until the next breakpoint. Step Over runs one line at the current level, Step Into descends into a called function, Step Out returns to the caller, and Resume Scripts continues normal play.
Open this step in lesson mode · Link to this step · This confused me
5Pause CoinService in Studio
Other ways pros do this
| Option | What it is | When pros pick it | Trade-offs |
|---|---|---|---|
| print and warn | Writes values to Output without pausing. | Quick checks, timing-sensitive loops, or messages you also want in live logs. | Requires editing the script; easy to forget and commit noise. |
| Studio breakpoints | Pauses the script on a line so you inspect Watch and Call Stack. | Tracing a branch, nil value or unexpected argument during Play. | Pausing changes timing, so race conditions can behave differently while paused. |
| error and assert | Fails fast with a stack trace when an assumption breaks. | Catching invalid state early, like DataService.get checking for a missing profile. | Halts the current thread unless caught with pcall. |
Build along
src/server/CoinService.luausrc/server/DataService.luau
Check out
m03-coins-count, openCoinServicein Studio, click the gutter next toDataService.addCoins(player, amount), press Play, and walk onto a coin pad.You should see Play pauses with a yellow arrow on DataService.addCoins(player, amount). Watch shows amount as 5, and Call Stack shows the Touched handler.
Press Step Into (
F11) to enterDataService.addCoins, press Step Over (F10) untilprofile.coins += amountruns, inspectprofile.coinsin Watch, then click Resume Scripts.You should see The yellow arrow jumps inside DataService.addCoins, steps past profile.coins += amount, and Resume Scripts returns you to Play with 5 coins in leaderstats.
Play is paused on DataService.addCoins(player, amount) in CoinService.luau. You want to watch profile.coins change line by line inside addCoins. Which button do you press?
Answer
Step Into (F11). Step Into (F11) enters DataService.addCoins so you can step through its body; Step Over (F10) would run all of addCoins in one jump.
Open this step in lesson mode · Link to this step · This confused me
6Challenge and recap
Level 1, Apprentice
Challenge
Fix the broken lookup in the fix-award cell above so both tests pass.
- Start from
- The game at milestone m03 (download)
- Check it here
- Run the code cell in the lesson view; the challenge completes when it passes. No Studio needed.
Done means
- Read the red error line in the fix-award cell and trace it back to the lookup key
- Fix the lookup so profiles.Ada.coins reaches 25
- Both assertions in the cell pass
Hints
- The error points at line 4, where profile.coins is read, because profile is nil.
- Check the capitalization of the key used to look up Ada in profiles.
Solution
local playerProfile = profiles["Ada"]
CoinService.luau runs inside ServerScriptService. Which context filter in Studio's Output window shows its print and warn lines?
Answer
Server. CoinService is a server module required by the server script, so its Output messages are tagged with the Server context.
Retiring the analogy
You no longer need “the panel by a building's front door, which names the room and the zone and lists the doors the smoke came through”. The virtual machine raises an error at the instruction that fails, and the stack trace lists the active calls at that moment, innermost first. The cause can be any earlier code that produced the bad value.
Recap
- print writes plain info, warn writes filterable yellow warnings, and error halts the current thread with a stack trace.
- Read the red error line for script, line and message, then trace backwards to where the bad value was created.
- Breakpoints pause Play on a line so Watch, Call Stack and F10/F11/Shift+F11 stepping let you inspect live values without guessing.
An error message is the panel by a building's front door, which names the room and the zone and lists the doors the smoke came through, until a smoke detector sounds close to the fire, but an error sounds at the line that used a bad value, which can be far from the line that made it, and the trace lists only the calls still running.
Queued for review
- Match each Beacon Yard situation to the Output call that fits best.
- DataService.get calls error("No profile for " .. player.Name, 2). What does the second argument, 2, change in Output?
- In the broken starter cell above, which line throws the error, and which line caused it?
- Play is paused on DataService.addCoins(player, amount) in CoinService.luau. You want to watch profile.coins change line by line inside addCoins. Which button do you press?
Open this step in lesson mode · Link to this step · This confused me