Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Initialize a variable to keep track of the turtle's current state
- local turtleState = "waiting"
- while true do
- -- Check the turtle's current state
- if turtleState == "waiting" then
- -- Wait for redstone signal on the back to break a block
- while not rs.getInput("back") do
- sleep(0.1)
- end
- -- Check if there's a block in front of the turtle
- if turtle.detect() then
- -- Set the turtle's state to "breaking"
- turtleState = "breaking"
- -- Attempt to dig the block
- turtle.dig()
- -- Wait for a few seconds to give the turtle time to break the block
- sleep(3)
- -- Check if the block was successfully broken
- if not turtle.detect() then
- print("Block broken!")
- -- Set the turtle's state to "placing"
- turtleState = "placing"
- else
- print("Failed to break the block.")
- -- Set the turtle's state back to "waiting"
- turtleState = "waiting"
- end
- else
- print("No block to break.")
- end
- elseif turtleState == "breaking" then
- -- Wait for redstone signal on the back to place a block
- while not rs.getInput("back") do
- sleep(0.1)
- end
- -- Check if the turtle has a block to place
- if turtle.getItemCount(1) > 0 then
- -- Wait for a redstone signal on the back before placing the block
- while not rs.getInput("back") do
- sleep(0.1)
- end
- -- Place a block in front of the turtle
- turtle.place()
- print("Block placed.")
- -- Set the turtle's state back to "waiting"
- turtleState = "waiting"
- else
- print("No block to place.")
- -- Set the turtle's state back to "waiting"
- turtleState = "waiting"
- end
- elseif turtleState == "placing" then
- -- Wait for redstone signal on the back to break a block
- while not rs.getInput("back") do
- sleep(0.1)
- end
- -- Check if there's a block in front of the turtle
- if turtle.detect() then
- -- Set the turtle's state to "breaking"
- turtleState = "breaking"
- -- Attempt to dig the block
- turtle.dig()
- -- Wait for a few seconds to give the turtle time to break the block
- sleep(3)
- -- Check if the block was successfully broken
- if not turtle.detect() then
- print("Block broken!")
- else
- print("Failed to break the block.")
- end
- -- Set the turtle's state back to "placing"
- turtleState = "placing"
- else
- print("No block to break.")
- -- Set the turtle's state back to "placing"
- turtleState = "placing"
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment