Jump to content




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


7 replies to this topic

#1 Rangicus

  • Members
  • 84 posts
  • LocationDeath City

Posted 08 September 2012 - 01: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

#2 Kingdaro

    The Doctor

  • Members
  • 1,634 posts
  • Location'MURICA

Posted 08 September 2012 - 01:49 AM

Your turtle.Up() call should be turtle.up(), lua is case sensitive.

#3 Rangicus

  • Members
  • 84 posts
  • LocationDeath City

Posted 08 September 2012 - 01:52 AM

 Kingdaro, on 08 September 2012 - 01:49 AM, said:

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.

#4 Kingdaro

    The Doctor

  • Members
  • 1,634 posts
  • Location'MURICA

Posted 08 September 2012 - 01: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"?

#5 Rangicus

  • Members
  • 84 posts
  • LocationDeath City

Posted 08 September 2012 - 01:55 AM

 Kingdaro, on 08 September 2012 - 01:55 AM, said:

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

#6 Rangicus

  • Members
  • 84 posts
  • LocationDeath City

Posted 08 September 2012 - 01:57 AM

 Rangemuldee, on 08 September 2012 - 01:55 AM, said:

 Kingdaro, on 08 September 2012 - 01:55 AM, said:

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:\

#7 Luanub

    Lua Nub

  • Members
  • 1,134 posts
  • LocationPortland OR

Posted 08 September 2012 - 02: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.

#8 Rangicus

  • Members
  • 84 posts
  • LocationDeath City

Posted 08 September 2012 - 03:04 AM

FIXED It turns out the X thing was the entire problem got rid of it and worked perfectly





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users