Lua Errors: Difference between revisions
>NecroBumpist Update to use syntax highlighting. |
m Text replacement - "</code>" to "</SyntaxHighlight>" |
||
Line 75: | Line 75: | ||
Game.GetService( -- missing close parenthesis ) | Game.GetService( -- missing close parenthesis ) | ||
Game:GetService -- missing parentheses () or "function arguments" | Game:GetService -- missing parentheses () or "function arguments" | ||
</ | </SyntaxHighlight> | ||
----- | ----- | ||
{{ErrorMessage|Workspace.Script:3: '=' expected near '=='|}} | {{ErrorMessage|Workspace.Script:3: '=' expected near '=='|}} | ||
Line 84: | Line 84: | ||
<code lua> | <code lua> | ||
print( 1.." tests passed" ) | print( 1.." tests passed" ) | ||
</ | </SyntaxHighlight> | ||
instead of | instead of | ||
<code lua> | <code lua> | ||
print( 1 .." tests passed" ) | print( 1 .." tests passed" ) | ||
</ | </SyntaxHighlight> | ||
----- | ----- | ||
{{ErrorMessage|Workspace.Script:1: bad argument #3 to '?' (Object expected, got function)|}} | {{ErrorMessage|Workspace.Script:1: bad argument #3 to '?' (Object expected, got function)|}} | ||
Line 99: | Line 99: | ||
<code lua> | <code lua> | ||
print( Workspace.Part[print] ) | print( Workspace.Part[print] ) | ||
</ | </SyntaxHighlight> | ||
----- | ----- | ||
{{ErrorMessage|Workspace.Script:1: bad argument #2 to '?' (Vector3 expected, got number)|}} | {{ErrorMessage|Workspace.Script:1: bad argument #2 to '?' (Vector3 expected, got number)|}} | ||
Line 105: | Line 105: | ||
<code lua> | <code lua> | ||
print( Vector3.new() + 1 ) | print( Vector3.new() + 1 ) | ||
</ | </SyntaxHighlight> | ||
* Advanced Note: The message comes from the __add(obj,val) function in the Instance metatable. | * Advanced Note: The message comes from the __add(obj,val) function in the Instance metatable. | ||
----- | ----- | ||
Line 113: | Line 113: | ||
print( Workspace.Part1 ) -- ok | print( Workspace.Part1 ) -- ok | ||
Workspace.Part1 = Instance.new("Part") -- error | Workspace.Part1 = Instance.new("Part") -- error | ||
</ | </SyntaxHighlight> | ||
You need to set the Name and Parent properties instead: | You need to set the Name and Parent properties instead: | ||
<code lua> | <code lua> | ||
Line 119: | Line 119: | ||
part.Name = "Part1" | part.Name = "Part1" | ||
part.Parent = Workspace | part.Parent = Workspace | ||
</ | </SyntaxHighlight> | ||
* Advanced Note: Why does this not have a source line? | * Advanced Note: Why does this not have a source line? | ||
----- | ----- | ||
Line 140: | Line 140: | ||
bv.Value = true | bv.Value = true | ||
</ | </SyntaxHighlight> | ||
----- | ----- | ||
{{ErrorMessage|Unable to cast value to std::string}} | {{ErrorMessage|Unable to cast value to std::string}} | ||
Line 148: | Line 148: | ||
<code lua> | <code lua> | ||
Game:FindFirstChild(Workspace) | Game:FindFirstChild(Workspace) | ||
</ | </SyntaxHighlight> | ||
instead of | instead of | ||
<code lua> | <code lua> | ||
Game:FindFirstChild("Workspace") | Game:FindFirstChild("Workspace") | ||
</ | </SyntaxHighlight> | ||
----- | ----- | ||
{{ErrorMessage|chunk has too many syntax levels}} | {{ErrorMessage|chunk has too many syntax levels}} | ||
Line 178: | Line 178: | ||
local value = some_function() | local value = some_function() | ||
(another_function or some_function)(2, true, "taco") | (another_function or some_function)(2, true, "taco") | ||
</ | </SyntaxHighlight> | ||
What's wrong? | What's wrong? | ||
Line 194: | Line 194: | ||
if( n == 0.9 ) then break end -- never happens | if( n == 0.9 ) then break end -- never happens | ||
end | end | ||
</ | </SyntaxHighlight> | ||
Just like the number 1/3 would be 0.3333... (repeating) in decimal. The number 1/10 is 0.0<span style="text-decoration:overline;">0011</span> (repeating) in binary. Exact binary values are powers of 2, like 1/2, 1/4, 1/8, 1/16 etc. And multiples of those. | Just like the number 1/3 would be 0.3333... (repeating) in decimal. The number 1/10 is 0.0<span style="text-decoration:overline;">0011</span> (repeating) in binary. Exact binary values are powers of 2, like 1/2, 1/4, 1/8, 1/16 etc. And multiples of those. | ||
Line 205: | Line 205: | ||
-- | -- | ||
end | end | ||
</ | </SyntaxHighlight> | ||
In this case, Transparency property of Roblox stores less digits than Lua uses. | In this case, Transparency property of Roblox stores less digits than Lua uses. |
Revision as of 03:13, 27 April 2023
Reading Lua Error Messages
Most errors are shown in Output Window in Roblox Studio or Tools. Understanding them can make debugging efforts much more focused.
Compile Errors
Thursday Nov 28 11:04:56 2024 - Workspace.Script:3: 'end' expected (to close 'while' at line 1) near '<eof>'
Let's break the message down in parts:
- "Wed Dec 09 12:34:56 2009" - time of the message
- "Workspace.Script" - Full name of the script
- "3" - the line with the error
- "'end' expected (to close 'while' at line 1) near '<eof>'" - the error message
Important: Output window doesn't always show these errors (see Debugging)
Runtime Errors
Thursday Nov 28 11:04:56 2024 - Workspace.Script:7: attempt to perform arithmetic on global 'a' (a function value) Wed Dec 09 12:34:56 2009 - Workspace.Script, line 7 - global b Workspace.Script, line 2 - global a Workspace.Script, line 14 stack end
The first part is the same as a compile time error. The second part is called a stack trace.
- "Workspace.Script, line 7" - the source of the error (inside function b)
- "- global b Workspace.Script, line 2" - call to function b (from function a)
- "- global a Workspace.Script, line 14" - call to function a (from main script)
- "stack end" - end of the stack trace
Advanced note: Stack trace doesn't include calls to functions that use a tail call.
Basic Lua Errors
Thursday Nov 28 11:04:56 2024 - Workspace.Script:2: attempt to index global 'a' (a function value) Thursday Nov 28 11:04:56 2024 - Workspace.Script:2: attempt to perform arithmetic on local 'a' (a string value) Thursday Nov 28 11:04:56 2024 - Workspace.Script:2: attempt to concatenate upvalue 'a' (a nil value) Thursday Nov 28 11:04:56 2024 - Workspace.Script:2: attempt to call field 'a' (a number value)
Many Lua "operators" give similar error messages:
- "Workspace.Script:2:" - The source of the error
- "attempt to ___" - The error is caused by the given operation on the wrong type of variable
Operation Name | Operator | Allowed Types |
---|---|---|
index | . or [k] | table, string |
perform arithmetic | various | number, string* |
concatenate | .. | string, number |
call | (...) | function |
- Note: String can only be used in arithmetic if it can be converted to a number.
- Advanced Note: Metatables can be used to allow tables or userdata perform any of these operations.
- "___ 'a'" - The given type of a variable named 'a' (a will be the actual name in the code)
- NOTE: that a field name itself can be a variable in which case the message says '?'
- "(a ___ value)" - Gives the actual type of the variable
Confusing Error Messages
Thursday Nov 28 11:04:56 2024 - Workspace.Script:3: 'end' expected (to close 'while' at line 1) near '<eof>'
The while on line 1 is missing an end. You need an end for each of the following:
- function
- do
- if (but not elseif)
Thursday Nov 28 11:04:56 2024 - Workspace.Script:4: '<eof>' expected near 'end'
The end on line 4 is extra.
Thursday Nov 28 11:04:56 2024 - Workspace.Script:1: 'then' expected near '='
This is caused by using '=' (assignment) in an if instead of '==' (comparsion)
Thursday Nov 28 11:04:56 2024 - Workspace.Script:2: '=' expected near 'if' Thursday Nov 28 11:04:56 2024 - Workspace.Script:2: unexpected symbol near 'if' Thursday Nov 28 11:04:56 2024 - Workspace.Script:2: function arguments expected near 'if'
Errors on the beginning of a line are often because an incomplete previous line.
In this case, the problems are:
Game.GetService -- missing parentheses ()
Game.GetService( -- missing close parenthesis )
Game:GetService -- missing parentheses () or "function arguments"
</SyntaxHighlight>
Thursday Nov 28 11:04:56 2024 - Workspace.Script:3: '=' expected near '=='
This is caused by using '==' (comparison) instead of '=' assignment.
Thursday Nov 28 11:04:56 2024 - Workspace.Script:1: malformed number near '1..'
This is caused by attempting to concatenate a number without putting a space since dots can be part of a number:
print( 1.." tests passed" )
</SyntaxHighlight>
instead of
print( 1 .." tests passed" )
</SyntaxHighlight>
Thursday Nov 28 11:04:56 2024 - Workspace.Script:1: bad argument #3 to '?' (Object expected, got function)
This error was caused by assigning the wrong type of value to a property:
Workspace.Part.Parent = print
- Advanced Note: The message comes from the __newindex(obj,ndx,val) function in the Instance metatable.
Similarly, you can get
Thursday Nov 28 11:04:56 2024 - Workspace.Script:1: bad argument #2 to '?' (string expected, got function)
By indexing an object with the wrong type:
print( Workspace.Part[print] )
</SyntaxHighlight>
Thursday Nov 28 11:04:56 2024 - Workspace.Script:1: bad argument #2 to '?' (Vector3 expected, got number)
This error was caused by incorrect math on an Object
print( Vector3.new() + 1 )
</SyntaxHighlight>
- Advanced Note: The message comes from the __add(obj,val) function in the Instance metatable.
Thursday Nov 28 11:04:56 2024 - Part1 is not a valid member of Workspace
Aside from the obvious, this message also occurs if you attempt to set a child directly:
print( Workspace.Part1 ) -- ok
Workspace.Part1 = Instance.new("Part") -- error
</SyntaxHighlight>
You need to set the Name and Parent properties instead:
local part = Instance.new("Part")
part.Name = "Part1"
part.Parent = Workspace
</SyntaxHighlight>
- Advanced Note: Why does this not have a source line?
Thursday Nov 28 11:04:56 2024 - Workspace.Script:1: bad argument #1 to 'Lerp' (Vector3 expected, got userdata)
Internally, Roblox objects use the userdata type of Lua. This means we've passed the wrong kind of object to the Lerp method.
Thursday Nov 28 11:04:56 2024 - Unknown exception
This error happens when you attempt to call a locked method, use a locked event, or change a locked property. This means that you're trying to use something that Roblox does not want you to use for security reasons.
Unusual Error Messages
Thursday Nov 28 11:04:56 2024 - maximum event re-entrancy depth exceeded
This is caused by too many events triggering each other.
A simple example is this script:
local bv = Instance.new("BoolValue")
bv.Parent = script
bv.Value = false
bv.Changed:connect( function() bv.Value = not bv.Value end )
bv.Value = true
</SyntaxHighlight>
Thursday Nov 28 11:04:56 2024 - Unable to cast value to std::string
This is a C++ error from the underlying Roblox function. They may have skipped the Lua type check for performance.
Most common reason for this is passing nil or other Lua type to method expecting a string:
Game:FindFirstChild(Workspace)
</SyntaxHighlight>
instead of
Game:FindFirstChild("Workspace")
</SyntaxHighlight>
Thursday Nov 28 11:04:56 2024 - chunk has too many syntax levels
This is caused by doing too many operations at once.
It can result from:
- Too many operations (e.g. Doing 500 concatenations in a single expression)
- Too many nested control statements (Loops, if statements, etc)
Local Variable Errors
Thursday Nov 28 11:04:56 2024 - main function has more than 200 local variables
This is due to declaring more than 200 local variables. This artificial limit is put in place to prevent problems when actually running your script.
Thursday Nov 28 11:04:56 2024 - function at line <number> has more than 60 upvalues
Like the above error, this one is artificial. Again, it has to do with maintaining your Lua script's state while executing it.
If you encounter either this error or the one above, you should look into grouping some of your variables into at least one localized table.
Ambiguous Syntax Errors
Thursday Nov 28 11:04:56 2024 - ambiguous syntax (function call x new statement) near '<some of your code>'
When this might happen:
local value = some_function()
(another_function or some_function)(2, true, "taco")
</SyntaxHighlight>
What's wrong?
Due to the way Lua handles Whitespace, it thinks that the second line is an extension of the first.
Instead of seeing two separate statements, Lua sees them as one, but it's not sure whether this is right. That's because it's ambigious, so it throws an error.
This can be fixed by adding a semicolon (;) after the first line.
Tricky Mistakes
Floating point calculations can be surprising; it's safer to use inequalities. Or use an integer for controlling the loop.
n = 0
while true do
n = n + 0.1
if( n == 0.9 ) then break end -- never happens
end
</SyntaxHighlight>
Just like the number 1/3 would be 0.3333... (repeating) in decimal. The number 1/10 is 0.00011 (repeating) in binary. Exact binary values are powers of 2, like 1/2, 1/4, 1/8, 1/16 etc. And multiples of those.
Since a computer must stop at a certain number of digits, the (repeating) idea is lost. If you add up 3 * 1/3, you get 0.999 instead of 1 or 10 * 1/10 in the computer is very close to 1 but not exactly. (In fact, it's so close to 1 that if you print it, it will say "1", but if you compare it with == or subtract it from one you'll see a slight difference).
part.Transparency = 0.1
if( part.Transparency == 0.1 ) then -- false
--
end
</SyntaxHighlight>
In this case, Transparency property of Roblox stores less digits than Lua uses.
Example
Lua says 1/3 is 0.333333, but
Transparency only holds 3 digits so it is set to 0.333.
When Lua compares 0.333 to 0.333333 they are not equal.