luaminecraftcomputercraft

'end' expected (to close 'if' at line 12) in Mining Turtle program


I have been working on a program to make a turtle mine for me. Here it is:

local depth = 0
local isJunk = true

function fuel()
    if turtle.getFuelLevel() < 20 then
        turtle.select(16)
        turtle.refuel(1)
    end
end
function up()
    fuel()
    if turtle.up() then
        return true
        depth = depth - 1
    else
        return false
    end
end
function down()
    fuel()
    if turtle.down() then
        return true
        depth = depth + 1
    else
        return false
    end
end
function checkWalls()
    for i = 1,4 do
        for j = 1,6 do
            turtle.select(i)
            if turtle.compare() then
                isJunk = true
            end
        end
        if isJunk == false then
            turtle.dig()
        end
        turtle.turnLeft()
    end
end
function digDown()
    for k = 1,6 do
        turtle.select(k)
        if turtle.compareDown() then
            if turtle.digDown() then
                return true
            else
                return false
            end
        end
    end
    turtle.select(1)
    turtle.digDown()
end
function digUp()
    for l = 1,6 do
        turtle.select(l)
        if turtle.compareUp() then
            if turtle.digUp() then
                return true
            else
                return false
            end
        end
    end
    turtle.select(1)
    turtle.digUp()
end

while true do
    term.clear()
    term.setCursorPos(1,1)
    print("-------Mining Operation Alpha-------")
    term.setCursorPos(1,2)
    term.write("Commence Mining Operation? (y/n): ")

    local input = read()

    if input  == "n" then
        term.setCursorPos(1,3)
        print("Cancelling Operation")
        sleep(1)
        exit()
    elseif input == "y" then
        term.setCursorPos(1,3)
        print("Commencing Alpha Mine")
        sleep(1)
     end

    digDn()
    down()
    digDn()
    down()
    turtle.select(7)
    turtle.placeUp()
    checkWalls()
    digDn()
    while down() do
        checkWalls()
        digDn()
    end
    up()
    turtle.select(15)
    turtle.placeDown()
    for m = 1,5 do
        up()
    end
    turtle.dig()
    fuel()
    turtle.forward()
    turtle.dig()
    fuel()
    turtle.forward()
    turtle.turnRight()
    turtle.dig()
    fuel()
    turtle.forward()
    turtle.turnLeft()
    digDn()
    while down() do
        digdn()
    end
    checkWalls()
    up()
    turtle.select(15)
    turtle.placeDown()
    checkWalls()
    while depth > 1 do
        digUp()
        up()
        checkWalls()
    end
    digUp()
    up()
    up()
    turtle.select(7)
    turtle.placeDown()
    fuel()
    turtle.forward()
    turtle.forward()
    turtle.turnRight()
    turtle.forward()
    turtle.turnLeft()
end

1st-6th slots are items that I don't want it to mine, 7th is cobblestone, 15th is torches so mobs don't spawn at the bottom of the shafts, and 16th is coal.

Whenever I run it, I get an error saying:

bios.lua:26: [string "mine.lua"]:14: 'end' expected (to close 'if' at line 12)

I looked over it and there is an 'end' for the statement. However, if I comment out the return functions it works.


Solution

  • A return statement must be the last statement in a block.

    You probably want

    function up()
        fuel()
        if turtle.up() then
            depth = depth - 1
            return true
        else
            return false
        end
    end