This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Plaguechill's profile picture

Problem with program...

Started by Plaguechill, 30 November 2013 - 02:58 AM
Plaguechill #1
Posted 30 November 2013 - 03:58 AM
So i have a door lock for my server im trying to get it to work as intended everything works except the redstone signal and for some reason it will except any username and password….im not sure what im doing wrong if anyone could help id love it…..


local username1 = "Plague"
local password1 = "5628"
local username2 = "Notyro"
local password2= "josh123"
term.clear()
print ("welcome select an option.")
print ("[1] Login")
print ("[2] Sutdown")
write (">")
input = read()
if input == "2" then
os.shutdown()
elseif input == "1" then
print ("Please Login.")
write ("Username: ")
local input = read()
if input == username1 or uesrname2 then
write ("Password: ")
local input = read()
in input == password1 or password2 then
print ("Access Granted.")
redstone.setOutPut("right", true)
sleep (4)
redstone.setOutPut("right", false)
os.reboot()
else
print "Access Denied"
sleep (1.5)
os.reboot()
end
end



i apologize that it isnt in code form notpad++ didnt want to work for me lol
Edited by
Lyqyd #2
Posted 30 November 2013 - 12:01 PM
You really should use a table for the user names and passwords:


local logins = {
    ["Plague"] = "5628",
    ["Notyro"] = "josh123",
}
while true do
    term.clear()
    print("welcome select an option.")
    print("[1] Login")
    print("[2] Sutdown")
    write(">")
    input = read()
    if input == "2" then
        os.shutdown()
    elseif input == "1" then
        print("Please Login.")
        write("Username: ")
        local username = read()
        write("Password: ")
        local password = read("*")
        if logins[username] and logins[username] == password then
            print("Access Granted.")
            redstone.setOutput("right", true)
            sleep(4)
            redstone.setOutput("right", false)
        else
            print("Access Denied")
            sleep(1.5)
        end
    end
end

Your code had a number of issues. Your if statements were basically, "if input is equal to password1 or password2 is neither nil nor false, then", and since password2 (same with username2) was never nil or false, the if statements were always evaluating to true. Using the table entries method, we can check that the username entry in the table exists, and then further check that the matching password is also correct, whereas the original code would have allowed any password and any username if you had corrected your if statements.

You also had redstone.setOutPut, where it should be redstone.setOutput, no capital P. I also got rid of the reboot calls and simply put the main body of the program in a loop. I also added a masking character to the password read call so that the passwords won't be visible on the screen.