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

[Solved] While loop not working for turtles

Started by maaatin, 04 August 2012 - 07:29 PM
maaatin #1
Posted 04 August 2012 - 09:29 PM
I'm trying to make a turtle that digs up until it gets x number of blocks in its first slot. This is the code I have:


local Var= turtle.getItemCount(1)
while Var<20 do
turtle.dig()
end
if Var>=20 then print("NO!")
end

When there are less than 20 blocks in slot 1 at the start of the program, it digs, but it keeps going, past 20 blocks. If it starts with 20 or more, it prints "NO!" like it should and doesn't dig. Anyone know why this is happening? I am placing blocks in front of it so it doesn't run off.
MysticT #2
Posted 04 August 2012 - 09:34 PM
The variable Var is never updated, so it always contains the same value and the loop never ends (if it starts).
You can add a line to update the variable, like:

Var = turtle.getItemCount(1)
But the best way would be like this:

while turtle.getItemCount(1) < 20 do
  turtle.dig()
end
maaatin #3
Posted 04 August 2012 - 09:45 PM
The variable Var is never updated, so it always contains the same value and the loop never ends (if it starts).
You can add a line to update the variable, like:

Var = turtle.getItemCount(1)
But the best way would be like this:

while turtle.getItemCount(1) < 20 do
  turtle.dig()
end

I tried this, and it worked like a charm! Thanks! :P/>/>