luaminecraftcomputercraft

Checking computercraft Lua code to operate a door


I am making a vault with some custom mods and I am using a computercraft computer to control the door but I can only open the door and not close. Is this code right?

term.setTextColor(colors.yellow)
print("Vault-Tec Door Computer")
term.setTextColor(colors.white)
print("What Command Would You Like To Do?")
term.setTextColor(colors.blue)
print("Vault.Open")
print("Vault.Close")
print("")
term.setTextColor(colors.white)
io.write("Vault-Tec:")
io.close()
    if io.read()=="Vault.Open" then
        term.setTextColor(colors.red)
        print("VAULT DOOR OPENING, PLEASE STAND BACK")
        term.setTextColor(colors.white)
        redstone.setAnalogOutput("bottom", 0)
        sleep(5)
    end
    if io.read()=="Vault.Close" then
        term.setTextColor(colors.red)
        print("SHUTTING VAULT DOOR, PLEASE STAND BACK")
        term.setTextColor(colors.white)
        redstone.setAnalogOutput("bottom", 15)
        sleep(5)
    end

Solution

  • Your first if statement calls io.read() and reads whatever is typed and compares it to Vault.Open. Your next if statements reads the next thing that was typed and compares it to Vault.Close. You should only read what was input once and store it in a variable, then you can use that value in multiple places.

    .....
    local valutStatus = io.read()
    if valutStatus == "Vault.Open" then
        term.setTextColor(colors.red)
        print("VAULT DOOR OPENING, PLEASE STAND BACK")
        term.setTextColor(colors.white)
        redstone.setAnalogOutput("bottom", 0)
        sleep(5)
    end
    if valutStatus == "Vault.Close" then
        term.setTextColor(colors.red)
        print("SHUTTING VAULT DOOR, PLEASE STAND BACK")
        term.setTextColor(colors.white)
        redstone.setAnalogOutput("bottom", 15)
        sleep(5)
    end