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

Attempt to compare nil with a number

Started by coarse, 18 March 2014 - 10:17 PM
coarse #1
Posted 18 March 2014 - 11:17 PM
Whenever I try to input the number of tunnels (var a) I want, the program replies with
:21: attempt to compare to nil
even though i have set
local a = tonumber(read())
. The previous versions of this program did not have these errors at all.

The code:
Spoiler

!
function inputLTDT()
    print("How long should the tunnel be?")
    local x = tonumber(read())
    print("After how many blocks should the turtle put torches?")
    local y = tonumber(read())
    print ("How many tunnels?")
    local a = tonumber(read())
end
function inputDirection()
    print ("Going which direction from the 1st tunnel? (left or right)")
    local b = read()
end
function inputGap()
    print ("Gap between tunnels? Default is 3")
    local c = tonumber(read())
end
function checkA()
    if(a > 2) then
        inputDirection()
        checkB()
    elseif(a == 1) then
        finalCheck()
    else
        print ("Invalid selection.")
        inputTunnel()
    end
end
function checkB()
    if(b == "left" or "right") then
        inputGap()
        checkC()
    else
        print ("Invalid selection.")
        inputDirection()
    end
end
function checkC()
    if(c >= 1) then
    elseif(c == 0) then
        print ("Invalid selection. Minimum block gap is 1.")
        inputGap()
    else
        print ("Block gap set to default.")
        c = 3
        finalCheck()
    end
end
function input()
    inputLTDT()
    checkA()
end
function finalCheck()
    write ("The turtle will dig ")
    if(a == 1) then
        write ("a/an" .. x .."-blocks long tunnel.\n")
    elseif(a >= 2) then
        write (a .. x .."-blocks long tunnel with " .. c .. " blocks in between going to the ")
        if(b == "left") then
            write ("left.\n")
        elseif(b == "right") then
            write ("right.\n")
        end
    end
    write ("Torches will be placed every " .. y .. " blocks.\n")
    confirm()
end
function confirm()
    print ("Is this correct? (yes/no)")
    local d = read()
    if(d == "yes") then
        print("Place torches in slot 1 and Gravel in slot 2 then press any key to continue")
        os.pullEvent("char")
        print ("Program starting...")
        c = c+1
        local t = 0
        programStart()
    elseif(d == "no") then
        print ("Going back to input...")
        input()
    else
        print ("Invalid selection.")
        confirm()
    end
end
function programStart()
    if(a == 1) then
        mainProgram()
    elseif(a >= 2) then
     mainProgram()
     a = a-1
     for i = 1, a do
      changeTunnel()
      mainProgram()
     end
    end
end
function gravelCheck() --checks for gravel
    turtle.select(2)
    if(turtle.compare()==true) then --if it is gravel...
  repeat --...dig until there is no more gravel
   turtle.dig()
   sleep(0.5)
  until (turtle.compare()==false)
  end
end
function dig()
if((turtle.detect()==true) and (turtle.detectDown()==true)) then --if there is a block in front and below
  gravelCheck() --gravel check
  turtle.dig()
  turtle.digDown()
elseif((turtle.detect()==true) and (turtle.detectDown()==false)) then --if there is a block in front only
  gravelCheck() --gravel check
  turtle.dig()
elseif((turtle.detect()==false) and (turtle.detectDown()==true)) then -- if there is a block below only
  turtle.digDown()
end
end
function final() --so the end of the tunnel has no 1x1 gap at the top
        turtle.forward()
        turtle.digDown()
        turtle.back()
end
function torch() --checks when to place torch
if(t == y) then
  turtle.select(1)
  turtle.placeDown()
  t = 0
end
end
function mainProgram() --self-explanatory
for i = 1, x do
  dig()
  torch()
  turtle.forward()
  t = t+1
  p = i/x*100
  print (p .. "% completed...")
end
final()
print ("Done! Going home...")
for i = 1, x do
  turtle.back()
end
a = a-1
end
function changeTunnel() --moves to next tunnel
if(b == "left") then
  turtle.turnLeft()
elseif(b == "right") then
  turtle.turnRight()
end
for i = 1, c do
  turtle.forward()
end
if(b == "left") then
  turtle.turnRight()
elseif(b == "right") then
  turtle.turnLeft()
end
turtle.dig()
turtle.forward()
end
-- Program Start
input()
Lyqyd #2
Posted 18 March 2014 - 11:50 PM
You are declaring a as local to that function. When that function ends, a goes out of scope, so any attempts to use the variable a outside of that function will find that it is nil.
coarse #3
Posted 19 March 2014 - 12:01 AM
I see, thank you! I will edit this post to see if it now works. I have to go to school in a few minutes so I wont be back until 4 pm gmt+8