Debugging

Debugging is the process of finding bugs within the code. Both Roblox and Luau provide tools for debugging scripts. 
There are two main ways of debugging: output messages and using the debugger tool. Unlike the former, the debugger is used for examining execution line by line, inspecting variables, and evaluating expressions without the use of print functions. The debugger may only be used in Roblox Studio. In addition, Roblox Studio's script editor supports Luau's type checking and includes warnings about potential errors such as deprecated features, global variables and, if --!strict is present, all type checking errors. These warnings may be read from the Script Analysis window.
== Output ==
The print function is the most commonly used tool for debugging, which can print expressions in the output window in Studio and the Developer Console for both Studio and the client. The warn and error/assert functions also have similar usage but both vary in their function: 
*warn prints a message in yellow.
*error raises an exception, printing message in red, and ends execution rather than continuing. 
*assert only raises an exception if the evaluation of the given argument is false.
Errors, whether printed by error/assert or not, are displayed in the output window (unless if they are handled by the pcall function). Error messages are red and include a stack trace (in blue) which displays the functions that are called prior to the error.
=== Formatting output ===
*The variadic string.format function is used to format given arguments to a string, which is useful for printing values within a specific format.
*In Luau, the syntax for string interpolations uses backticks (different from quotation marks). Expressions within string interpolations are enclosed using curly brackets. For example: `2 + 2 = {2 + 2}` will evaluate to "2 + 2 = 4" and `The variable x is {x}` will evaluate to "The variable is " followed by the value of the variable x.
== Debugger ==
In Studio, breakpoints, as well as watch expressions, can also be used instead of using print functions within the code. In the script editor, breakpoints are red circles that can be placed by clicking on the right of the line number or through the context menu, and can be toggled by clicking. This can also be done while the game is running. A breakpoint is used to pause execution whenever the line is reached while the breakpoint is enabled, entering the debugger mode.
Individual breakpoints can be edited. A condition expression can be added to a breakpoint, in which the breakpoint may only activate when it evaluates to true. The condition expression may also do specific actions. A log message can also be added, which functions similar to the print function.
Within the debugger, all variables that can be accessed by the current line are recorded and can be read from the Watch window. Watch expressions are evaluated and their values as well as their data type are also recorded and can be read from the Watch window, similar to the print function. Variables and selected expressions in the script editor can be hovered over to read their value. Rendering is disabled while the debugger is active. There are three buttons used to control the debugger:
*Step Into - continue to the next line
*Step Over - skip the next line (usually for functions that do not need to be debugged)
*Step Out - exit the current function
The debugger can also be activated through errors using the Debug Errors option, which will cause errors to act similar to breakpoints. Selecting only unhandled exceptions will ignore errors handled by the pcall function.
In Studio settings, the "Command bar operates on local state at breakpoint" option can be turned on to allow running code from the command bar using the state of the breakpoint, which includes functions and variables.
== Further reading ==
*Debugging on Wikipedia
Category:Scripting