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

For loop keeps going

Started by americanbeast, 14 October 2014 - 03:37 PM
americanbeast #1
Posted 14 October 2014 - 05:37 PM
I isolated the problem but still can't see the problem with it, the turtle keeps going forward an extra 3-7 blocks randomly whether its mining anything or not. I did a little searching on Google and as far as I can see the repeat and for are written correctly it just doesn't work correctly.


function BlockTest()
   if turtle.forward() == false then
	  repeat
	  turtle.dig()
	  sleep(0.5)
	  until turtle.forward() == true
   end
end
for n = 1,10,1 do --[[ I added the last 1 just to rule out any chance that it was the cause ]]--
   turtle.dig()
   turtle.forward()
   BlockTest()
end
Cranium #2
Posted 14 October 2014 - 06:31 PM
Well each time you call turtle.forward() it will try to move forward. Even if you're just checking to see that it can move.
So each time you're in a loop, it first digs, moves, then calls BlockTest (which first moves, and the repeat loop will move a third time)
Evil_Bengt #3
Posted 15 October 2014 - 07:54 AM
try to just use something like:


function move()
repeat
  turtle.dig()
until turtle.forward()
end
for n = 1, 10 do
move()
end

Though I don't know if that's what you're looking for but it will at least work….