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

Expected number error

Started by Birdgeek3, 03 January 2013 - 10:47 AM
Birdgeek3 #1
Posted 03 January 2013 - 11:47 AM
Here again with a script that will dig a 2x1 mine shaft and place a torch and fuel if need be

Whever I run it I get the error

"turtle:18:number expected"

I find this odd because line 18 is just function declaration.

Here is the code

--Variables
local tArgs = { ... }
local togo = tonumber (tArgs[1])
local num = 0
--Function
function tfuel(amount)
  local fuelLevel = turtle.getFuelLevel()
  if fuelLevel < 50 then
    turtle.select(16)
    turtle.refuel(amount)
    turtle.select(1)
  end
end
function torch()
  if num == 10 then
    turtle.select(15)
    turtle.placeUp()
    turtle.select(1)
    num = 0
  end
end
function detect()
turtle.detect()
    if result == true then
	  turtle.dig()
    end
  end
 
function layer()
  turtle.dig()
  os.sleep(.25)
  detect()
  turtle.up()
  turtle.dig()
  os.sleep(.25)
  detect()
  turtle.down()
  turtle.forward()
end
 
--Main Script
 
  repeat
    tfuel()
    layer()
    torch()
    num = num + 1
    togo = togo - 1
  until togo == 0
OmegaVest #2
Posted 03 January 2013 - 11:59 AM
…Is the program called turtle? If not, the problem is with one of your turtle api calls. And I think it is turtle.refuel(), in the tfuel function, which is attempting to pass a nil variable, which counts as a parameter, to a function looking for a number. Since it is a valid variable, it assumes you are trying to pass a number. I think this is the problem.


Second query, does the program run correctly if you comment out that line (and any dependent lines)? That's my way of figuring out if a line is actually bad, and I generally assume everyone does this. I consciously realize this is not always the case, but habit is hard to break. And I just found my signature, I think.
Birdgeek3 #3
Posted 03 January 2013 - 12:08 PM
…Is the program called turtle? If not, the problem is with one of your turtle api calls. And I think it is turtle.refuel(), in the tfuel function, which is attempting to pass a nil variable, which counts as a parameter, to a function looking for a number. Since it is a valid variable, it assumes you are trying to pass a number. I think this is the problem.


Second query, does the program run correctly if you comment out that line (and any dependent lines)? That's my way of figuring out if a line is actually bad, and I generally assume everyone does this. I consciously realize this is not always the case, but habit is hard to break. And I just found my signature, I think.
The program is called shaftDigger and that same refuel function works in every other script I run. It is simply copy pasted into each one for basic fueling.
remiX #4
Posted 03 January 2013 - 12:33 PM
Try adding an argument when you call tfuel(). Try tfuel(1) because as OmegaVest said, it's trying to refuel a nil value, and it needs a number, number expected.