Lua Errors: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>GoldenUrg
initial set (from recent Scripting Helpers posts)
>Blocco
Added link to number and explained tricky mistakes on precision
Line 46: Line 46:


== Tricky Mistakes ==
== Tricky Mistakes ==
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>
<pre>
n = 0
n = 0
Line 61: Line 61:
end
end
</pre>
</pre>
<!-- Mark for below, may need to use more age-appropriate language that young viewers will understand. -->
As you can see from the examples above, when using floating points, the numbers will not always necessarily be calculated with <u>exact</u> precision.  This is because in binary, the number system that computers use, when using a number less than 1, you can't really get perfect precision with "binary fractions."  It is better explained in the following example.
<pre>
for i = 0, 1, 1/16 do
if i == 3/16 then
print("Yes!")
end
end
for i = 0, 1, 1/10 do
if i == 3/10 then
print("No!")
end
end
</pre>
A number out of 16 is easily calculated with "binary fractions," while a number out of 10 isn't.  More precision is required to do that.  That is why in "binary fractions," you can't get exactly a number out of 10.
<!-- Mark ends. -->

Revision as of 20:02, 30 April 2010

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

Wed Apr 28 21:10:21 2010 - Workspace.Script:3: 'end' expected (to close 'while' at line 1) near '<eof>'

Let's break the message down in parts:

  • "Wed Apr 28 21:10:21 2010" - 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

Wed Apr 28 21:11:56 2010 - Workspace.Script:7: attempt to perform arithmetic on global 'a' (a function value)
Wed Apr 28 21:11:56 2010 - 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.

Confusing Error Messages

Wed Apr 28 21:10:21 2010 - 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)
Wed Apr 28 21:10:30 2010 - Workspace.Script:4: '<eof>' expected near 'end'

The end on line 4 is extra.

Wed Apr 28 21:34:08 2010 - Workspace.Script:1: 'then' expected near '='

This is caused by using '=' (assignment) in an if instead of '==' (comparsion)

Wed Apr 28 21:36:33 2010 - Workspace.Script:2: '=' expected near 'if'
Wed Apr 28 21:38:42 2010 - Workspace.Script:2: unexpected symbol near 'if'

Errors on the beginning of a line are often because an incomplete previous 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
part.Transparency = 0.1
if( part.Transparency == 0.1 ) then -- false
--
end

As you can see from the examples above, when using floating points, the numbers will not always necessarily be calculated with exact precision. This is because in binary, the number system that computers use, when using a number less than 1, you can't really get perfect precision with "binary fractions." It is better explained in the following example.

for i = 0, 1, 1/16 do
if i == 3/16 then
print("Yes!")
end
end

for i = 0, 1, 1/10 do
if i == 3/10 then
print("No!")
end
end

A number out of 16 is easily calculated with "binary fractions," while a number out of 10 isn't. More precision is required to do that. That is why in "binary fractions," you can't get exactly a number out of 10.