Forum

> > CS2D > Scripts > [GUIDE] Lua errors meanings
Forums overviewCS2D overview Scripts overviewLog in to reply

English [GUIDE] Lua errors meanings

16 replies
To the start Previous 1 Next To the start

old [GUIDE] Lua errors meanings

Dousea
User Off Offline

Quote
Are you having trouble to understand the error that Lua (CS2D console) gives to you? Fear not because user Dousea is here for you! This thread is going to explain what errors you'd expect while scripting. Note that maybe not all errors have been identified by me so please.. contact if you find anything. Enough with the useless chit-chat, let's go to the fun part.

Just a note, use CTRL+F to find your error here.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

General Syntax
These are the errors that are commonly given for general syntax error. Keep in mind that you either forgot or accidentally inserted a word or a symbol that doesn't match with Lua's syntax.

Quote
'X' expected near 'Y'

X
: a word or a symbol.
Y
: a word or a symbol.

Lua is expecting
X
near
Y
.
if (not a)
	a = {true} -- 'then' expected near 'a'
end

print(a][1]) -- ')' expected near ']'


Quote
'X' expected (to close 'Z' at line W) near 'Y'

W
: line that contains
Z
.
X
: a word or a symbol.
Y
: a word or a symbol.
Z
: a word or a symbol.

Lua is expecting
X
near
Y
to close
Z
at line
W
.
if (true) then
	print("it's true!") -- 'end' expected (to close 'if' at line 1) near '<eof>'


Quote
unexpected symbol near 'X'

X
: a word or a symbol.

Lua is not expecting any symbol near
X
.
print("a")) -- unexpected symbol near ')'


Quote
malformed number near 'X'

X
: a word or a symbol.

There's malformed number near
X
. You either try to concatenate a literal number with strings without brackets or accidentally inserted a word or a symbol after a number.
print(1.."st") -- malformed number near '1..'


Quote
unfinished string near 'X'

X
: a word or a symbol.

