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

[LUA][Solved] More Turtle control issues AKA: lrn2use 'end'

Started by rzp1029, 23 October 2012 - 06:27 PM
rzp1029 #1
Posted 23 October 2012 - 08:27 PM
So I posted on here a day or two ago and got some help with my turtle receiver code, as it kept throwing an error due to some mis-typed code, and it was working… I thought. I didn't test it very thoroughly at the time (read: hardly any) because I had some other obligations to attend to. I got back to it about an hour ago, and realized that they will excavate in synchronous, and will go upward, but no other direction. I suspect this is an issue with my for loops, but am honestly not sure.
Computer Broadcasting Code

while true do
rednet.open("top")
print("Opened modem...")
print("What would you like the turtle to do?")
input = string.lower(read())
if input == "excavate" then
  print("Broadcasting excavate to all turtles.")
  rednet.broadcast("excavate")
elseif input == "go left" then
  print("Relocating turtles...")
  rednet.broadcast("left")
elseif input == "go right" then
  print("Relocating turtles...")
  rednet.broadcast("right")
elseif input == "go up" then
  print("Relocating turtles...")
  rednet.broadcast("up")
elseif input == "go down" then
  print("Relocating turtles...")
  rednet.broadcast("down")
elseif input == "go forward" then
  print("Relocating turtles...")
  rednet.broadcast("forward")
elseif input == "go back" then
  print("Relocating turtles...")
  rednet.broadcast("back")
end
rednet.close("top")
print("Broadcast complete.")
end
Turtle Receiver Code

rednet.open("right")
	while true do
		  event, id, text = os.pullEvent()
		  if event == "rednet_message" then
			if text == "excavate" then
									shell.run(""..text, "5")
			elseif text == "up" then
									for x = 0, 5 do
									if turtle.detectUp() then
													turtle.digUp()
									end
													x=x+1
													turtle.up()
									end
									end
			elseif text == "right" then
									turtle.turnRight()
									for x = 0, 5 do
									if turtle.detect() then
													turtle.dig()
									end
													turtle.forward()
													x=x+1
									end
									turtle.turnLeft()
			elseif text == "left" then
									turtle.turnLeft()
									for x = 0, 5 do
									if turtle.detect() then
													turtle.dig()
									end
													turtle.forward()
													x=x+1
									end
									turtle.turnRight()
			elseif text == "down" then
									for x = 0, 5 do
									if turtle.detectDown() then
													turtle.digDown()
									end
													turtle.down()
													x=x+1
									end
	  
			elseif text == "back" then
									turtle.turnRight()
									turtle.turnRight()
									for x = 0, 5 do
									if turtle.detect() then
													turtle.dig()
									end
													turtle.forward()
													x=x+1
									end
	  
									turtle.turnLeft()
									turtle.turnLeft()
   elseif text == "forward" then
		 for x = 0, 5 do
		 if turtle.detect() then
			 turtle.dig()
		 end
			 turtle.forward()
			 x=x+1
		 end
	end
end
remiX #2
Posted 23 October 2012 - 08:37 PM
That indentation.

EDIT: I looked over your code and i think I know why. You have one too many 'end' just before the it checks if the text is right. Remove.


elseif text == "up" then
  for x = 0, 5 do
    if turtle.detectUp() then
      turtle.digUp()
    end
    x=x+1
    turtle.up()
  end
end    -- remove this one
elseif text == "right" then

rzp1029 #3
Posted 23 October 2012 - 08:43 PM
Top is mine, bottom is the code I was assisted with.

Yep. That fixed it, just had to move that end to the very end of my code. Seems my mortal nemesis in Lua shall be end.
remiX #4
Posted 23 October 2012 - 08:45 PM
Did it work?
rzp1029 #5
Posted 23 October 2012 - 08:52 PM
Sure did! Thanks!