[ComputerCraft] Password protected door

Aug 2nd, 2013 (edited)
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. os.pullEvent = os.pullEventRaw -- This prevents people from closing the program with ctrl + t
  2. local side = "left" -- What side is the door on?
  3. local password = "password" -- What is the main password?
  4. local debug = "debug password" -- The debug password will quit the password program allowing you to edit the system
  5. local stayOpen = "stay open password" -- This password will keep the door open
  6. local opentime = 5 -- How long (With the standard password) should the door stay open?
  9. while true do -- Keep the program running
  10.  term.clear() -- Clear the screen of any text
  11.  term.setCursorPos(1,1) -- Set the "cursor" to the top left hand side of the screen
  12.  write("Please Input Your Password: ") -- Request password, I used write because it does not start a new line
  13.  local input = read("*") -- This defines the local variable "input" and it is equivenent to read("*"), this means that it will store what the user entered in in an attempt to open the door, the read() has a "*" in it so it will print * instead of what the user is entering
  14.  if input == password then -- If what the user entered in was the same as the password defined above
  15.   term.clear() -- Clear the screen
  16.   term.setCursorPos(1,1) -- set cursor to top left corner
  17.   print("Password correct!") -- Tell the user they entered the correct password
  18.   rs.setOutput(side,true) -- sets the redstone output on side (Defined on line 2) to true
  19.   sleep(opentime) -- waits for opentime (Defined on line 6)
  20.   rs.setOutput(side,false) -- sets the redstone output on side (Defined on line 2) to false
  21. elseif input == debug then -- elseif means if it does not match the if statement above then if it matches this elseif statement run it... Here it checks if the user entered the "debug" password.
  22.  error() -- I have found exit() to print stuff to the console so I used error() to close the program
  23. elseif input == stayOpen then -- another elseif statement, this time seeing if they entered the "stayOpen" password
  24.  rs.setOutput(side,true) -- sets the redstone output on side (Defined on line 2) to true
  25.  term.clear() -- Clear the screen
  26.  term.setCursorPos(1,1) -- Set the cursor to top right corner
  27.  print("The door will stay open until you press a key") -- Tell them they need to press a key to close the door
  28.  local event, keyCode = os.pullEvent("key") -- this waits for a key press event
  29.  rs.setOutput(side,false) -- sets the redstone output on side (Defined on line 2) to false
  30. else -- If it is not any of those passwords...
  31.   print("Password incorrect!") -- Tell the user that
  32.   sleep(2) -- Wait for 2 seconds
  33.  end -- end the if password part
  34. end -- end the while true do loop
Add Comment
Please, Sign In to add comment