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

Basic Straightline Turtle Loop

Started by Robinlemon, 05 April 2014 - 07:02 AM
Robinlemon #1
Posted 05 April 2014 - 09:02 AM
So basicaly im making a turtle that asks me how many block do i want to go and if i put 20 it will go forward 20 blocks and place blocks underneath it as it goes, pretty basic right!

I started using a for loop with variable but that dint work so i tried a while loop and it seems that hasn't worked either so now its down to those ninja coding experts to try and help me!

:11:end expected (to close if at line 9)

Any help Much Appreciated!


print("Robin's Straight Line Program V1.0")
print("How long do you want your line to be?")
x = read()

while true do
   turtle.forward()
   turtle.placeDown()
   x = x-1
   if (x == 0) then
	   break
	   print("Finished!")
end
Edited on 05 April 2014 - 07:09 AM
Bomb Bloke #2
Posted 05 April 2014 - 09:41 AM
read() returns a string ("text") value, not a number. You can convert it with tonumber(), eg:

x = tonumber( read() )

Note that tonumber() returns nil if it can't do the conversion (eg you pass it a string along the lines of "cabbage").

Here's a couple of ways this could be done:

print("Robin's Straight Line Program V1.0")
print("How long do you want your line to be?")
x = tonumber( read() )

while true do
   turtle.forward()
   turtle.placeDown()
   x = x - 1
   if (x == 0) then
           break
           print("Finished!")
   end  -- You were missing this "end"!
end

print("Robin's Straight Line Program V1.0")
print("How long do you want your line to be?")

local x
while not x do   -- A variable with no value counts as "false", whereas a variable with a value counts as "true".
   x = tonumber( read() )
end

for i = 1,x do
   turtle.forward()
   turtle.placeDown()
end

print("Finished!")
johnneijzen #3
Posted 05 April 2014 - 09:57 AM
here my way so doing this



Local Dist = 0

print("Robin's Straight Line Program V1.0")
print("How long do you want your line to be?")
input = io.read()
Dist = tonumber(input)
for i = 1, dist do
   turtle.forward()
   turtle.placeDown()
end


But you want it to dig and place here



Local Dist = 0

print("Robin's Straight Line Program V1.0")
print("How long do you want your line to be?")
input = io.read()
Dist = tonumber(input)
for i = 1, dist do
  if turtle.detect() then
    turtle.dig()
  end
  turtle.forward()
  turtle.select(1)
  turtle.placeDown()
end
 
Edited on 05 April 2014 - 07:59 AM
theoriginalbit #4
Posted 05 April 2014 - 09:59 AM
here my way so doing this

SpoilerLocal Dist = 0

print("Robin's Straight Line Program V1.0")
print("How long do you want your line to be?")
input = io.read()
Dist = tonumber(input)
for i = 1, dist do
turtle.forward()
turtle.placeDown()
end

But you want it to dig and place here

SpoilerLocal Dist = 0

print("Robin's Straight Line Program V1.0")
print("How long do you want your line to be?")
input = io.read()
Dist = tonumber(input)
for i = 1, dist do
if turtle.detect() then
turtle.dig()
end
turtle.forward()
turtle.select(1)
turtle.placeDown()
end
use [code][/code] tags… also you have errors in your code, our purpose here in Ask a Pro is to solve problems, not create more.
johnneijzen #5
Posted 05 April 2014 - 11:46 AM
sorry