Difference between revisions of "Making a Password Protected Door"
(Added an alternative code) |
m (→Alternative code) |
||
Line 55: | Line 55: | ||
== Alternative code == | == Alternative code == | ||
− | Use Ctrl + T to terminate the program. You could | + | Use Ctrl + T to terminate the program. You could set it up as a [[Startup|startup script]] to make it run automatically at boot time. |
<code> | <code> | ||
local function pass() | local function pass() |
Revision as of 21:40, 17 March 2012
This tutorial covers on how to make a computer output redstone current when the right "password" is typed in. In this case, it would output redstone current to an iron door.
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 two lines.
The first line defines a function, so that we can repeat the program if a password is entered, and the second line is assigning whatever the user types to the variable, "t", So for example, if I typed "qwerty", it would be t = "qwerty". It also overrides anything else the user types in, so if I typed in "programs", it would just be t = "programs", and not act as the programs command in the regular OS. This can very useful.
After you're done with the previous lines, enter in the following line:
This makes it so the program checks if the variable that we declared earlier, "t", equals "password". (You can change password to what you want the actual password to be.)
But now you'll need to put in what to do, depending on if the user typed in the right password.
This next set of lines will do that:
Now, this maay seem complicated, but it's really simple. The four lines after the if line, determine what the computer will do if the password is correct. So if it's correct, It'll set the redstone output from it's back to true (on), and open the door. (this is the "redstone.setOutput("back", true)" command.) It'll then wait two seconds, and turn the redstone back off again. It'll then repeat the function, so you don't need to reboot the computer.
However, the last two lines dictate what the computer will do if the password is wrong, which is really just not opening the door, and repeating the function.
And now the finishing touches:
This adds the "Ends" to the if which is inside the function, and the function. So we've declared our function. Now to start it, we have the computer call the function after it declares it.
Well, you should have your own password protected door! Feel free to change the script around, maybe add a few success/failure messages, etc. etc.
(NOTE: If the program fails somehow, Pressing and holding Ctrl + T will terminate it, and allow you to edit it like normally)
Alternative code
Use Ctrl + T to terminate the program. You could set it up as a startup script to make it run automatically at boot time.
local function pass() term.clear() term.setCursorPos(1, 1) textutils.slowWrite("Enter password: ") t = io.read() -- Replace 'password' by the one you want if t == "password" then textutils.slowWrite("Access granted!") redstone.setOutput("back", true) sleep(2) redstone.setOutput("back", false) else textutils.slowWrite("Access denied!") sleep(2) end end while true do pass() end