Lua roadblock! Help plz

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here
  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

sinistersuperspy

New Member
Jul 29, 2019
5
0
0
Ok - so this code is for an entry door that remains locked until the player acknowledges all questions. The problem is as i am testing each phase either yes or no, to make sure it works alright.

Everything does, except for the first question: "Understand the rules?" If N is chosen, it restarts the program, rather than immediately going to the appropriate elseif line.

I absolutely have no idea whats wrong, as everything else works perfectly.

Heres the code: (EDITED: Colour coded each section)

while true do
term.clear()
term.setCursorPos(1, 1)
print("Welcome to the Aubi-Minecraft Hardcore Server")
print("****************************************************")
sleep(5)term.clear()
term.setCursorPos(1, 1)
print("Before you proceed")
print("please answer the following questions")
print("****************************************************")
sleep(5)
term.clear()
term.setCursorPos(1, 1)
  • print("I hereby acknowledge that I have read and understood the server rules")
  • print("**************************************************")
  • sleep(5)
  • print("Type Y or N")
  • input = read()
  • if input == "Y" then
  • sleep(5)
  • term.clear()
  • term.setCursorPos(1, 1)
  • print("Do you understand the Sacrificial Tax Honour System?")
  • print("*********************************************")
  • sleep(5)
  • print("Type Y or N")
  • input = read()
    • if input == "Y" then
    • term.clear()
    • term.setCursorPos(1, 1)
    • print("Please respect other players and their work")
    • sleep(5)
    • term.clear()
    • term.setCursorPos(1, 1)
    • print("The Admins are always watching. BEHAVE or BE BANNED!")
    • sleep(5)
    • term.clear()
    • term.setCursorPos(1, 1)
    • print("Please wait while the system unlocks the door.")
    • redstone.setOutput("left", true)
    • print("LEAVE NOW")
    • sleep(5)
    • redstone.setOutput("left", false)
    • elseif input == "N" then
    • term.clear()
    • term.setCursorPos(1, 1)
    • print("In the event of your untimely demise through stupidity or laziness")
    • sleep(5)
    • term.clear()
    • term.setCursorPos(1, 1)
    • print("the following process MUST occur without fail!")
    • sleep(5)
    • term.clear()
    • term.setCursorPos(1, 1)
    • print("Make your way to the Crimson Reflection, the diamond over the lava pit.")
    • sleep(5)
    • term.clear()
    • term.setCursorPos(1, 1)
    • print("Enter the diamond, and drop your item of highest value into the pit")
    • sleep(5)
    • term.clear()
    • term.setCursorPos(1, 1)
    • print("Head back to the Wall of Shame, and place a plaque with your name and item sacrificed.")
    • sleep(5)
    • term.clear()
    • term.setCursorPos(1, 1)
    • print("Do you understand? Type Y or N")
      • input = read()
      • if input == "Y" then
      • term.clear()
      • term.setCursorPos(1, 1)
      • print("Please respect other players and their work")
      • sleep(5)
      • term.clear()
      • term.setCursorPos(1, 1)
      • print("The Admins are always watching. BEHAVE or BE BANNED!")
      • sleep(5)
      • term.clear()
      • term.setCursorPos(1, 1)
      • print("Please wait while the system unlocks the door.")
      • redstone.setOutput("left", true)
      • print("LEAVE NOW")
      • sleep(5)
      • redstone.setOutput("left", false)
      • elseif input == "N" then
      • term.clear()
      • term.setCursorPos(1, 1)
      • print("Seriously? tough Shit. GAME ON!")
      • sleep(5)
      • term.clear()
      • term.setCursorPos(1, 1)
      • print("You have 5 seconds to get through the door")
      • redstone.setOutput("left", true)
      • print("LEAVE NOW")
      • sleep(5)
      • redstone.setOutput("left", false)
  • elseif input == "N" then
  • term.clear()
  • term.setCursorPos(1, 1)
  • print("Access Denied")
  • sleep(5)
  • term.clear()
  • term.setCursorPos(1, 1)
  • print("Please exit and read the server rules")
  • sleep(5)
  • term.clear()
  • term.setCursorPos(1, 1)
  • print("If you don't know where they are , you don't belong here")
  • sleep(5)
  • term.clear()
  • term.setCursorPos(1, 1)
  • print("Goodbye.")
  • sleep(5)
  • os.shutdown()
      • end
    • end
  • end
