Advertisement
Guest User

Lua Password script + explanation

a guest
Dec 10th, 2012
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.18 KB | None | 0 0
  1.  
  2. -- Variables are being defined here
  3. password = "password" -- This is the password variable
  4. side = "back" -- This is the redstone output side, in this case the back
  5. lockDown = 60 -- If the password is wrong the computer will lock down for 60 seconds
  6. count = 2 -- This number checks how many times you can try entering a password.
  7.  
  8. -- Clear the screen
  9. os.pullEvent = os.pullEventRaw
  10. term.clear() -- Clear screen
  11. term.setCursorPos(1, 1) --Reset cursor
  12.  
  13. -- Prints text to the screen
  14. print("Enter the password to open the door.")
  15. var1 = read("*") -- This defines var1 as whatever the user types on the screen. The * means that every character will be replaced with a * to ensure nobody can read the pass while you are typing it.
  16. sleep(0.5)
  17.  
  18. -- Lockdown check starts here
  19. while true do -- This is a loop and will run continuously.
  20.  if count <= 0 then -- Checks how many tries are left. <= means smaller than or equal to.
  21.   print("Lockdown activated. Program will be inaccessible for: 1 minute(s).")
  22.    for i = 1, 60 do -- Here's another loop. This will repeat the code below 60 times.
  23.     print("Lockdown will disable in: ", lockDown, " seconds.") -- Prints the remaining time
  24.     sleep(1) -- Wait one second
  25.     lockDown = lockDown - 1 -- Subtract one second from the time
  26.     term.clear() -- Clear the screen so it isn't filled with text
  27.     term.setCursorPos(1, 1) -- idem
  28.    end
  29.   lockDown = 60 -- Resets the value
  30.   break -- Loop ends here
  31.  end -- and here
  32.  
  33. -- Password check starts here
  34.  if var1 ~= password then -- Check if user input DOES NOT match password
  35.   print("Incorrect password.")
  36.   term.clearLine()
  37.   print(count, " attempt(s) remaining.") -- Print remaining attempts
  38.   count = count - 1 -- Subtract 1 from the attempt.
  39.   var1 = read("*") -- Again, accept user input
  40.  
  41.  elseif var1 == "@quit" then --If you enter a certain code the program will stop.
  42.   break
  43.  elseif var1 == password then -- If user input MATCHES password
  44.   print("Enter the amount of seconds you want the door to stay open. Maximum is 20, minimum is 1.")
  45.   varTime = read() --User input
  46.   varTime = tonumber(varTime) -- Writes user input to value.
  47.  
  48.   if tonumber(varTime) ~= nil then -- check if user actually inputted something
  49.    if varTime < 1 then -- Smaller than one is not allowed.
  50.     print("Minimum is 1 second.")
  51.     return -- Return to the user input
  52.    elseif varTime > 20 then -- Bigger than 20 seconds isn't allowed either
  53.     print("Maximum is 20 seconds.")
  54.     return -- Back to input again
  55.    elseif varTime >= 1 and varTime <= 20 then -- If input is between 1 and 20, proceed
  56.     print("Door will open. Please leave the console.")
  57.     rs.setOutput(side, true) -- Turns on the output on the side, "back" as defined above.
  58.     sleep(varTime) -- The program waits for the time specified earlier, between 1 and 20 seconds.
  59.     rs.setOutput(side, false) -- Turns redstone off again
  60.     break --Loop
  61.    end -- Ends
  62.   end -- Here
  63.  end -- Password check ends here
  64.  
  65. sleep(0.5)
  66. end
  67.  
  68. term.clear() -- Clear the screen
  69. term.setCursorPos(1, 1) -- Reset cursor position
  70.  
  71. os.reboot() -- Restart the system. In my case I put this program in the startup file so on reboot this program would restart.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement