Skip to content
Debugging Lua Scripts
Garry's Mod

Debugging Lua Scripts

Master Lua debugging in Garry's Mod. Learn to use print statements, the error console, and code comments to fix syntax, runtime, and logic errors.

By ···10 min read·Multi-source verified
1 reading this guide  

Master Lua debugging in Garry's Mod. Learn to use print statements, the error console, and code comments to fix syntax, runtime, and logic errors.

13.9. Debugging Lua Scripts

Lua scripting is the backbone of many Garry's Mod addons, enabling custom functionality and complex behaviors. When your scripts don't work as intended, effective debugging is essential to identify and fix errors, ensuring your addon functions flawlessly.

Writing Lua scripts for Garry's Mod can be incredibly rewarding, allowing you to extend the game in countless ways. However, even experienced scripters encounter bugs. Debugging is the process of finding and fixing these errors. This section will equip you with the knowledge and techniques to effectively debug your Lua scripts, saving you time and frustration.

Common Lua Errors:

  • Syntax Errors: Typos, missing punctuation (like semicolons or commas), or incorrect keyword usage. These usually prevent the script from running at all.
  • Runtime Errors: Errors that occur while the script is executing, such as trying to access a non-existent variable, calling a function with incorrect arguments, or dividing by zero.
  • Logic Errors: The script runs without crashing, but it doesn't produce the intended results. This is often the most challenging type of error to find.

Debugging Techniques:

  1. Print Statements (`print()`): This is the most basic and often most effective debugging tool. Insert `print()` statements at various points in your code to output variable values, check if a certain code block is being reached, or track the flow of execution.
  2. Error Console: Garry's Mod has an in-game console that displays error messages. Pay close attention to these messages, as they often provide clues about the type of error and the line number where it occurred.
  3. Code Comments (`--`): Temporarily comment out sections of your code using `--` to isolate the problematic part. Uncomment lines or blocks one by one to pinpoint the source of the error.
  4. Breakpoints (Advanced): While Garry's Mod doesn't have built-in visual breakpoints like some IDEs, you can simulate them with `print()` statements or by using more advanced debugging tools if you're using an external editor with Lua support.
  5. Rubber Duck Debugging: Explain your code, line by line, to an inanimate object (like a rubber duck). The act of explaining often helps you spot logical flaws.
  6. Version Control (Git): For larger projects, using a version control system like Git allows you to track changes and revert to previous working versions if a new change introduces bugs.

Example: Debugging a Simple Function

Suppose you have a function that's supposed to add two numbers but returns an incorrect result.


function AddNumbers(a, b)
 -- print("a is: " .. tostring(a))
 -- print("b is: " .. tostring(b))
 local result = a + b
 -- print("Result is: " .. tostring(result))
 return result
end

local num1 = 5
local num2 = "10" -- Oops! This is a string.

local sum = AddNumbers(num1, num2)
print("The sum is: " .. sum)

By uncommenting the `print` statements, you'd quickly see that `num2` is a string, causing the addition to fail. You would then correct `local num2 = 10`.

Effective debugging is a skill that improves with practice. By employing these techniques, you'll become much more proficient at identifying and resolving issues in your Garry's Mod Lua scripts.

100% Human-Written. AI Fact-Checked. Community Verified. Learn how AntMag verifies content