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

'for i = 1,test do' doesn't work

Started by oWave, 28 June 2013 - 09:54 AM
oWave #1
Posted 28 June 2013 - 11:54 AM
I get the error: "for limit must be a number"
when I run this:

local function GetDrop()
  if bm == true then
    Drop = 14
  elseif bm == false then
    Drop = 15
  else
    DropInvalid()
  end
  toDrop = tonumber(Drop)
end
local function Drop()
  for i = 2,toDrop do
    turtle.select(i)
    turtle.dropDown()
  end
end

I already use tonumber() but it still says that.
Zudo #2
Posted 28 June 2013 - 12:04 PM
Why are you using tonumber? Your Drop variable already is a number!
oWave #3
Posted 28 June 2013 - 12:06 PM
I just tried it with tonumber to see if it works with this. But even without it gives me the error. :(/>
GopherAtl #4
Posted 28 June 2013 - 12:13 PM
running that will do absolutely nothing, because the code you shows us only declares functions, never calls them.

Please, everyone, post entire programs that don't work, not just the tiny snippets you think are the problem. If you knew what the problem was, you wouldn't be here asking for help!

If the program is too long, try taking the bits and running them in a separate, smaller program yourself, it can make it a lot easier to identify where the problems are.

That said, you're using Drop as a number in GetDrop, which means if you had called GetDrop first (as it appears you should, since GetDrop assigns toDrop), then the Drop function may well have been overwritten with the number 14 or 15, so an attempt to call Drop() will get you an attempt to call nil error.
oWave #5
Posted 28 June 2013 - 12:26 PM
Ok, I now just used print(Drop) and got nil. For some reason it didn't set Drop.
Fixed it now
oWave #6
Posted 28 June 2013 - 03:26 PM
Poorly the error is back again. The code:

local function ConvertInput()
  if bm == "y" or bm == "yes" then
	bm = true
	Drop = 14
  elseif bm == "n" or bm == "no" then
	bm = false
	Drop = 15
  end
bm1 = tostring(bm)
print("bm = " .. bm1)
print("Drop =" .. Drop)
end
Before this is a read() command: bm = read()
The last three things are for testing, but shows everything is fine. The next thing:

local function Drop()
  for i = 2,Drop do
	turtle.select(i)
	turtle.dropDown()
  end
end
(They run in that order. Between is nothing that modifies this)
Still getting the "'for' limit must be a number" error
I also set a print(Drop) before the Drop() function. It shows 14 or 15 like it should.
If you need the whole code for some reason: http://pastebin.com/pqz9MAMv (Waring: That's a big mess. Also it doesn't work yet, so don't run it)

I hope this time it's clear and that someone knows why this happens.
svdragster #7
Posted 28 June 2013 - 03:47 PM
You have called a var "Drop", you can't aditionally call function "Drop". You could call it "dropItem" or something.
oWave #8
Posted 28 June 2013 - 04:01 PM
Ohh.. ok.
This means if I have a function Drop(), the name Drop is reserved and can't be used as placeholder.
Good to know. Thank you!
ChunLing #9
Posted 28 June 2013 - 07:23 PM
Hey, +1 GopherAtl for spotting that too! And as kinda an apology for not noticing.