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

'For' limit must be a number

Started by MrNorth, 22 January 2017 - 08:19 AM
MrNorth #1
Posted 22 January 2017 - 09:19 AM
So, basically I already tried to make a quaryy program, which actually worked, and right now I'm trying to make a farming program. The issue I'm having concerns the first lines of the farming sequence.

–Place turtle in rightmost position 1, 1, 1
–Declare dimensions
tArgs={…}
tVar={x, z}
for i=1, 2 do
tVar=tonumber(tArgs)
end

–forward mining
function turtle.advance(dist)
for Tz=1, dist do
turtle.dig()
turtle.forward()
end
end

–make turns
function turtle.uturnLeft()
turtle.turnLeft()
turtle.dig()
turtle.forward()
turtle.turnLeft()
end

function turtle.uturnRight()
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.turnRight()
end

–real farming
print("harvesting")
for Tx=1, x do –error line
turtle.advance(z-1)
if Tx%2>0 then
turtle.uturnRight()
else
turtle.uturnLeft()
end
end

It tells me "'for' limit must be a number". I checked with type() function, and tVar are actually numbers.
What do I do now? What am i getting wrong?
Thanks everyone
Admicos #2
Posted 22 January 2017 - 09:32 AM
is "x" a number?
Lupus590 #3
Posted 22 January 2017 - 03:22 PM
in the second line of –real farming you don't define x anywhere so lua uses nil which is not a number
Dog #4
Posted 22 January 2017 - 03:50 PM
To build a little on what's been said,with a little repeat and some emphasis - you're using x in your real farming for loop but you never define x. You do define tVar[1] and tVar[2]. If those are the values you want to use, then those are the variables you will have to use. Using type on your tVars will not tell you anything about x, you need to use type(x) to see what x is.
Tazkazz #5
Posted 22 January 2017 - 06:35 PM
I'd suggest changing

--Place turtle in rightmost position 1, 1, 1
--Declare dimensions
tArgs={...}
tVar={x, z}
for i=1, 2 do
tVar[i]=tonumber(tArgs[i])
end
to

--Place turtle in rightmost position 1, 1, 1
--Declare dimensions
tArgs={...}
x = tonumber(tArgs[1])
z = tonumber(tArgs[2])
MrNorth #6
Posted 23 January 2017 - 11:25 PM
I see. Thank you guys, I had already come to Tazkazz solution but it's good to know.