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

Error in mining program

Started by swg18, 17 October 2013 - 09:28 PM
swg18 #1
Posted 17 October 2013 - 11:28 PM
I keep recieving this error
Mine:67: 'for' limit must be a number
I have looked at line 67 and I am not sure how to fix this problem.


-- Mining Turtle
function checkFuel()
  print ("Checking fuel level")
  if turtle.getFuelLevel() <= 10 then
	print ("Re-fueling")
	turtle.select(13)
	turtle.refuel(1)
  else
	print ("No fuel needed")
  end
end

function turnAround()
  turtle.turnRight()
  turtle.turnRight()
end

function dig()
   if turtle.detect() == true then
		repeat
			turtle.dig()
			sleep(0.80)
		until turtle.detect() == false
end
	turtle.forward()
	if turtle.detectUp() then
		repeat
			turtle.digUp()
			sleep(0.80)
		until turtle.detectUp() == false
end
	turtle.turnLeft()
	if turtle.detect() == true then
		repeat
			turtle.dig()
			sleep(0.80)
		until turtle.detect() == false
end
	turtle.up()
	if turtle.detect() == true then
		repeat
			turtle.dig()
			sleep(0.80)
		until turtle.detect() == false
end
	turtle.turnRight()
	turtle.turnRight()
	if turtle.detect() == true then
		repeat
			turtle.dig()
			sleep(0.80)
		until turtle.detect() == false
end
	turtle.down()
	if turtle.detect() == true then
		repeat
			turtle.dig()
			sleep(0.80)
		until turtle.detect() == false
end
	turtle.turnLeft()
   end

function tunnel
  local distance = 0
  for index = 1,givenLength do
	dig()
	if turtle.forward() then
	  distance = distance + 1
	end
	 turtle.select(14)
	turtle.placeDown()

	if distance == 10 then
	  turtle.select(15)
	  print("Placing torch...")
	  turnAround()
	  turtle.placeDown()
	  turnAround()
	  distance = 0
	  checkFuel()
	end
if turtle.getItemCount(12) ~= 0 then
   turtle.digDown()
  print("Placing Chest...")
   turtle.select(16)
   turtle.placeDown()
   for i = 1,12 do
	  turtle.select(i)
	  turtle.dropDown()
		end
	end
end

	turtle.up()
	for index = 1,givenLength do
	  turtle.back()
	end
	turtle.down()
end

print("Input tunnel length:")
local length = read()
local fuelNeeded = length*5
fuelNeeded = fuelNeeded+length
local floorNeeded = length/5
local torchNeeded = length/10
local chestNeeded = length/25
local fuelLevel = turtle.getFuelLevel()
local floorLevel = turtle.getItemCount(14)
local torchLevel = turtle.getItemCount(15)
local chestLevel = turtle.getItemCount(16)
if fuelLevel < fuelNeeded then
print ("Please fuel into slot 13")
repeat
  select(13)
  sleep(0.8)
  turtle.refuel(10)
  fuelLevel = turtle.getFuelLevel()
until fuelLevel >= fuelNeeded
end
if floorLevel < floorNeeded then
print ("Please insert non-wood floor block into slot 14")
repeat
  sleep(0.8)
  floorLevel = turtle.getItemCount(14)
until floorLevel > floorNeeded
end
if torchLevel < torchNeeded then
print("Please insert torches into slot 15")
repeat
  sleep(0.8)
  torchLevel = turtle.getItemCount(15)
until torchLevel > torchNeeded
end
if chestLevel < chestNeeded then
print("Please insert chests into slot 16")
repeat
  sleep(0.8)
  chestLevel = turtle.getItemCount(16)
until chestLevel > chestNeeded
end
print("Resources aquired, Starting excavation...")
tunnel(givenLength)
print("The tunnel has been excavated!")
Lyqyd #2
Posted 19 October 2013 - 09:03 PM
Split into new topic.
Bomb Bloke #3
Posted 19 October 2013 - 09:48 PM
Line 67: for index = 1,givenLength do

You don't at any point define "givenLength" as anything. If you changed line 144 from tunnel(givenLength) to tunnel(length), and 64 from function tunnel to function tunnel(givenLength), then the variable would be passed to the function under that name. Bearing in mind that the "length" variable would be available there anyways, since that's local to the whole program, not local to any specific function.

You may also wish to change 103 to local length = tonumber(read()), as read() alone returns strings.
swg18 #4
Posted 27 October 2013 - 05:07 PM
I'm sorry it took so long to respond I have been busy with school work. I think I made the changes you suggested. The turtle will start mining now but it does not place torches at 10 block intervals and it moves 2 blocks at a time I am not receiving any error messages.
Here is the corrected code ;

-- Strip Mining Turtle

function checkFuel()
  print ("Checking fuel level")
  if turtle.getFuelLevel() <= 10 then
    print ("Re-fueling")
    turtle.select(13)
    turtle.refuel(1)
  else
    print ("No fuel needed")
  end
end

function turnAround()
  turtle.turnRight()
  turtle.turnRight()
end

function dig()
   if turtle.detect() == true then
	    repeat
		    turtle.dig()
		    sleep(0.80)
	    until turtle.detect() == false
	    end --if
    turtle.forward()
    if turtle.detectUp() then
	    repeat
		    turtle.digUp()
		    sleep(0.80)
	    until turtle.detectUp() == false
end --if
    turtle.turnLeft()
    if turtle.detect() == true then
	    repeat
		    turtle.dig()
		    sleep(0.80)
	    until turtle.detect() == false
end --if
    turtle.up()
    if turtle.detect() == true then
	    repeat
		    turtle.dig()
		    sleep(0.80)
	    until turtle.detect() == false
end--if
    turtle.turnRight()
    turtle.turnRight()
    if turtle.detect() == true then
	    repeat
		    turtle.dig()
		    sleep(0.80)
	    until turtle.detect() == false
end
    turtle.down()
    if turtle.detect() == true then
	    repeat
		    turtle.dig()
		    sleep(0.80)
	    until turtle.detect() == false
end
    turtle.turnLeft()
   end

function tunnel(length)
  local distance = 0
  for index = 1, length do
    dig()
    if turtle.forward() then
	  distance = distance + 1
    end
	 turtle.select(14)
    turtle.placeDown()

    if distance == 10 then
	  turtle.select(15)
	  print("Placing torch...")
	  turnAround()
	  turtle.placeDown()
	  turnAround()
	  distance = 0
	  checkFuel()
    end

	    if turtle.getItemCount(12) ~= 0 then
			    turtle.digDown()
			    print("Placing Chest...")
			    turtle.select(16)
			    turtle.placeDown()
			    for i = 1,12 do
					    turtle.select(i)
					    turtle.dropDown()
	    end
    end
end
  
    turtle.up()
    for index = 1,length do
	  turtle.back()
    end
    turtle.down()
end

print("Input tunnel length:")
local length = tonumber(read())
local fuelNeeded = length*5
fuelNeeded = fuelNeeded+length
local floorNeeded = length/5
local torchNeeded = length/10
local chestNeeded = length/25
local fuelLevel = turtle.getFuelLevel()
local floorLevel = turtle.getItemCount(14)
local torchLevel = turtle.getItemCount(15)
local chestLevel = turtle.getItemCount(16)

if fuelLevel < fuelNeeded then
	    print ("Please fuel into slot 13")
	    repeat
			    select(13)
			    sleep(0.8)
			    turtle.refuel(10)
			    fuelLevel = turtle.getFuelLevel()
	    until fuelLevel >= fuelNeeded
end

if floorLevel < floorNeeded then
	    print ("Please insert non-wood floor block into slot 14")
	    repeat
			    sleep(0.8)
			    floorLevel = turtle.getItemCount(14)
	    until floorLevel > floorNeeded
end

if torchLevel < torchNeeded then
	    print("Please insert torches into slot 15")
	    repeat
			    sleep(0.8)
			    torchLevel = turtle.getItemCount(15)
	    until torchLevel > torchNeeded
end

if chestLevel < chestNeeded then
	    print("Please insert chests into slot 16")
	    repeat
			    sleep(0.8)
			    chestLevel = turtle.getItemCount(16)
	    until chestLevel > chestNeeded
end
print("Resources aquired, Starting excavation...")
tunnel(length)
print("The tunnel has been excavated!")
Bomb Bloke #5
Posted 27 October 2013 - 06:02 PM
There's never a need to check if "something == true". Your "if" statements are already checking for that. Eg, "if turtle.detect() then" is perfectly valid code. "turtle.detect() == false" is better written as "not turtle.detect()".

There's also no need to wrap an "if" statement around a conditional loop. Eg, this:

if turtle.detect() == true then
            repeat
                    turtle.dig()
                    sleep(0.80)
            until turtle.detect() == false
            end --if

… can be replaced with just this:

while turtle.detect() do
            turtle.dig()
            sleep(0.80)
end

You may even consider slapping that into a little function all of its own so you can shrink down your main dig() function further.

Every time you call dig() your turtle digs out the block ahead of it and moves forward one (without incrementing "distance"). Once that function finishes you try to move forward again, and if that movement succeeds, you do add one to "distance": Only odds are it won't, because more likely then not there's going to be a solid rock wall in front of it.

In short, don't try to move forward after calling dig(): Just increment "distance".

Bear in mind that every movement forward consumes three fuel (as the dig function has the turtle go forward, up, then down again). Hence after ten calls of the dig function you'll've used at least thirty fuel units, and since that's how often you're checking your fuel level you should alter your checkFuel() function accordingly.