You forgot to finish a string near
X
.
print("I forgot to close this string) -- unfinished string near '<eof>'


▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Arithmetic
Arithmetic operation is a weakness for some scripters. These are the errors for arithmetic operations failures. Keep in mind that Lua provides automatic conversion for strings to numbers whenever a number is expected. Arithmetic only can be performed with numbers, and strings with following condition: only numerical constant inside the string, no other char.

Quote
attempt to perform arithmetic on Y 'X' (a Z value)

X
: variable that is attempted to be performed with arithmetic.
Y
: either
local
,
global
, or
field
, based on
X
.
Z
: either
nil
,
boolean
,
number
,
string
,
function
,
userdata
,
thread
, or
table
, based on
X
.

You're trying to perform arithmetic on
X
, while
X
is a
Z
value that couldn't perform any arithmetic.
a = {}
b = "b"

print(a - 0) -- attempt to perform arithmetic on global 'a' (a table value)
print(4 / b) -- attempt to perform arithmetic on global 'b' (a string value)


Quote
attempt to perform arithmetic on a X value

X
: either
nil
,
boolean
,
number
,
string
,
function
,
userdata
,
thread
, or
table
.

You're trying to perform arithmetic on a literal or table value that couldn't perform any arithmetic.
a = {}
a[10] = "string"

print(a[10] - 1) -- attempt to perform arithmetic on a string value
print("1" % {}) -- attempt to perform arithmetic on a table value


▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Concatenation
These are the errors that given when a concatenation error occured. Concatenation is only for strings and numbers. Keep in mind that Lua automatically converts numbers to strings whenever a string expected. Therefore, string concatenation accepts numbers besides strings.

Quote
attempt to concatenate Y 'X' (a Z value)

X
: variable that is attempted to be concatenated.
Y
: either
local
,
global
, or
field
, based on
X
.
Z
: either
nil
,
boolean
,
number
,
string
,
function
,
userdata
,
thread
, or
table
, based on
X
.

You're trying to concatenate
X
, while
X
is a
Z
value that is unconcatenatable.
a = {}

print(a .. " is a table") -- attempt to concatenate global 'a' (a table value)
print("a.b = " .. a.b) -- attempt to concatenate field 'b' (a nil value)


Quote
attempt to concatenate a X value

X
: either
nil
,
boolean
,
number
,
string
,
function
,
userdata
,
thread
, or
table
.

You're trying to concatenate a literal or table value that is unconcatenatable.
a = {}

print("Can I concatenate " .. true .. "?") -- attempt to concatenate a boolean value
print("a[1] = " .. a[1]) -- attempt to concatenate a nil value


▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Length Operator
Errors of these are given because you're messing with length operator that's denoted by
#
tag. Keep in mind that values that have length are strings and tables. The length of a string is its number of bytes (each character is one byte), while the length of a table is only permitted if the table is a sequence, a set of its keys are all numeric and equal to {1..n} where n is a non-negative integer (n is its length).

Quote
attempt to get length of Y 'X' (a Z value)

X
: variable that is attempted to get its length.
Y
: either
local
,
global
, or
field
, based on
X
.
Z
: either
nil
,
boolean
,
number
,
string
,
function
,
userdata
,
thread
, or
table
, based on
X
.

You're trying to get the length of
X
, while
X
is a
Z
value that doesn't have any sort of length.
print(#a) -- attempt to get length of global 'a' (a nil value)


Quote
attempt to get length of a X value

X
: either
nil
,
boolean
,
number
,
string
,
function
,
userdata
,
thread
, or
table
.

You're trying to get length of a literal or table value that doesn't have any sort of length.
a = {}
a[97] = false

print(#nil) -- attempt to get length of a nil value
print(#a[1]) -- attempt to get length of a boolean value


▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Table Index
These are the errors for table indexing. Keep in mind that all variables can act as keys in tables, but only two literal values can act as ones, number and string.

Quote
attempt to index Y 'X' (a Z value)

X
: variable that is attempted to be indexed.
Y
: either
local
,
global
, or
field
, based on
X
.
Z
: either
nil
,
boolean
,
number
,
string
,
function
,
userdata
,
thread
, or
table
, based on
X
.

You're trying to index
X
, while
X
is a
Z
value that couldn't be indexed.
a = io.open("test.txt")

print(a[1]) -- attempt to index global 'a' (a userdata value)
print(b.b) -- attempt to index global 'b' (a nil value)


Quote
attempt to index a X value

X
: either
nil
,
boolean
,
number
,
string
,
function
,
userdata
,
thread
, or
table
.

You're trying to index a literal or table value that couldn't be indexed.
a = {}
a[98] = true

print(a[1].index) -- attempt to index a boolean value
print(("a")[1]) -- attempt to index a string value


Quote
table index is nil


You're trying to set a value to nil index of a table.
a[nil] = true -- table index is nil


▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Function Calls
These errors are given to you if you're doing something weird with function calls. Keep in mind that function calls are only for, well, functions. Built-in functions give you an error if you're trying to send invalid arguments.

Quote
attempt to call Y 'X' (a Z value)

X
: variable that is attempted to be called.
Y
: either
local
,
global
, or
field
, based on
X
.
Z
: either
nil
,
boolean
,
number
,
string
,
function
,
userdata
,
thread
, or
table
, based on
X
.

You're trying to call
X
, while
X
is a
Z
value that couldn't be called.
print(a()) -- attempt to call global 'a' (a nil value)


Quote
attempt to call a X value

X
: either
nil
,
boolean
,
number
,
string
,
function
,
userdata
,
thread
, or
table
.

You're trying to call a literal or table value that couldn't be called.
a = {}
a.ortimh = "user"

a.ortimh() -- attempt to call a string value
io.openfile("file") -- attempt to call a nil value


Quote
bad argument #X to 'Z' (Y expected)

X
: position of the argument.
Y
: either
nil
,
boolean
,
number
,
string
,
function
,
userdata
,
thread
,
table
, or
value
.
Z
: a word or a symbol.

You're trying to send an invalid value at argument position #
X
to
Z
function while argument #
X
of the function is expecting
Y
.
a = {}
b = setmetatable(a, "__add") -- bad argument #2 to 'setmetatable' (nil or table expected)


Quote
bad argument #X to 'W' (Y expected, got Z)

W
: a word or a symbol.
X
: position of the argument.
Y
: either
nil
,
boolean
,
number
,
string
,
function
,
userdata
,
thread
,
table
, or
value
.
Z
: either
nil
,
boolean
,
number
,
string
,
function
,
userdata
,
thread
,
table
, or
no value
.

You're trying to send
Z
at argument position #
X
to
W
function while argument #
X
of the function is expecting
Y
.
print(table.concat()) -- bad argument #1 to 'concat' (table expected, got no value)


▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

You've read one, or some, or all of the errors meanings. These are several statements/questions that may lurk in your head currently.

I don't understand these errors meanings.
Sorry for the inconvenience. I'm trying to give the best-short explanation for you to understand it better without useless words being used. Errors that Lua give are pretty explanatory.

I always get "attempt to concatenate a boolean value" error while I don't have any boolean value?
Maybe you're trying to send invalid arguments to CS2D functions such as cs2d lua cmd player, thus resulting a false value.
user Flacko has written
Some cs2d functions such as player() return false on error (wrong parameter or invalid player ID)

Here's an example of how this error can occur:
function joinhook(id)
	msg2(id, "Welcome, " .. player(id - 1, "name") .. "!@C") -- attempt to concatenate a boolean value, if id = 1
end

addhook("join", "joinhook")


I'm getting "attempt to call a nil value" every X, without giving me which file that causes the error!
Maybe that's because you're adding a hook while the function you add to the hook is nil or not exist. You either need to delete the "adding hook" part or create a function for that hook. Here's an example of how this error can occur:
function joinhook(id)
	msg2(id, "Welcome, " .. player(id, "name") .. "!@C")
end

addhook("join", "welcomehook") -- attempt to call a nil value


What do ?, <eof> and <name> mean?
? means unknown, <eof> means end of file, and <name> could mean a lot such as identifier.

Let's hope that you find your solutions to your problems!
edited 11×, last 07.07.16 01:41:40 pm

old Re: [GUIDE] Lua errors meanings

GeoB99
Moderator Off Offline

Quote
That's a great idea! With this, anyone who has doubts regarding a Lua error, such as lack thereof explanation of the error or unknown root of it can check this thread for a better understanding and overview. Definitely this thread should get pinned as long if user DC agrees.

Aside, here's the solution of a problem (despite how basic it is) I have so far.

Getting the length of a number using # operator
1
2
3
4
5
a = 25
b = 10

print('Length of \'a\' variable is: ', #a)
print('Length of \'b\' variable is: ', #b)
Quote
Attempt to get length of global 'a' (a number value)

Question

Why it gives me such an error like this? Why doesn't want to get the length of these variables?

Synopsis

a
and
b
are global variables by default in Lua hence
get length of global
. It can be either local or global depending on coder preferences onto setting up the variables environment.

Solution

Before we're going to troubleshoot the issue, we must understand the semantics of
#
operator. This operator returns the length of a variable type. The type has to be whether a string or a table in order to get their length though. Therefore, only strings and tables by default are permissible within the operator - and for that matter it is explained on this website.

In any case, unbeknownst to some people (merely beginners) you can get the length of a number indirectly with
tostring()
for argument conversion into a string format type according to tostring() rules which would imply. Thus...

1
2
3
4
5
6
7
8
a = 25
b = 10

first = tostring(a)
second = tostring(b)

print('Length of \'a\' variable is: ', #first)
print('Length of \'b\' variable is: ', #second)

Note that you must create a new variable to hold
tostring()
for each variable which holds a number in it.

old Re: [GUIDE] Lua errors meanings

Yates
Reviewer Off Offline

Quote
I like the effort but I don't see how this will be used. We all know no one here searches on the forum or even uses Google anyway. Every time someone gets an error they will create a thread without even knowing this exists.

old Re: [GUIDE] Lua errors meanings

Cebra
User Off Offline

Quote
if you know how to solve this:
1
attempt to concatenate a boolean value

you can add it. I get this error regularly and don't know how to prevent/solve it

old Re: [GUIDE] Lua errors meanings

GeoB99
Moderator Off Offline

Quote
@user Cebra: That's because of Lua nature and mechanism to work on that way. Lua will NEVER concatenate boolean values in any way regardless of concatenation declaration. To explain briefly in a technical (or maybe not really technical at the fullest) way:

..
operator is mainly and intended only for concatenation of strings. A string represents an array/s (also so called a set) of character types. This is the main rule of the thumb regarding the strings. Boolean values, however, tend to fail at this rule. They only hold their particular values (true or 1 for true and false or 0 for false).

They never held arrays of chars in the first place. With that being said, your concatenation process fails leading to this error.

In conclusion, the solution would be by using
tostring()
for a total conversion into a string.
tostring()
converts nearly each key type onto a reasonable format regardless of type key category but you cannot convert vice-versa. Of course, you could catch this issue on a variety of ways and for that it is advisable to analyze the documentation of functions and things your're using in the code environment if they hold a boolean value or return a bool val. I guess user Dousea has more information in hand to cover this problem out.

old Re: [GUIDE] Lua errors meanings

Yates
Reviewer Off Offline

Quote
@user GeoB99: The problem is not understanding the error, it's understanding where it comes from. Lua supposedly (by my own experience) throws this error as a last resort and sometimes it doesn't even make sense to why it throws this.

Lua is weird like that.

old Re: [GUIDE] Lua errors meanings

Dousea
User Off Offline

Quote
@user Yates: Lua only reads and executes, not to understand what you're trying to do and executes. Lua is only interpreting statements and not compiling. It executes code on run-time. So this'd cause Lua to throw some unexpected errors as you said.

old Re: [GUIDE] Lua errors meanings

Flacko
User Off Offline

Quote
user Cebra has written
@user GeoB99: first, thanks
second, i know the things you write exept tostring function, but in my code isn't any bool value


Some cs2d functions such as player() return false on error (wrong parameter or invalid player ID)

old Re: [GUIDE] Lua errors meanings

Dousea
User Off Offline

Quote
Added function calls errors and "predicted statements/questions" at very bottom.

@user Flacko: Thanks for the statement. Added your post in OP as the answer of one of "predicted statements/questions".
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview