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

Problem with if statements

Started by GaryIsHairy, 15 July 2013 - 04:11 PM
GaryIsHairy #1
Posted 15 July 2013 - 06:11 PM
This mining turtle program should mine an area out, however I'm having a problem with one of the if statements, it should change x so the turtle alternates from turning left to turning right every 10 blocks it mines, but instead x stays at one so the turtle goes in circles.


local x = 1
for c = 1, 10, 1 do
print(x)

if x == 1 then
turtle.turnLeft()
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.turnLeft()
end
if x == 2 then
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.turnRight()
end
for x = 1, 10 , 1 do
turtle.dig()
turtle.forward()
turtle.digUp()
end
if x == 1 then
x = 2
end
if x == 2 then
x = 1
end
end
Edited by
Lyqyd #2
Posted 15 July 2013 - 06:26 PM
Split into new topic.

The problem is that you first set x to two if it was equal to one, then immediately set it to one if it is equal to two! Try using an else instead of two if blocks:


if x == 1 then
  x = 2
else
  x = 1
end
GaryIsHairy #3
Posted 15 July 2013 - 06:36 PM
Oh, lol, it works, thanks for the help.