Advertisement
Alexr360

Password Door

Mar 18th, 2024 (edited)
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.92 KB | None | 0 0
  1. -- Set your desired password
  2. local password = "your_password"
  3.  
  4. -- Function to prompt for password input
  5. local function promptPassword()
  6.     term.clear()
  7.     term.setCursorPos(1,1)
  8.     print("Enter password:")
  9.     return read("*")
  10. end
  11.  
  12. -- Function to monitor keyboard input
  13. local function monitorInput()
  14.     while true do
  15.         local event, key = os.pullEvent("key")
  16.         if key == keys.enter then
  17.             return true -- Enter key pressed
  18.         end
  19.     end
  20. end
  21.  
  22. -- Main function to run the program
  23. local function run()
  24.     while true do
  25.         local input = promptPassword()
  26.         if input == password then
  27.             redstone.setOutput("right", true) -- Emit signal to the left
  28.             print("Access granted. Signal emitted to the left.")
  29.             local timerID = os.startTimer(30) -- Start a timer for 30 seconds
  30.             local inputThread = coroutine.create(monitorInput) -- Start monitoring keyboard input
  31.             while true do
  32.                 local event, param = os.pullEvent()
  33.                 if event == "timer" and param == timerID then
  34.                     redstone.setOutput("right", false) -- Stop emitting signal after 30 seconds
  35.                     print("Signal stopped.")
  36.                     break
  37.                 elseif event == "key" and param == keys.enter then
  38.                     redstone.setOutput("right", false) -- Stop emitting signal if enter key is pressed
  39.                     print("Signal stopped early.")
  40.                     break
  41.                 elseif event == "terminate" then
  42.                     print("Program termination is disabled.")
  43.                 elseif event == "coroutine" and param == inputThread then
  44.                     print("Invalid thread state. Restarting...")
  45.                     break
  46.                 end
  47.             end
  48.         else
  49.             print("Incorrect password. Try again.")
  50.         end
  51.     end
  52. end
  53.  
  54. -- Run the program
  55. run()
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement