Errors

List of errors

Attempt to call a nil value

Description: calling a function that doesn't exist
Example:
1
printtt("abc") -- printtt is not a function
2
​
3
test() -- calling before it's defined, same error
4
​
5
function test() end
6
​
7
test() -- works
Copied!
​

Unexpected symbol near 'something'

Description: an invalid character is placed next to a value/keyword/variable/function call
1
function abc()) end -- unexpected symbol near ')'
2
​
3
local a = 5
4
a l= 15 -- unexpected symbol near 'l'
5
​
6
local a = 5] -- unexpected symbol near ']'
Copied!
​

Attempt to index global 'variable' (a nil value)

Description: indexing via [key] or .key for a variable that doesn't exist
Example:
1
abc.x = 5 -- abc is nil, error
2
​
3
abc = {} -- abc defined here
4
​
5
abc.x = 5 -- works
6
​
7
xyz['x'] = 'abc' -- xyz is nil, error
Copied!
​

Attempt to perform arithmetic on a nil value

Description: performing arithmetic (*, /, -, +, %, ^) on a nil value
Example:
1
print(xyz + 5) -- error, xyz not defined
2
a = a + 5 -- error, a not defined
Copied!
​

Attempt to perform arithmetic on field '?' (a nil value)

Description: performing arithmetic (*, /, -, +, %, ^) on a nil value
Example:
1
local t = {}
2
t.x = t.x + 5
Copied!
​

Attempt to compare nil with <TYPE>

Description: using a comparison operator (<, >, ~=, ==, >=, <=) in which one side is a nil value
Example:
1
local x = 5
2
print(x < y) -- y is not defined
Copied!
​

Malformed number near <NUMBER>

Description: the number has an invalid character next to it
Example:
1
print(12345aaa) -- aaa makes it a malformed number
Copied!
​

Unfinished capture | Malformed pattern

Description: the pattern is missing a closing ), or a closing ]
Example:
1
print(string.match('ABC', '(')) -- unfinished capture
2
print(string.match('ABC', '[')) -- malformed pattern
Copied!
​

Interpreting errors

When you get an error, Lua provides a stack trace showing you where it originates from, where the error occurs, the function calls that led to the error, and the line number of where it occurred.
A general error will look like this:
1
file_location:LINE NUMBER: error message
2
stack traceback:
3
file_location:LINE NUMBER: in <local/function> 'FUNC'
4
file_location:LINE NUMBER: in main chunk
5
[C]: in ?
Copied!
Here's an example:
1
C:\Users\user\lua_file.lua:5: attempt to perform arithmetic on a nil value (field 'x')
2
stack traceback:
3
C:\Users\user\lua_file.lua:5: in local 'c'
4
C:\Users\user\lua_file.lua:7: in local 'b'
5
C:\Users\user\lua_file.lua:9: in function 'a'
6
C:\Users\user\lua_file.lua:12: in main chunk
7
[C]: in ?
Copied!
The code that resulted in this error is:
1
function a()
2
local function b()
3
local function c()
4
local t = {}
5
t.x = t.x + 5
6
end
7
c()
8
end
9
b()
10
end
11
​
12
a()
Copied!
Here you can see the line number 5 after the file location, which tells us where the exact line with the code that resulted in the error. The stack traceback shows the functions that were called that led up to that.
First, function a is called at line 12, then function b is called at line 9 inside of a, then c is called at line 7 inside of function b, and finally at line 5, the error occurs inside of function c.
A note to add is that comments can offset the line number and make it appear as the error is in a line that doesn't exist or doesn't look like an error,
Last modified 2yr ago