The Errors of ComputerCraft and How to Fix Them

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here
  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

VikeStep

New Member
Jul 29, 2019
1,117
0
0
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:
Code:
undefinedFunction()
I saved this program as 'test' and ran it, the error I got was:
Code:
test:1: attempt to call nil
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
Code:
function definedFunction()
end
 
definedFunction()
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
Code:
toNumber("1") -- this will error
tonumber("1") -- this will work
redstone.getinput("back") -- this will error
redstone.getInput("back") -- this will work
Another cause is when you call a function before it has been stated. Example:
Code:
definedFunction()
 
function definedFunction()
end
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:
Code:
while true do
  if redstone.getInput("back") == true then
    print("redstone on")
  end
end
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)
Code:
while true do
os.pullEvent("redstone")
  if redstone.getInput("back") == true then
    print("redstone on")
  end
end
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
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])
this can be fixed by declaring it as an array/index
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.
Troubleshooting
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
 

whizzball1

New Member
Jul 29, 2019
2,502
0
0
I WISH I COULD LIKE THIS A MILLION BILLION TIMES!
No really.
The first time I attempted to make a program in CC, it was to break a block when receiving a redstone signal, and the Too Long Without Yielding error happened, so this is great for me!
Thanks!
Of course, I will probably continue to be completely clueless at CC.
Thanks so much!
 

Josh Senneff

New Member
Jul 29, 2019
10
0
0
Hi. Ive been trying to program a mining turtle to mine a 2x1 tunnel. Ive tried to program it to place torches, place blocks underneith it as it goes and I tried to make it go to a chest when its done and put everything in it and then to move on to the next tunnel. Ecerytime I try to mine a certain distance long tunnel, it doesnt mine the amount of blocks I want and when it tries to go back to the chest, it doesnt make it the whole way there. Heres the pastebin link:
Http://pastebin.com/CMFGZLDR
Please Help!
 

jordsta95

New Member
Jul 29, 2019
5,056
-4
1
I would like to assume a few things:
1) You didn't check the ComputerCraft forums that script is sorta like what you're looking for if I remember it correctly
2) ComputerCraft may have changed, so these things may be outdated
3) I haven't seen the OP around here, so they may not answer. See answer 1 for what may be your best bet (that I can think of)
 

The_Revanchist

New Member
Jul 29, 2019
64
0
0
Hey, could i take a moment with this program i wrote, the issue is Attempt to call nil on line 17 and im not sure of what it is... I put in an arrow showing the line also saying Broken Line. If you see any other mistakes tell me :)

sensor = peripheral.wrap("left")
gterm = peripheral.wrap("top")
owner = RevanBAS

local pl_list = {}
local pl_pos = {}

gterm.clear()
pl_box = gterm.addBox(5, 25, 100, 10, 0x000000, 0.4)
gterm.addText(6, 26, "Players nearby:", 0xFFFFFF)
gterm.addBox(5, 150, 100, 100, 0x339933, 0.2)
for i = 0, 19 do
pl_pos = gterm.addBox(0, 0, 5, 5, 0xFFFFFF, 0.2)
end

while true do
pl = sensor.getPlayerNames() <------ Broken Line
pl_box.setHeight(10 + 10 * #pl)

q = 0
for i, j in pairs(pl) do
data = sensor.getPlayerData(j)
pos = data["position"]
pl_pos.setX(pos["x"] + 55)
pl_pos.setY(pos["z"] + 205)
pl_list = gterm.addText(6, 26 + 10 * i, j, 0xFFFFFF)
end

os.sleep(5)

for i = 1, #pl do
pl_list.delete()
end
 

VikeStep

New Member
Jul 29, 2019
1,117
0
0
Can you find the code located in your saves folder? should be a computercraft folder in there. Upload that as I am skeptical that the line you point to is the issue.

Also in line 3 you do owner=RevanBAS, did you forget quotation marks?
 

The_Revanchist

New Member
Jul 29, 2019
64
0
0
Simple enough. BTW i did decided to change it to sensor on top and bridge on bottom cause I liked it better lol

Edit: Image didnt work im resending
 
Last edited:

VikeStep

New Member
Jul 29, 2019
1,117
0
0
So it still doesn't work if you change the locations in the code?

try exit out of that and go into your lua prompt and try wrap the sensor and call sensor.getPlayerNames()
 

The_Revanchist

New Member
Jul 29, 2019
64
0
0
If it helps to know any of this, Im on DW20 1.7.10 using CC V.1.65 & OpenperipheralCore V.0.5.0 and the addon V.0.2.0
 

VikeStep

New Member
Jul 29, 2019
1,117
0
0
it seems to be an OpenPeripheral bug then. Take it to either the #OpenMods IRC or to the CC Forums page for OpenPeripheral Addons and take it there