There is no looping statement in your function.
It appears that you are checking to see if there is one or less items in slot 1. Your code reads:
if turtle.getItemCount(1) <= 1 then
Don't you mean to test for "less than one item" in slot 1? I changed the line to read:
if turtle.getItemCount(1) < 1 then
Also, since it appears you are just asking for the user to press any key, I suggest using the
os.pullEvent("key") function over
read. What the
os.pullEvent("key") function will do is to simply pause the program until the user presses a key. Since you don't care about the value of the key press, there is no need to store the return variables anywhere.
You also have no need for the
else statement and the repeated code block with which it is associated. I moved the following section of code to just past the
end in the
if control block and deleted the
else block and the repeated code within. I have no opinion on whether you need to use
os.sleep(0.4) in this section, so I just left it in.
turtle.select(1)
turtle.refuel(1)
os.sleep(0.4)
Try this code corrected code instead. Does it work for you? When you call rFuel(), the function will loop until the fuel level is 20 or above. If there is less than one item in slot 1, it will make a user request for an item and wait for a key press. The program will then select slot 1 and refuel one item worth of fuel (that is what the argument to the
refuel(1) function does, it attempts to use up to the argument number of fuel items). If you wanted it to use all of the fuel items in slot 1, leave the argument out – just use
refuel(). Next, the program will pause for 0.4 seconds (again, I am not sure this is necessary) and repeat the
while loop – testing if the fuel level is still less than 20. If in this entire process the fuel level eventually becomes 20 or above, the function will exit.
function rFuel()
while turtle.getFuelLevel() < 20 do
if turtle.getItemCount(1) < 1 then
print("Please put fuel into slot 1 and press any key")
os.pullEvent("key")
end
turtle.select(1)
turtle.refuel(1)
os.sleep(0.4)
end
end