Jump to content




[Solved] While loop not working for turtles


  • You cannot reply to this topic
2 replies to this topic

#1 maaatin

  • New Members
  • 3 posts

Posted 04 August 2012 - 08: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.

#2 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 04 August 2012 - 08: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


#3 maaatin

  • New Members
  • 3 posts

Posted 04 August 2012 - 08:45 PM

View PostMysticT, on 04 August 2012 - 08:34 PM, said:

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/>





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users