This guide is going to go through common errors, what causes them and how to fix errors not mentioned here. If you do find another error please post it here and me or another community member will try fix it and I will add it to this guide
Analysing what the error means
Let's start off by analysing an error and what the different sections of it mean:
I saved this program as 'test' and ran it, the error I got was:
An error is always put in parts separated by colons. The first part is the name of the program, the second part is the line that the code is erroring in and the third part is the type of error. in other errors there are more parts before the name of the program and may start with "bios:" or something similar, in most cases you are only interested in the last 2 parts of the error
This particular error is saying that in the program called 'test' on Line 1 there was an attempt to call nil.
Attempt to call nil
This error is pointing out that the program is trying to call a function that doesnt exist.
In the above example this could be fixed by changing the code to
Sometimes this problem is caused by spelling mistakes or incorrect capitalisation. most functions will have the first world uncapitalised and the second word capitalised but that is not always the case as shown below
Another cause is when you call a function before it has been stated. Example:
To combat this, most programmers will put all the functions in the code first and then use them after all functions have been stated.
Too Long Without Yielding
This error is quite difficult to fix, this occurs when a computer is checking for something faster than it can get a response. I occasionally get this error when a computer is waiting for a redstone signal such as the following:
to fix this problem when it is redstone related I add the line os.pullEvent("redstone") below before i wait for a redstone signal. this basically procedes through the loop when there is a redstone update near the computer on any side (this includes a redstone signal being turned off)
if it is not redstone related you can add the line sleep(0) in the first line of the while loop, this gives the computer some time to do its other things
Syntax Errors (error ends in "expected")
This type of error is generally where you make a typo, or you forget to add a piece of code
Common examples:
'then' expected, when doing if statements you are comparing statements if you write a = b you are stating what a is equal to instead of comparing it to b, that is why you use "==" instead of "=". Another cause of this problem is where you forget to add the word "then" after writing "if" or "elseif". the below code will result in this error
'end' expected, this is basically when you forget to close either a function, an if statement, a while loop or anything else similar.
[Variable Type] Expected, Got [Variable Type]
The variable type in this error can be either a Boolean, a Function, nil, a number, a string or an index (array)
I was able to reproduce this by trying to modify the data of an array before it was declared an Array:
this can be fixed by declaring it as an array/index
Common Bugs with No Error
Maybe your program is presenting no errors but it still doesn't do what you want it to.
So maybe your error is not here or the solutions with no error dont seem to be working. When it comes to this I decide to take a different path and try and follow what happens to a variable as a program progresses, how it is modified through each of its functions and maybe see if somewhere along the way it is being changed to something I don't want.
Another approach is to print variables to see if they are what you want in different parts of the code.
If nothing here helped you with your problem please post a pastebin link to the code, if applicable a picture of your setup in a spoiler, spoilers are made with the tags [SPOILER][/SPOILER], the error the program is having and also a description of what the program is supposed to be doing
Please reply here if you find anything incorrect or you have anything to add to this thread
Analysing what the error means
Let's start off by analysing an error and what the different sections of it mean:
Code:
undefinedFunction()
Code:
test:1: attempt to call nil
This particular error is saying that in the program called 'test' on Line 1 there was an attempt to call nil.
Attempt to call nil
This error is pointing out that the program is trying to call a function that doesnt exist.
In the above example this could be fixed by changing the code to
Code:
function definedFunction()
end
definedFunction()
Code:
toNumber("1") -- this will error
tonumber("1") -- this will work
redstone.getinput("back") -- this will error
redstone.getInput("back") -- this will work
Code:
definedFunction()
function definedFunction()
end
Too Long Without Yielding
This error is quite difficult to fix, this occurs when a computer is checking for something faster than it can get a response. I occasionally get this error when a computer is waiting for a redstone signal such as the following:
Code:
while true do
if redstone.getInput("back") == true then
print("redstone on")
end
end
Code:
while true do
os.pullEvent("redstone")
if redstone.getInput("back") == true then
print("redstone on")
end
end
Syntax Errors (error ends in "expected")
This type of error is generally where you make a typo, or you forget to add a piece of code
Common examples:
'then' expected, when doing if statements you are comparing statements if you write a = b you are stating what a is equal to instead of comparing it to b, that is why you use "==" instead of "=". Another cause of this problem is where you forget to add the word "then" after writing "if" or "elseif". the below code will result in this error
Code:
a = 2
b = 2
if a = b then
print(a)
end
'end' expected, this is basically when you forget to close either a function, an if statement, a while loop or anything else similar.
[Variable Type] Expected, Got [Variable Type]
The variable type in this error can be either a Boolean, a Function, nil, a number, a string or an index (array)
I was able to reproduce this by trying to modify the data of an array before it was declared an Array:
Code:
array[1] = "1"
print(array[1])
Code:
array = {}
array[1] = "1"
print(array[1])
Common Bugs with No Error
Maybe your program is presenting no errors but it still doesn't do what you want it to.
- Variable Scope: if you declare a variable as local inside a function the variable is an argument of a function then that variable won't be accessible outside that piece of code. Visit this website for more info on variable scope in lua
- Typos: It is still possible to not get an error with a typo, you may have tried to use a shell.run() command on a program that doesn't exist or something similar
- You may have an if statement that is checking something that should be true but might be something else, a good way to figure out if this is happening is to print() the variable you are comparing before an if statement to see if the variable was modified to something else.
So maybe your error is not here or the solutions with no error dont seem to be working. When it comes to this I decide to take a different path and try and follow what happens to a variable as a program progresses, how it is modified through each of its functions and maybe see if somewhere along the way it is being changed to something I don't want.
Another approach is to print variables to see if they are what you want in different parts of the code.
If nothing here helped you with your problem please post a pastebin link to the code, if applicable a picture of your setup in a spoiler, spoilers are made with the tags [SPOILER][/SPOILER], the error the program is having and also a description of what the program is supposed to be doing
Please reply here if you find anything incorrect or you have anything to add to this thread