AR/Debugging

From Multi Theft Auto: Wiki

Debug console

MTA features a built-in debug console that shows debug messages output from MTA functions or from scripts. You can open it by typing debugscript x in console, while x is the debug level:

  • 1: only errors
  • 2: errors and warnings
  • 3: errors, warnings and info messages

Thus, by typing debugscript 3 all messages are visible, that or level 2 are recommended for most occasions. You should have debugscript enabled most of the time you are testing your scripts, this will help you detect typos or other simple issues and solve them easily.

Example

This example snippet has two errors:

if (getPlayerName(player) == "Fedor")
	outputChatbox("Hello Fedor")
end

When the script this piece of code is in is tried to be loaded, debugscript will output something similiar to this:

INFO: Loading script failed: C:\<server path>\mods\deathmatch\resources\myResource\script.lua:15: 'then' expected near ´outputChatbox'

This means the script could not be parsed, because there was a syntax error. It shows the path of the script, so you can also see what resource it is in ('myResource' in this case) and of course the name of the script. After the filename it shows the line number and again after that what was wrong. Easy to solve now, we just forgot the 'then' keyword:

if (getPlayerName(player) == "Fedor") then
	outputChatbox("Hello Fedor")
end

Now the script will load fine and won't output any errors, until a player with the name 'Fedor' enters this section of the script. Then, debugscript will output:

ERROR: C:\<server path>\mods\deathmatch\resources\d\script.lua:15: attempt to call global 'outputChatbox' (a nil value)

This means the called function does not exist, which can be easily explained since the functions' name is outputChatBox (with a capital B):

if (getPlayerName(player) == "Fedor") then
	outputChatBox("Hello Fedor")
end

This is of course just an example, there are plenty of other messages and scenarios, but you should get the idea.