What am I missing? Turtle script

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here

Soulsbane96

New Member
Jul 29, 2019
30
0
0
Only my second script so I'm not all that adept at turtle coding. I've fixed some errors but now when running it I get
260ybzq.png


Code:
Code:
function check()
    turtle.select(reed)
    if turtle.compare() then
        turtle.up()
        if turle.compare() then
            turtle.dig()
            turtle.down()
            turtle.dig()
        else
            turtle.down()
        end
    end
end
 
print("I must be placed 1 block above bottom of the bottom left sugarcane plant in a 5x5 farm
 
(SWSWS x5) with 1 block of space on all sides")
print("I'd also be very appreciative if there was a chest under my starting position")
print("Please place Sugarcane into Slot 1")
local reed = turtle.getItemCount(1)
 
while true do
    check()
    turtle.turnLeft()
    turtle.forward()
    turtle.turnRight()
    turtle.forward()
    turtle.forward()
    turtle.turnrRight()
    check()
    turtle.turnLeft()
    turtle.forward()
    turtle.turnRight()
    check()
    turtle.turnLeft()
    turtle.forward()
    turtle.turnRight()
    check()
    turtle.turnLeft()
    turtle.forward()
    turtle.turnRight()
    check()
    turtle.turnLeft()
    turtle.forward()
    turtle.turnRight()
    turtle.forward()
    turtle.forward()
    turtle.turnRight()
    turtle.forward()
    turtle.turnLeft()
    check()
    turtle.turnRight()
    turtle.forward()
    turtle.turnLeft()
    check()
    turtle.turnRight()
    turtle.forward()
    turtle.turnLeft()
    check()
    turtle.turnRight()
    turtle.forward()
    turtle.turnLeft()
    check()
    turtle.turnRight()
    turtle.forward()
    turtle.turnLeft()
    check()
    turtle.turnRight()
    turtle.forward()
    turtle.turnLeft()
    turtle.forward()
    turtle.forward()
    turtle.turnLeft()
    turtle.forward()
    turtle.turnRight()
    check()
    turtle.turnLeft()
    turtle.forward()
    turtle.turnRight()
    check()
    turtle.turnLeft()
    turtle.forward()
    turtle.turnRight()
    check()
    turtle.turnLeft()
    turtle.forward()
    turtle.turnRight()
    check()
    turtle.turnLeft()
    turtle.forward()
    turtle.turnRight()
    check()
    turtle.turnLeft()
    turtle.forward()
    turtle.turnRight()
    turtle.forward()
    turtle.forward()
    turtle.turnRight()
    turtle.forward()
    turtle.forward()
    turtle.forward()
    turtle.forward()
    turtle.forward()
    turtle.forward()
    turtle.turnRight()
    turtle.forward()
    turtle.forward()
    turtle.forward()
    turtle.forward()
    turtle.forward()
    turtle.turnRight()
    for i=2,16 do
        turtle.select(i)
        turtle.dropDown()
    end
    sleep(60)
end

Any help appreciated
 

djo201

New Member
Jul 29, 2019
8
0
0
You're defining reed as the amount of items on slot 1 (reed = turtle.getItemCount(1) ), and using that value in the check function ( turtle.select(reed) ). This is probably not what you wanted, and if the result of the item count is either 0 or > 16, it will cause an error.

If I understand what you want correctly, it should be turtle.select(1)
 

Soulsbane96

New Member
Jul 29, 2019
30
0
0
You're defining reed as the amount of items on slot 1 (reed = turtle.getItemCount(1) ), and using that value in the check function ( turtle.select(reed) ). This is probably not what you wanted, and if the result of the item count is either 0 or > 16, it will cause an error.

If I understand what you want correctly, it should be turtle.select(1)
Originally I had local reed = 1
Would that work as well? Jw

I got the getItemCount from another script I saw and thought it would work.
Thanks for the info
 

djo201

New Member
Jul 29, 2019
8
0
0
Originally I had local reed = 1
Would that work as well? Jw

I got the getItemCount from another script I saw and thought it would work.
Thanks for the info

If you don't plan on changing where you place the reed (i.e. slot 1), you can just do turtle.select(1) instead of defining the reed variable.[DOUBLEPOST=1359177098][/DOUBLEPOST]Also, I just noticed you defined the variable as local. If you wish to use it inside the function you can either:
  1. Define it without the "local" (which means it will be global)
  2. Give it as a parameter to the check() function
 
  • Like
Reactions: Soulsbane96

Soulsbane96

New Member
Jul 29, 2019
30
0
0
If you don't plan on changing where you place the reed (i.e. slot 1), you can just do turtle.select(1) instead of defining the reed variable.[DOUBLEPOST=1359177098][/DOUBLEPOST]Also, I just noticed you defined the variable as local. If you wish to use it inside the function you can either:
  1. Define it without the "local" (which means it will be global)
  2. Give it as a parameter to the check() function


Ok, thanks.
Could you give more info on the defining it part?

And as of now the check function doesnt seem to do much. It didnt move up or down at all just started moving, completely disregarding the sugarcane. it turns, looking at the sugarcane, but doesnt seem to do the check function. Did I just code it crappy or do something wrong or what? Any ideas?

Thanks for your help btw
 

djo201

New Member
Jul 29, 2019
8
0
0
Ok, thanks.
Could you give more info on the defining it part?

And as of now the check function doesnt seem to do much. It didnt move up or down at all just started moving, completely disregarding the sugarcane. it turns, looking at the sugarcane, but doesnt seem to do the check function. Did I just code it crappy or do something wrong or what? Any ideas?

Thanks for your help btw

For defining the variable you'd do just
Code:
reed = 1
That way it would be global (note that variables are automatically defined as global when you don't specify otherwise in LUA), and the value would be available to any function.

As far as the check function goes, first verify that the slot with the sugar cane is selected (which it should be if you did the turtle.select(1) or turtle.select(reed) after defining reed as 1)

If it is, I can't help much since I can't see your farm setup. What I'd suggest is adding a few prints here and there to verify if the function is working correctly. For example, you could add
Code:
print("check() function running")
to the beginning of check() to verify that it's running, and could also add other prints inside the if conditionals, to verify if the conditions are being met.
 

djo201

New Member
Jul 29, 2019
8
0
0
Just tested some things myself and found out that the comparison won't work. I guess sugar cane has different ID's when planted vs. harvested. In this case, instead of using turtle.compare(), you can instead use turtle.detect() which should work.
 
  • Like
Reactions: Soulsbane96