Multiplayer, saving and debugging, lesson 31
Debugging
Goal: Find out why code does not do what you meant.
about 7 min4 practice exercises
Debugging is finding out why code does not do what you meant. It is a skill, and it follows a routine.
1. Read the error. Output tells you the script, the line and the problem. The three you will meet most:
| Message | Usual cause |
|---|---|
attempt to index nil with 'Name' | The thing before the dot is nil: a wrong path or a failed FindFirstChild |
attempt to call a nil value | A misspelled function name |
attempt to perform arithmetic (add) on nil and number | A variable you thought held a number is nil |
2. If there is no error, print. Code that runs without errors but does the wrong thing needs you to look inside it:
coin.Touched:Connect(function(hit)
print("Touched by", hit.Name, "inside", hit.Parent.Name)
local player = Players:GetPlayerFromCharacter(hit.Parent)
print("Player found:", player)
end)
If the first line never prints, the event is not connected or the script is not running. If it prints but the player is nil, the problem is on that line. Each print halves the area the bug can hide in.
3. Check where the script runs. A LocalScript in ServerScriptService, or a Script in ReplicatedStorage, never runs at all. No error, no output.
4. Change one thing at a time, and test after each change.
Studio also has breakpoints: click next to a line number and the script pauses there, so you can look at every variable. And in a live game, the Developer Console (F9) shows the same output as the Output window.
You have finished the course. You can read and write the Luau that most Roblox games are made of. The best next step is to build something small of your own and get stuck on it.
Try it in Studio
Optional, and the best way to make it stick.
Click in the margin next to a line number to set a breakpoint, press Play, and hover over variables when the script pauses.
Sources
Checked against Roblox's documentation on . The lesson is in our own words; these pages are the source of truth.