end

This last end is apparently needed to close the "while" statement at the top.
 

BlackFire

New Member
Jul 29, 2019
344
0
0
Could you please point out the exact quote that isn't printing. Is that the only thing that's not working?[DOUBLEPOST=1374197760][/DOUBLEPOST]By the looks of it I think you have an "end" in the wrong place closing the "if" statment before the "n" option can be executed.
 

Enigmius1

New Member
Jul 29, 2019
499
0
0
Are you entering the Y/N choices as capitals or lower case? Also, you appear to have two else statements referencing 'N' as input. It's kind of a mess, really. From formatting to method it's a pretty wobbly script. Bulletproof the input, format the code properly with indentations and such that make it readable, use 'elseif' instead of 'else if' if that's what you're wanting to do, and don't use the same condition twice in chain of conditionals.
 

Norfgarb

New Member
Jul 29, 2019
202
0
0
Ok, in LUA "elseif" is one wordso change your "else if" s to "elseif" then you should have 3 ends not 4

If that doesn't help could you paste the error you are recieving when you run the code.
 

sinistersuperspy

New Member
Jul 29, 2019
5
0
0
Wow - thx guys -

In the actual code the elseif's are one word - i double checked that my first time through.
The part that isn't working is the "N" option on the first question "I hereby acknowledge that I have read and understood the server rules".
When "N" is entered, the program just resets, rather than going to "Access Denied".
 

sinistersuperspy

New Member
Jul 29, 2019
5
0
0
Are you entering the Y/N choices as capitals or lower case? Also, you appear to have two else statements referencing 'N' as input. It's kind of a mess, really. From formatting to method it's a pretty wobbly script. Bulletproof the input, format the code properly with indentations and such that make it readable, use 'elseif' instead of 'else if' if that's what you're wanting to do, and don't use the same condition twice in chain of conditionals.



Y and N are capitals. Each affirmative answer leads to the next question, and eventually, an unlocked door. Each negative answer either loops through more text and a repeated Y or N, or in the case of the first question, to an "Access Denied" and the program, ideally, shuts down.
 

gattsuru

Well-Known Member
May 25, 2013
364
103
68
The most immediate issue is that ends close their surrounding code blocks; they need to be in the right order, or the code will treat each elseif as if it were in the same code block and scope as the most recent if. You need the Access Denied code block to occur before your second-to-last end in order for it to occur where it sounds like you want it, otherwise it's just going to occur after the "Tough Shit /Game On" message.

Beyond that, your code is a mess : LUA is case sensitive, including when doing letter comparisons, so you really should sanitize your inputs. Also, use methods. Also, don't use magic numbers more than necessary. And os.Shutdown turns off the computer, which you probably don't want to happen ever. Also, use code blocks and indent your work when posting to forums. Finally, you may want to have the elseif statement occur as long as they encounter something that isn't a Y, in which case you'll want to change them out from "elseif string.lower(input) == "n" then" to just "else"

