←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft

»

'For' limit must be a number

MrNorth's Photo MrNorth 22 Jan 2017

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[i]=tonumber(tArgs[i])
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[i] are actually numbers.
What do I do now? What am i getting wrong?
Thanks everyone
Quote

Admicos's Photo Admicos 22 Jan 2017

is "x" a number?
Quote

Lupus590's Photo Lupus590 22 Jan 2017

in the second line of --real farming you don't define x anywhere so lua uses nil which is not a number
Quote

Dog's Photo Dog 22 Jan 2017

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.
Quote

Tazkazz's Photo Tazkazz 22 Jan 2017

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])
Quote

MrNorth's Photo MrNorth 23 Jan 2017

I see. Thank you guys, I had already come to Tazkazz solution but it's good to know.
Quote