Turtle script problem?

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here
  • FTB will be shutting down this forum by the end of July. To participate in our community discussions, please join our Discord! https://ftb.team/discord

tyessen

New Member
Jul 29, 2019
9
0
0
ok, so i created a script to cut down a 1x1 tree, and whenever i try to run it, the turtle won't do anything besides go back to the terminal to let me type something else in.

Code:
function plant()
turtle.select(1) --select sapling
turtle.place()
turtle.select(2) --select bonemeal
turtle.place()
turtle.select(3) --select bonemeal
turtle.place()
turtle.select(4) --select bonemeal
turtle.place()
turtle.select(5) --select bonemeal
turtle.place()
end
 
function cut()
turtle.dig()
turtle.forward()
 
while turtle.detectUp() == true do
  turtle.digUp()
  turtle.up()
end
end
 
function down()
while turtle.detectUp() == false do
  turtle.down()
  turtle.suck()
end
end

So, when i try to run it nothing happens. i'm still learning LUA, so there are probably some things wrong here. can anybody help me out?

oh, and it'd be awesome if someone showed me how to loop this so it runs 24/7 :)
 

Bomb Bloke

New Member
Jul 29, 2019
612
0
0
You've defined three new functions - plant(), cut() and down() - but functions don't fire off on their own. You have to call them.

At the end of your script, add:

Code:
while true do -- "True" is always "true", so this leads in to an infinite loop
plant()
cut()
down()
turtle.back()
end

Note that this code doesn't go inside a function, meaning that it'll run automatically when the script runs. While you could do away with your functions altogether, and dump all your code into this while block, you'd find that'd make your code harder to read (especially if you've been away from it for a while), meaning it'll take you longer to work out how to expand it.

The extra turtle movement call is to account for the fact that the turtle had to move forward a block after growing the tree. Your next steps should be looking into inventory management methods so's the turtle can drop off its loot into a chest or something and keep itself stocked with saplings and bonemeal.
 

tyessen

New Member
Jul 29, 2019
9
0
0
You've defined three new functions - plant(), cut() and down() - but functions don't fire off on their own. You have to call them.

At the end of your script, add:

Code:
while true do -- "True" is always "true", so this leads in to an infinite loop
plant()
cut()
down()
turtle.back()
end

Note that this code doesn't go inside a function, meaning that it'll run automatically when the script runs. While you could do away with your functions altogether, and dump all your code into this while block, you'd find that'd make your code harder to read (especially if you've been away from it for a while), meaning it'll take you longer to work out how to expand it.

The extra turtle movement call is to account for the fact that the turtle had to move forward a block after growing the tree. Your next steps should be looking into inventory management methods so's the turtle can drop off its loot into a chest or something and keep itself stocked with saplings and bonemeal.
ohhh... i think i get it. the program isn't doing anything from what i already have, it's just defining functions, right? the part you gave me just runs them continuously since true == true.

and you are right about the inventory management, but i am going to try a few more simple scripts first before i get into inventory work. thanks :)
 

Silent_007

New Member
Jul 29, 2019
302
0
0
To make your script run forever (once you've made the tweaks suggested above) simply do edit startup and write shell.run("your script name here") and save that. The startup program runs anytime the turtle is rebooted or the chunk it is in gets reloaded, so that should always run. Keep in mind though that if the turtle gets unloaded while in the middle of its program it will forget where it was, and start from the beginning again once it gets reloaded.

Also, I think you want to alter your down() function to use detectDown() instead of detectUp(). Otherwise that loop will run indefinitely until you place a block above the turtle...
 
  • Like
Reactions: tyessen

tyessen

New Member
Jul 29, 2019
9
0
0
Also, I think you want to alter your down() function to use detectDown() instead of detectUp(). Otherwise that loop will run indefinitely until you place a block above the turtle...
if i used detectDown(), then as soon as the turtle moved up a block there would be a space below it where it would move right back to the ground. then there would be a space above it, and it would move up, then back down... i tested this without the loop and it ran perfectly, unless adding the loop would do something to change things up?

@Bomb Bloke, i added it and ran it, and it only went once and stopped at the ground (didn't even move back a block). i tried repeat (functions) until 1 > 2 (since 1 will never be greater than 2), and that produced the same result. is there a problem with one of my functions?

EDIT: oh... so that's why it... stopped... :/
 

Bomb Bloke

New Member
Jul 29, 2019
612
0
0
You might be confusing it with the cut() function, which indeed should use detectUp() because it's trying to move the turtle up. But the down() function is going the other way, and so needs to use detectDown() to avoid an infinite loop (no matter how far the turtle goes down there'll never be a block above it).
 
  • Like
Reactions: tyessen

tyessen

New Member
Jul 29, 2019
9
0
0
You might be confusing it with the cut() function, which indeed should use detectUp() because it's trying to move the turtle up. But the down() function is going the other way, and so needs to use detectDown() to avoid an infinite loop (no matter how far the turtle goes down there'll never be a block above it).
ok, so he was right :D i guess i was getting confused over what commands ran over what and thought the detectUp would take priority... well anways, it's running in a loop now. thanks so much :)