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

[Help][Error] 'for' limit must be a number

Started by oneupe1even, 18 March 2013 - 07:55 AM
oneupe1even #1
Posted 18 March 2013 - 08:55 AM
Here is my code:

-- Area Flatten Script 1.0 by oneupe1even of OmegaTEK Industries on the OniTech server
-- Variables
local tArgs = {...}
if #tArgs == 0 then
  print("Usage: flat [length] [width]")
  return
else
  local length = tonumber(tArgs[1])
  local width = tonumber(tArgs[2])
end
local turnFlag = true
-- Functions
local function tFuel(amount) -- By Guude
  if turtle.getFuelLevel() < 5 then
	turtle.select(16)
	turtle.refuel(amount)
	turtle.select(1)
  end
end
local function digMove()
  if turtle.detect() then
	repeat
	  turtle.dig()
	  sleep(.25)
	until turtle.detect() == false
	tFuel(1)
	turtle.forward()
  else
	tFuel(1)
	turtle.forward()
  end
end
local function checkTop()
  if turtle.detectUp() then
	repeat
	  tFuel(1)
	  repeat
		turtle.digUp()
		sleep(.25)
	  until turtle.detectUp() == false
	  turtle.up()
	until turtle.detectUp() == false
	repeat
	  tFuel(1)
	  turtle.down()
	until turtle.detectDown()
  end
end
local function uTurn()
  for i = 1, 2 do
	turtle.turnRight()
  end
end
local function mineLine()
  for i = 1, length do
	tFuel()
	digMove()
	checkTop()
  end
end
-- Main Script
for i = 1, width do
  mineLine()
  if turnFlag then
	turtle.turnRight()
	digMove()
	turtle.turnRight()
	turnFlag = false
  else
	turtle.turnLeft()
	digMove()
	turtle.turnLeft()
	turnFlag = true
  end
end

And here is the pastebin link: http://pastebin.com/qpwwTBmM

This is the error I get:


When I use 'width' in that for loop on line 74, it tells me that it's not a number. I even used tonumber() at the start of the code to make sure it was.

What am I doing wrong?
ikke009 #2
Posted 18 March 2013 - 08:58 AM
width is nil because you declared it a local variable inside your first if statement :)/>
so at line 12 delete the word local
remiX #3
Posted 18 March 2013 - 09:00 AM
Change
if #tArgs == 0 then
  print("Usage: flat [length] [width]")
  return
else
  local length = tonumber(tArgs[1])
  local width = tonumber(tArgs[2])  
end

to
if #tArgs == 0 then
  print("Usage: flat [length] [width]")
  return
end
  local length = tonumber(tArgs[1])
  local width = tonumber(tArgs[2])  

It's doing that because you define width and length as local in the if block, that means it is only usable within that block.
oneupe1even #4
Posted 18 March 2013 - 09:18 AM
Thanks everybody, there were a few other misc. errors that I ironed out myself.