Lua Errors: Difference between revisions

2,313 bytes added ,  27 April 2023
m
Text replacement - "</SyntaxHighlight>" to "</syntaxhighlight>"
>Blocco
(How's this?)
m (Text replacement - "</SyntaxHighlight>" to "</syntaxhighlight>")
 
(13 intermediate revisions by 4 users not shown)
Line 33: Line 33:
* "Workspace.Script:2:" - The source of the error
* "Workspace.Script:2:" - The source of the error
* "attempt to ___" - The error is caused by the given operation on the wrong type of variable
* "attempt to ___" - The error is caused by the given operation on the wrong type of variable
{|
{| class="wikitable"
! Operation Name !! Operator !! Allowed Types
! Operation Name !! Operator !! Allowed Types
|-
|-
| index || '''.''' ''or'' '''['''___''']''' || table, string
| index || '''.''' ''or'' '''['''k''']''' || table, string
|-
|-
| perform arithmetic || ''various'' || number, string*
| perform arithmetic || ''various'' || number, string*
Line 42: Line 42:
| concatenate || '''..''' || string, number
| concatenate || '''..''' || string, number
|-
|-
| call || '''('''___''')''' || function
| call || '''('''...''')''' || function
|}
|}
** Note: String can only be used in arithmetic if it can be converted to a number.
** Note: String can only be used in arithmetic if it can be converted to a number.
Line 71: Line 71:
Errors on the beginning of a line are often because an incomplete previous line.
Errors on the beginning of a line are often because an incomplete previous line.
In this case, the problems are:
In this case, the problems are:
<pre>
<syntaxhighlight lang="lua">
Game.GetService -- missing parentheses ()
Game.GetService -- missing parentheses ()
Game.GetService( -- missing close parenthesis )
Game.GetService( -- missing close parenthesis )
Game:GetService -- missing parentheses () or "function arguments"
Game:GetService -- missing parentheses () or "function arguments"
</pre>
</syntaxhighlight>
-----
-----
{{ErrorMessage|Workspace.Script:3: '&#61;' expected near '&#61;&#61;'|}}
{{ErrorMessage|Workspace.Script:3: '&#61;' expected near '&#61;&#61;'|}}
Line 82: Line 82:
{{ErrorMessage|Workspace.Script:1: malformed number near '1..'|}}
{{ErrorMessage|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:
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 lang="lua">
print( 1.." tests passed" )
</syntaxhighlight>
instead of
instead of
print( 1 .." tests passed" )
<syntaxhighlight lang="lua">
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 93: Line 97:
{{ErrorMessage|Workspace.Script:1: bad argument #2 to '?' (string expected, got function)|}}
{{ErrorMessage|Workspace.Script:1: bad argument #2 to '?' (string expected, got function)|}}
By indexing an object with the wrong type:
By indexing an object with the wrong type:
print( Workspace.Part[print] )
<syntaxhighlight lang="lua">
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)|}}
This error was caused by incorrect math on an Object
This error was caused by incorrect math on an Object
print( Vector3.new() + 1 )
<syntaxhighlight lang="lua">
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.
-----
-----
{{ErrorMessage|Part1 is not a valid member of Workspace|}}
{{ErrorMessage|Part1 is not a valid member of Workspace|}}
Aside from the obvious, this message also occurs if you attempt to set a child directly:
Aside from the obvious, this message also occurs if you attempt to set a child directly:
print( Workspace.Part1 ) -- ok
<syntaxhighlight lang="lua">
Workspace.Part1 = Instance.new("Part") -- error
print( Workspace.Part1 ) -- ok
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:
local part = Instance.new("Part")
<syntaxhighlight lang="lua">
part.Name = "Part1"
local part = Instance.new("Part")
part.Parent = Workspace
part.Name = "Part1"
part.Parent = Workspace
</syntaxhighlight>
* Advanced Note: Why does this not have a source line?
* Advanced Note: Why does this not have a source line?
-----
-----
{{ErrorMessage|Workspace.Script:1: bad argument #1 to 'Lerp' (Vector3 expected, got userdata)|}}
{{ErrorMessage|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.
Internally, Roblox objects use the [[userdata]] type of Lua. This means we've passed the wrong kind of object to the Lerp method.
-----
{{ErrorMessage|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 ==
== Unusual Error Messages ==
Line 117: Line 132:
This is caused by too many events triggering each other.
This is caused by too many events triggering each other.
A simple example is this script:
A simple example is this script:
<pre>
<syntaxhighlight lang="lua">
local bv = Instance.new("BoolValue")
local bv = Instance.new("BoolValue")
bv.Parent = script
bv.Parent = script
Line 125: Line 140:


bv.Value = true
bv.Value = true
</pre>
</syntaxhighlight>
-----
-----
{{ErrorMessage|Unable to cast value to std::string}}
{{ErrorMessage|Unable to cast value to std::string}}
Line 131: Line 146:


Most common reason for this is passing nil or other Lua type to method expecting a string:
Most common reason for this is passing nil or other Lua type to method expecting a string:
Game:FindFirstChild(Workspace)
<syntaxhighlight lang="lua">
Game:FindFirstChild(Workspace)
</syntaxhighlight>
instead of  
instead of  
Game:FindFirstChild("Workspace")
<syntaxhighlight lang="lua">
Game:FindFirstChild("Workspace")
</syntaxhighlight>
-----
-----
{{ErrorMessage|chunk has too many syntax levels}}
{{ErrorMessage|chunk has too many syntax levels}}
This is caused by doing too many operations at once. E.g. Doing 500 concatenations is likely to raise this error.
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]], [[Conditional_statements|if statements]], etc)
 
=== Local Variable Errors ===
{{ErrorMessage|main function has more than 200 local variables}}
This is due to declaring more than 200 [[Variable#Local_Variables|local variables]]. This artificial limit is put in place to prevent problems when actually running your script.
 
{{ErrorMessage|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|table]].
 
=== Ambiguous Syntax Errors ===
{{ErrorMessage|ambiguous syntax (function call x new statement) near '<some of your code>'}}
<!--Not sure if this how the error would turn out, I think it needs a line number, but I don't know how ROBLOX would handle it.!-->
 
When this might happen:
<syntaxhighlight lang="lua">
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 ==
== Tricky Mistakes ==
[[Number|Floating point]] calculations can be surprising; it's safer to use inequalities. Or use an integer for controlling the loop.
[[Number|Floating point]] calculations can be surprising; it's safer to use inequalities. Or use an integer for controlling the loop.
<pre>
<syntaxhighlight lang="lua">
n = 0
n = 0
while true do
while true do
Line 146: Line 194:
   if( n == 0.9 ) then break end -- never happens
   if( n == 0.9 ) then break end -- never happens
end
end
</pre>
</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 152: Line 200:
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).
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).


<pre>
<syntaxhighlight lang="lua">
part.Transparency = 0.1
part.Transparency = 0.1
if( part.Transparency == 0.1 ) then -- false
if( part.Transparency == 0.1 ) then -- false
--
--
end
end
</pre>
</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.