This will fix your immediate issue :
Code:
while true do
  term.clear()
  term.setCursorPos(1, 1)
  print("Welcome to the Aubi-Minecraft Hardcore Server")
  print("****************************************************")
  sleep(5)
  term.clear()
  term.setCursorPos(1, 1)
  print("Before you proceed")
  print("please answer the following questions")
  print("****************************************************")
  sleep(5)
  term.clear()
  term.setCursorPos(1, 1)
  print("I hereby acknowledge that I have read and understood the server rules")
  print("**************************************************")
  sleep(5)
  print("Type Y or N")
  input = read()
  if string.lower(input) == "y" then
    sleep(5)
    term.clear()
    term.setCursorPos(1, 1)
    print("Do you understand the Sacrificial Tax Honour System?")
    print("*********************************************")
    sleep(5)
    print("Type Y or N")
    input = read()
    if string.lower(input) == "y" then
      term.clear()
      term.setCursorPos(1, 1)
      print("Please respect other players and their work")
      sleep(5)
      term.clear()
      term.setCursorPos(1, 1)
      print("The Admins are always watching. BEHAVE or BE BANNED!")
      sleep(5)
      term.clear()
      term.setCursorPos(1, 1)
      print("Please wait while the system unlocks the door.")
      redstone.setOutput("left", true)
      print("LEAVE NOW")
      sleep(5)
      redstone.setOutput("left", false)
    elseif string.toLower(input) == "n" then
      term.clear()
      term.setCursorPos(1, 1)
      print("In the event of your untimely demise through stupidity or laziness")
      sleep(5)
      term.clear()
      term.setCursorPos(1, 1)
      print("the following process MUST occur without fail!")
      sleep(5)
      term.clear()
      term.setCursorPos(1, 1)
      print("Make your way to the Crimson Reflection, the diamond over the lava pit.")
      sleep(5)
      term.clear()
      term.setCursorPos(1, 1)
      print("Enter the diamond, and drop your item of highest value into the pit")
      sleep(5)
      term.clear()
      term.setCursorPos(1, 1)
      print("Head back to the Wall of Shame, and place a plaque with your name and item sacrificed.")
      sleep(5)
      term.clear()
      term.setCursorPos(1, 1)
      print("Do you understand? Type Y or N")
      input = read() 
      if input == "Y" then
        term.clear()
        term.setCursorPos(1, 1)
        print("Please respect other players and their work")
        sleep(5)
        term.clear()
        term.setCursorPos(1, 1)
        print("The Admins are always watching. BEHAVE or BE BANNED!")
        sleep(5)
        term.clear()
        term.setCursorPos(1, 1)
        print("Please wait while the system unlocks the door.")
        redstone.setOutput("left", true)
        print("LEAVE NOW")
        sleep(5)
        redstone.setOutput("left", false)
      elseif input == "N" then
        term.clear()
        term.setCursorPos(1, 1)
        print("Seriously? tough Shit. GAME ON!")
        sleep(5)
        term.clear()
        term.setCursorPos(1, 1)
        print("You have 5 seconds to get through the door")
        redstone.setOutput("left", true)
        print("LEAVE NOW")
        sleep(5)
        redstone.setOutput("left", false)
      end
    end
  elseif string.lower(input) == "n" then
    term.clear()
    term.setCursorPos(1, 1)
    print("Access Denied")
    sleep(5)
    term.clear()
    term.setCursorPos(1, 1)
    print("Please exit and read the server rules")
    sleep(5)
    term.clear()
    term.setCursorPos(1, 1)
    print("If you don't know where they are , you don't belong here")
    sleep(5)
    term.clear()
    term.setCursorPos(1, 1)
    print("Goodbye.")
    sleep(5)
  end
end

But this will make it a lot more readable and understandable in the future, as well as fix likely bugs :
Code:
local sleepTimer = 5

function resetPrintSleep(text)
  term.clear()
  term.setCursorPos(1,1)
  print(text)
  print("***************************************************")
  sleep(sleepTimer)
end

while true do
  resetPrintSleep("Welcome to the Aubi-Minecraft Hardcore Server")
  resetPrintSleep("Before you proceed \n please answer the following questions ")
  resetPrintSleep("I hereby acknowledge that I have read and understood the server rules")
  print("Type Y or N")
  input = read()
  if string.lower(input) == "y" then
    resetPrintSleep("Do you understand the Sacrificial Tax Honour System?")
    print("Type Y or N")
    input = read()
    if string.lower(input) == "y" then
      resetPrintSleep("Please respect other players and their work")
      resetPrintSleep("The Admins are always watching. BEHAVE or BE BANNED!")
      resetPrintSleep("Please wait while the system unlocks the door.")
      redstone.setOutput("left", true)
      print("LEAVE NOW")
      sleep(5)
      redstone.setOutput("left", false)
    elseif string.toLower(input) == "n" then
      resetPrintSleep("In the event of your untimely demise through stupidity or laziness")
      resetPrintSleep("the following process MUST occur without fail!")
      resetPrintSleep("Make your way to the Crimson Reflection, the diamond over the lava pit.")
      resetPrintSleep("Enter the diamond, and drop your item of highest value into the pit")
      resetPrintSleep("Head back to the Wall of Shame, and place a plaque with your name and item sacrificed.")
      print("Do you understand? Type Y or N")
      input = read() 
      if string.toLower(input) == "y" then
        resetPrintSleep("Please respect other players and their work")
        resetPrintSleep("The Admins are always watching. BEHAVE or BE BANNED!")
        resetPrintSleep("Please wait while the system unlocks the door.")
        redstone.setOutput("left", true)
        print("LEAVE NOW")
        sleep(5)
        redstone.setOutput("left", false)
      elseif input == "N" then
        resetPrintSleep("Seriously? tough Shit. GAME ON!")
        print("You have 5 seconds to get through the door")
        redstone.setOutput("left", true)
        print("LEAVE NOW")
        sleep(5)
        redstone.setOutput("left", false)
      end
    end
  elseif string.lower(input) == "n" then
    resetPrintSleep("Access Denied")
    resetPrintSleep("Please exit and read the server rules")
    resetPrintSleep("If you don't know where they are, you don't belong here")
    resetPrintSleep("Goodbye.")
  end
