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

Turtle lumberjack, Error"12"

Started by Robus, 19 May 2012 - 03:23 PM
Robus #1
Posted 19 May 2012 - 05:23 PM
I've made this program. It's a (simple) turtle lumberjack.
But if I run it, the turtle just says 12.

There's something wrong with this part, but I dont get what…..
Spoiler


else while not turtle.detect() do
turtle.back()
turtle.back()
sleep(2)
turtle.forward()
turtle.forward()

Here's the whole code:
Spoiler

term.clear()
term.setCursorPos(1,1)

x = 0
y = 0

while true do

if y == 0 then
turtle.up()
turtle.placeDown()
turtle.back()
y = y+1

if turtle.detect() then
y = y-1
turtle.dig()
turtle.digDown()

else while not turtle.detect() do
turtle.back()
turtle.back()
sleep(2)
turtle.forward()
turtle.forward()

while turtle.detectUp() do
turtle.digUp()
turle.up()
x = x+1
end

while x > 0 do
turtle.down()

end
end
end
end


sorry 4 bad english^^

Robus
Luanub #2
Posted 19 May 2012 - 09:17 PM
Your missing an end, i think its for this while loop


else while not turtle.detect() do


If you use proper coding habbits such as indenting your code these types of errors are relatively easy to find here's an example


if turtle.detect then
    turtle.dig()
    turtle.forward()
    if turtle.detectUp() then
	    turtle.digUp()
	    turtle.up()
    end
end

Also its better to use your vars as local instead of global so I would change

x = 0
y = 0


to

local x = 0
local y = 0
Robus #3
Posted 19 May 2012 - 10:08 PM
ok ty ;D
It works now….
but not as I want it too q.q
I want a loop, thats why I added the "repat" but its still not….looping :P/>/>

btw changed the code…..just 4 you <3
Spoiler


term.clear()
term.setCursorPos(1,1)

local x = 0
local y = 0

repeat

  while true do

	if y == 0 then
	   turtle.up()
	   turtle.placeDown()
	   turtle.back()
	   y = y+1

	  while not turtle.detect() do
		turtle.back()
		turtle.back()
		sleep(2)
		turtle.forward()
		turtle.forward()
	  end

	  else if turtle.detect() then
		turtle.dig()
		turtle.forward()
		turtle.digDown()
		y = y-1
	  end

	  while turtle.detectUp() do
		turtle.digUp()
		turtle.up()
		x = x+1
	  end

	  while x > 0 do
		turtle.down()
	  end
	  end
		end

until redstone.getInput("bottom")
Luanub #4
Posted 19 May 2012 - 10:25 PM
else if's are actually done as one word in lua, elseif. doing else if causes some problems.

This should work
Spoiler

term.clear()
term.setCursorPos(1,1)

local x = 0
local y = 0

repeat
while true do -- you probably want to remove this
    if y == 0 then
        turtle.up()
        turtle.placeDown()
        turtle.back()
        y = y+1
        while not turtle.detect() do
            turtle.back()
            turtle.back()
            sleep(2)
            turtle.forward()
            turtle.forward()
        end

    elseif turtle.detect() then
        turtle.dig()
        turtle.forward()
        turtle.digDown()
        y = y-1
    end
    while turtle.detectUp() do
        turtle.digUp()
        turtle.up()
        x = x+1
    end
    while x > 0 do
        turtle.down()
    end
end -- and this
until redstone.getInput("bottom")

The restone signal will probably not stop the loop however. the while true do loop will run until something stops it and due to that it will not reach the until statement. I would just remove it and the end for it. I've commented in the script
Robus #5
Posted 19 May 2012 - 11:12 PM
ok finally it works ;D
ty