Basic Lua debugging in Garry's Mod
When working with Garry's Mod, a good deal of the time you'll be working with Lua scripts which can be rather particular about how to do things. This serves as a basic guide to help you solve some issues with scripts that don't work.
Console errors
Errors in the console almost always have the file and line number where the error happened, and if the server has simplerr installed (it comes with DarkRP and a few other mods) the error will also have a few hints regarding what may have gone wrong, the hints are also usually ordered in most likely to least likely
Below is a sample error message that you might see in the console, which a breakdown of the information it gives you:
The section outlined in red, is the name of the file that the error occurred in, in this case modificationloader.lua
The section outlined in green is the line number inside of that file where the error occurred. in this case 137
The section outlined in blue is the file path where the file can be found, which in this case in /gamemodes/darkrp/gamemode/libraries/
(which is actually found in the /garrysmod/
folder in the main directory, so the full file path is /garrysmod/gamemodes/darkrp/gamemode/libraries/
.
There will not be any hints if the exact cause is unknown, in which case the game may output The best help I can give you is this:
before the error. This is only true for servers that have Simplerr installed, such as with DarkRP.
As an example:
In addition to the console, Lua errors experienced by a player are put into the /garrysmod/clientside_errors.txt
file.
Sample error messages
Couldn't include file 'script.lua' (File not found) (Lua file)
The Lua file tried to import another lua file, but couldn't find it. Make sure the addon the lua file is from was installed correctly, and that any required addons are also installed.
file.lua:1: attempt to call global 'varName' (a nil value)
An addon or Lua script (in this case file.lua) tried to call a variable or function (in this case varName) that hasn't been given a value yet (in this case, the call happened at line 1). Usually caused by one of the following:
Another addon or Lua script is supposed to be on the server and isn't
Another addon or Lua script failed to load correctly
The addon or Lua script is for an older version of the game, and no longer works
file.lua:4: attempt to index local 'varTable' (a nil value)
The Lua script (in this case file.lua
) tried to access a table that doesn't exist. Check the file for when that table was made and see what went wrong (there's usually going to be another error that caused this one that happened earlier)
For more information and example error messages, you can look here: https://wiki.garrysmod.com/page/Lua_Error_Explanation
Some helpful tips to avoid errors
'1' equals 1
will return false, but in any other circumstance they would be interchangeable.When working with
and
,or
, ornot
,false
andnil
are 'falsey' values, and everything else is 'truthy'and
andor
can be a little complicated:a and b
returnsa
ifa
isfalse
ornil
, andb
otherwisea or b
returnsa
unless a isfalse
ornil
, in which case it returnsb
Most other issues you'll encounter will be with variables, you can read about them here: https://wiki.garrysmod.com/page/Beginner_Tutorial_Variables
Naming conventions
Global variables are in ALL_CAPITAL_LETTERS (as well as local variables that are always a copy of a global variable):
THE_QUICK_BROWN_FOX_JUMPS_OVER_THE_LAZY_DOG
Global functions and Object properties are in PascalCase, where every word has it's first letter capitalized and there are no spaces:
TheQuickBrownFoxJumpsOverTheLazyDog
local variables, local functions, and Object functions are in camelCase, which is almost the same as pascal case except the first word is all lowercase. Most of the time, these will also only be a single word:
theQuickBrownFoxJumpsOverTheLazyDog
Array indexes are usually snake_case (all lowercase, underscores instead of spaces), completelylowercase (all lowercase, no spaces), or Title Case (most words start with a capital, and uses spaces.)
(snake_case)
the_quick_brown_fox_jumps_over_the_lazy_dog
(completelylowercase)
thequickbrownfoxjumpsoverthelazydog
(Try to keep these to only one or two words long)(Title Case)
The Quick Brown Fox jumps over the Lazy Dog
There are exceptions to these, but these are what you can generally expect to see used.
For further reading, see the related article linked at the bottom for a basic introduction to programming in gLua.