Making a Password Protected Door
This tutorial covers on how to make a computer output redstone current when the right password is typed in. The current is then used to trigger an iron door.
How to make it
A password protected door is actually pretty easy, if you break it into steps.
First, you need to craft a computer, and connect the back to an iron door with redstone. Like this:
Once you're done with the basics, open the computer and edit the "startup" file. (type in 'edit startup') This will make it so the program will be executed when the computer boots.
Once you access the startup file, enter in these five lines.
while true do term.clear() term.setCursorPos(1, 1) print("Please Enter Password:") input = read("*")
The first line opens a loop that will continue running forever, or until we explicitly exit it with the 'break' keyword. The second and third lines clear the screen of whatever may have been on it, and then set the cursor position to the top-left corner of the screen. This ensures that each time the program loops, the text appears in the same place. The fifth line assigns whatever the user types to the variable 'input'. So for example, if I typed "qwerty", it would be assigning "qwerty" to the 'input' variable. In the read call on the third line ('read("*")'), the asterisk character will be used to replace each letter typed by the user, so that the password stays hidden.
Here are the next few lines added on:
while true do term.clear() term.setCursorPos(1, 1) print("Please Enter Password:") input = read("*") if input == "password" then redstone.setOutput("back", true) sleep(2) redstone.setOutput("back", false) end end
The first line we add, 'if input == "password" then' checks whether or not the 'input' variable contains the string "password". The three lines immediately following are only executed if the password is correct. So if it's correct, 'redstone.setOutput("back", true)' sets the redstone output on the back side to true (on), which opens the door. The program then wait two seconds ('sleep(2)'), and turns the redstone output back off again. The program then reaches the end of the loop and repeats.
Please note that this is the basic format for a computer protection program as well. We would put this program in the startup file and instead of outputting redstone signals if the password was correct, we would use the break keyword instead to exit the loop and allow access to the rest of the computer.
Note also that if the program fails somehow, pressing and holding Ctrl + T will terminate it and allow you to edit it, see the No termination section to stop people from exiting your lock.
Stop people from terminating your lock
If you don't want people holding CTRL+T and quitting your lock, use this code at the top of your program:
os.pullEvent = os.pullEventRaw