This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Rangicus's profile picture

[SOLVED]Attempt To Perform Arithmetic __add on nil and number

Started by Rangicus, 08 September 2012 - 12:47 AM
Rangicus #1
Posted 08 September 2012 - 02:47 AM
Alright so I am making an API, X-Cor API (A.K.A - xcorAPI), and I am trying to make a treecutting function but I get this error.

Attempt To Perform Arithmetic __add on nil and number
It says it is on line 6 of this code.

function getTree()
x = 0
while turtle.detect() do
  turtle.dig()
  turtle.digUp()
  turtle.up()
  x = x + 1
end
while turtle.detectDown() == false do
  turtle.down()
end
  print("The tree was "..x.." blocks high!")
end

I placed my 'file' in the API's folder and i am befuddled as to what I should try. Can someone help?
EDIT: When I run it I enter 'xcorAPI.getTree()' under the lua
EDIT: Here is my API http://pastebin.com/VHfk8KYP
Kingdaro #2
Posted 08 September 2012 - 02:49 AM
Your turtle.Up() call should be turtle.up(), lua is case sensitive.
Rangicus #3
Posted 08 September 2012 - 02:52 AM
Your turtle.Up() call should be turtle.up(), lua is case sensitive.
Actually it said turtle.up() first but I got this error so I changed it to turtle.Up() because I thought that would fix it, I am going to post the program to see if that will help.
Kingdaro #4
Posted 08 September 2012 - 02:55 AM
I knew the error was with the "x = x + 1" line because it's saying that x doesn't exist, yet you've clearly defined it. I couldn't find the answer to that problem but I figured that I'd point out what I actually found that would've caused an error.

Maybe you should say "local x = 0" instead of just "x = 0"?
Rangicus #5
Posted 08 September 2012 - 02:55 AM
I knew the error was with the "x = x + 1" line because it's saying that x doesn't exist, yet you've clearly defined it. I couldn't find the answer to that problem but I figured that I'd point out what I actually found that would've caused an error.

Maybe you should say "local x = 0" instead of just "x = 0"?
I will try that
Rangicus #6
Posted 08 September 2012 - 02:57 AM
I knew the error was with the "x = x + 1" line because it's saying that x doesn't exist, yet you've clearly defined it. I couldn't find the answer to that problem but I figured that I'd point out what I actually found that would've caused an error.

Maybe you should say "local x = 0" instead of just "x = 0"?
I will try that
No such luck:\
Luanub #7
Posted 08 September 2012 - 03:32 AM
Try adding in some prints to print x. Do it prior to the math and see what it prints. If you've got things right it should print 0.

If not play with it until it does. You can try moving the declaration outside of the function, just make sure it is above the function in the script.

local x = 0 -- declare it here.

function getTree()
x = 0 -- to reset the value each time the function is called.
while turtle.detect() do
  turtle.dig()
  turtle.digUp()
  turtle.up()
  x = x + 1
end
while turtle.detectDown() == false do
  turtle.down()
end
  print("The tree was "..x.." blocks high!")
end


I would definately keep it as a local var, it will save you from running into more issues later on.
Rangicus #8
Posted 08 September 2012 - 04:04 AM
FIXED It turns out the X thing was the entire problem got rid of it and worked perfectly