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

arithmetic __sub on nil and number error

Started by Uzra, 08 April 2013 - 12:07 PM
Uzra #1
Posted 08 April 2013 - 02:07 PM
Hi, I tried to write my first basic program today and it was intended to dig out a shallowish 16x16 chunk of land just to make underground base building easier. However I've ran into an error that I have no idea how to fix :\
Error is "chunkdigger:63: attempt to perform arithmetic __sub on nil and number"
I realize that this is incredibly amateurish code but this is the first programming I've ever done so…. Yeah :P/> Anyways, here's the raw code from pastebin:
Spoiler

--Variable chunk excavating depth program, doesn't include auto disposal of blocks however.
local function DigOneRow()
for i = 1,15 do
  while (turtle.detect()) do
   turtle.dig()
   sleep(0.5)
  end
  turtle.forward()
end
end
local function DigTwoRows()
DigOneRow()
turtle.turnRight()
while (turtle.detect()) do
  turtle.dig()
  sleep(0.5)
end
turtle.forward()
turtle.turnRight()
DigOneRow()
turtle.turnLeft()
while (turtle.detect()) do
  turtle.dig()
  sleep(0.5)
end
turtle.forward()
turtle.turnLeft()
end
local function LastRowsInLayer()
DigOneRow()
turtle.turnRight()
while (turtle.detect()) do
  turtle.dig()
  sleep(0.5)
end
turtle.forward()
turtle.turnRight()
DigOneRow()
end
local function MoveToStartXZ()
turtle.turnRight()
DigOneRow()
turtle.turnRight()
end
local function DigOneLayer()
for i=1,7 do
  DigTwoRows()
end
LastRowsInLayer()
MoveToStartXZ()
turtle.digDown()
turtle.Down()
end
local function UserDepthInput()
print("Hello. I am Shaft001. I can dig a 16x16 hole to the depth you desire. Please note that I have no checkFuel function so you must chock me full of fuel before starting! If digging a deep shaft, note that I do not have an automatic item storage function so you must periodically empty my inventory.")
print("Please enter a value, preferably under 20, but must be less than current Y value-4.")
print("Dig Depth:")
depth= read("")
print("Your selected depth is: "..depth)
end
local function InputtedDepthLayers()
local depth=depth
local depthup=depth-1
for i=1,depth do
  DigOneLayer()
end
turtle.Up()
turtle.select(1)
turtle.placeDown()
for i=1,depthup do
  turtle.Up()
end
end
InputtedDepthLayers()
Thanks and sorry for being newbish xD
Kingdaro #2
Posted 08 April 2013 - 02:17 PM
On that line, the script attempts to subtract 1 from the variable "depth", which hasn't been defined yet. You should call UserDepthInput() first before calling InputtedDepthLayers().
Uzra #3
Posted 08 April 2013 - 03:42 PM
Oh my gosh I'm such a derper! I somehow forgot to call UserDepthInput(). Herp derp. I'll let ya know how it goes….
Uzra #4
Posted 08 April 2013 - 04:36 PM
Yup, it works. Thanks a lot! :D/>