Lua Error Explanation
m (moved Lua error explanation to Lua Error Explanation: Format yo) |
(Added a basic explanation about what Lua errors are) |
||
Line 1: | Line 1: | ||
− | == Attempt to call a nil value == | + | = What Are Lua Errors? = |
+ | A Lua error is caused when the code that is being ran is improper. There are many reasons for why a Lua error might occur, but understanding what a Lua error is and how to read it is an important skill that any developer needs to have. | ||
+ | |||
+ | = Lua Error Format = | ||
+ | The first line of the Lua error contains 3 important pieces of information: | ||
+ | * The path to the file that is causing the error | ||
+ | * The line that is causing the error | ||
+ | * The error itself | ||
+ | |||
+ | Here is an example of a code that will cause a Lua error: | ||
+ | <pre>local text = "Hello World" | ||
+ | Print( text )</pre> | ||
+ | The code will produce the following error: | ||
+ | <pre>[ERROR]addons/my_addon/lua/autorun/server/sv_my_addon_autorun.lua:2: attempt to call global 'Print' (a nil value) | ||
+ | 1. unknown - addons/my_addon/lua/autorun/server/sv_my_addon_autorun.lua:2</pre> | ||
+ | |||
+ | That is because '''Print''' is not an existing function ('''print''', however, does exist). | ||
+ | |||
+ | The first line includes the path to the file that is causing the error - '''addons/my_addon/lua/autorun/server/sv_my_addon_autorun.lua''' | ||
+ | |||
+ | Afterwards, the line that's producing the error - '''sv_my_addon_autorun.lua:2''' (Line 2) | ||
+ | |||
+ | Lastly, the error itself - '''attempt to call global 'Print' (a nil value)''' | ||
+ | |||
+ | Below the error, we have the trace of the function. Simplified - If the error is inside a function that is called somewhere else, it will state where the function is called from. | ||
+ | = Common Errors = | ||
+ | |||
+ | == Attempt to call global '?' a nil value == | ||
'''Description:''' You tried to call a function that doesn't exist. | '''Description:''' You tried to call a function that doesn't exist. | ||
'''Possible causes:''' | '''Possible causes:''' | ||
− | * Your function might be defined in another | + | * Your function might be defined in another Lua state. (e.g Calling a function on the client that only exists on the * server.) |
* You're using a metafunction on the wrong kind of object. (e.g. Calling :SteamID() on a Vector) | * You're using a metafunction on the wrong kind of object. (e.g. Calling :SteamID() on a Vector) | ||
* The function you're calling has an error in it which means it is not defined. | * The function you're calling has an error in it which means it is not defined. |
Revision as of 14:04, 4 July 2017
What Are Lua Errors?
A Lua error is caused when the code that is being ran is improper. There are many reasons for why a Lua error might occur, but understanding what a Lua error is and how to read it is an important skill that any developer needs to have.
Lua Error Format
The first line of the Lua error contains 3 important pieces of information:
- The path to the file that is causing the error
- The line that is causing the error
- The error itself
Here is an example of a code that will cause a Lua error:
local text = "Hello World" Print( text )
The code will produce the following error:
[ERROR]addons/my_addon/lua/autorun/server/sv_my_addon_autorun.lua:2: attempt to call global 'Print' (a nil value) 1. unknown - addons/my_addon/lua/autorun/server/sv_my_addon_autorun.lua:2
That is because Print is not an existing function (print, however, does exist).
The first line includes the path to the file that is causing the error - addons/my_addon/lua/autorun/server/sv_my_addon_autorun.lua
Afterwards, the line that's producing the error - sv_my_addon_autorun.lua:2 (Line 2)
Lastly, the error itself - attempt to call global 'Print' (a nil value)
Below the error, we have the trace of the function. Simplified - If the error is inside a function that is called somewhere else, it will state where the function is called from.
Common Errors
Attempt to call global '?' a nil value
Description: You tried to call a function that doesn't exist.
Possible causes:
- Your function might be defined in another Lua state. (e.g Calling a function on the client that only exists on the * server.)
- You're using a metafunction on the wrong kind of object. (e.g. Calling :SteamID() on a Vector)
- The function you're calling has an error in it which means it is not defined.
- You've misspelled the name of the function.
Ways to fix:
- Make sure the function exists
- Make sure your function is defined in the correct realm
- Check your function calls for spelling errors
Attempt to perform arithmetic on global '?' (a nil value)
Description: You tried to perform arithmetic (+, -, *, /) on a global variable that is not defined.
Possible causes:
- You tried to use a local variable that was defined later in the code
- You've misspelled the name of the global variable
Ways to fix:
- Make sure you define local variables before calling them in the code
- Check for spelling errors
Attempt to perform arithmetic on '?' (a type value)
Description: You tried to perform arithmetic (+, -, *, /) on a variable that cannot perform arithmetic. (e.g. 2 + "some string")
Attempt to index global 'varname' (a nil value)
Description: You tried to index an undefined variable (e.g. print( variable.index ) where variable is undefined)
Possible causes:
- The variable is defined in a different realm
- The variable is local and defined later in the code
- You've misspelled the name of the variable
Ways to fix:
- Make sure the variable is only accessed in the realm it was defined in
- If the variable is local, define it before accessing it
Malformed number near 'number'
Description: There is a malformed number in the code (e.g. 1.2.3, 2f)
Possible causes:
- An IP address was written as a number instead of a string
- Incorrect writing of multiplication of a number and a variable
Ways to fix:
- Store IP addresses as a string
- Multiply variables with numbers by using the * operator
Unexpected symbol near 'symbol'
Description: You typed a symbol in the code that Lua didn't know how to interpret.
Possible causes:
- Incorrect syntax (e.g. Forgot to write "then" after an if statement)
- Not closing brackets and parenthesis at the correct locations
Ways to fix:
- Make sure there are no mistypes in the code
- Close brackets and parenthesis correctly (See: Code Indentation)
'symbol1' expected near 'symbol2'
Description: Lua expected symbol1 instead of symbol2
Possible causes:
- Not closing all brackets and parenthesis before the end of the file
- Wrong operator calling (e.g. "==" instead of "=")
Ways to Fix
- Close brackets and parenthesis correctly (See: Code Indentation)
- Use the correct operators