end
 

Enigmius1

New Member
Jul 29, 2019
499
0
0
Y and N are capitals. Each affirmative answer leads to the next question, and eventually, an unlocked door. Each negative answer either loops through more text and a repeated Y or N, or in the case of the first question, to an "Access Denied" and the program, ideally, shuts down.

I'd add a blurb to handle inputs in lower case or outside the expected values.

More to the point, it's very hard to tell but it looks like you're just nesting the questions and then barfing the ends all at the bottom.

Consider this:

Code:
function waitClear (length)
 
    if not (length == 0) then
        sleep (length)
    end
    term.clear()
    term.setCursorPos (1, 1)
end
 
while true do
    waitClear (0)
    print("Welcome to the Aubi-Minecraft Hardcore Server")
    print("****************************************************")
    waitClear (5)
    print("Before you proceed")
    print("please answer the following questions")
    print("****************************************************")
    waitClear(5)
    print("I hereby acknowledge that I have read and understood the server rules")
    print("**************************************************")
    sleep(5)
    print("Type Y or N")
    input = read()
    if input == "Y" then
        waitClear (5)
        print("Do you understand the Sacrificial Tax Honour System?")
        print("*********************************************")
        sleep(5)
        print("Type Y or N")
        input = read()
        if input == "Y" then
            waitClear(0)
            print("Please respect other players and their work")
            waitClear (5)
            print("The Admins are always watching. BEHAVE or BE BANNED!")
            waitClear (5)
            print("Please wait while the system unlocks the door.")
            redstone.setOutput("left", true)
            print("LEAVE NOW")
            sleep(5)
            redstone.setOutput("left", false)
       elseif input == "N" then
            waitClear (0)
            print("In the event of your untimely demise through stupidity or laziness")
            waitClear (5)
            print("the following process MUST occur without fail!")
            waitClear (5)
            print("Make your way to the Crimson Reflection, the diamond over the lava pit.")
            waitClear (5)
            print("Enter the diamond, and drop your item of highest value into the pit")
            waitClear (5)
            print("Head back to the Wall of Shame, and place a plaque with your name and item sacrificed.")
            waitClear (5)
            print("Do you understand? Type Y or N")
            input = read()
            if input == "Y" then
                waitClear (0)
                print("Please respect other players and their work")
                waitClear (5)
                print("The Admins are always watching. BEHAVE or BE BANNED!")
                waitClear (5)
                print("Please wait while the system unlocks the door.")
                redstone.setOutput("left", true)
                print("LEAVE NOW")
                sleep(5)
                redstone.setOutput("left", false)
            elseif input == "N" then
                waitClear (0)
                print("Seriously? tough Shit. GAME ON!")
                waitClear (5)
                print("You have 5 seconds to get through the door")
                redstone.setOutput("left", true)
                print("LEAVE NOW")
                sleep(5)
                redstone.setOutput("left", false)
            end
        end
    elseif input == "N" then
        waitClear (0)
        print("Access Denied")
        waitClear (5)
        print("Please exit and read the server rules")
        waitClear (5)
        print("If you don't know where they are , you don't belong here")
        waitClear (5)
        print("Goodbye.")
        sleep(5)
        os.shutdown()
    end
end

Properly formatted to assist readability, 'end' statements in appropriate locations, groups of commands that you use in the same way over and over again shifted to a function to reduce size/improve readability.
 
  • Like
Reactions: gattsuru