This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Astrophylite's profile picture

How to make a *simple* door lock!

Started by Astrophylite, 03 July 2015 - 02:28 PM
Astrophylite #1
Posted 03 July 2015 - 04:28 PM
Just sharing something pretty simple, a door lock.
First, set up the door - a computer next to an iron door. If you are good at redstone, you can set up a piston door, but don't worry about that.
Now, go onto the computer and type "edit startup". This will allow you to edit what happens when you boot up the computer.
You will now be presented with a black area and text at the bottom saying "Press CTRL to access the menu" and the line count.
Start off by typing the following:

os.pullEvent = os.pullEventRaw
This will stop people from terminating the lock and opening the door without the password.
Next, we will define three things. Where the redstone/door is located, the password and how long the door will stay open for. To do this, type:

local doorLocation = "side_goes_here"
local password = "password_goes_here"
local sleepTime = time_in_seconds
Replace the "side_goes_here" with where the door/redstone is located on the computer (e.g: front,back,right,left,bottom,top)
Replace the "password_goes_here" with the password you want to be able to open the door with.
Replace the "time_in_seconds" to a number - that number being how long you want the door to stay open for. Do not put speech marks around this.
Next, type the following block of code:

while true do
  write("Enter the password: ")
  input = read("*")
  if input == password then
	print("Password Correct!")
	redstone.setOutput(doorLocation, true)
	sleep(sleepTime)
	redstone.setOutput(doorLocation, false)
	term.clear()
	term.setCursorPos(1,1)
  else
	print("Incorrect Password! Try again!")
	sleep(sleepTime)
	term.clear()
	term.setCursorPos(1,1)
  end
end
Now for some explaining. The "while true do" part starts a loop for the program to execute, so it will constantly ask for the password.
The write part asks you for the password, this will appear on the screen.
The next line puts what you type into a variable called "input". The program then checks the contents of this variable against the password you defined earlier on. If the password is correct, the door will open and print to the screen that the password is correct, then, after the set amount of time (the sleepTime variable), the door will close and the screen will be cleared then the code will repeat. The "else" section will be the part that gets executed if you get the password wrong. It will print that the password is incorrect then sleep for the defined amount of time and then clear the terminal. Then it will end the if statement and the loop.

So there you go, pretty long post, but it means you can add security to your home or other buildings!
Hope this helps alot of begginners to Lua or ComputerCraft.

_Zircon_

EDIT: I forgot that it is Lua not LUA! :(/>
Edited on 24 October 2015 - 08:29 PM
Lyqyd #2
Posted 03 July 2015 - 08:24 PM
Moved to Tutorials.
Xanomes #3
Posted 13 August 2015 - 09:51 PM
Thanks for this awesome tutorial, it helped me a lot :)/>
Astrophylite #4
Posted 18 August 2015 - 09:41 PM
Thanks for this awesome tutorial, it helped me a lot :)/>
Glad I could help someone ;)/>
bbc100 #5
Posted 01 November 2015 - 12:32 AM
This doesn't work for me

EDIT: I got it to work now, how do I edit it after I create it?
Edited on 31 October 2015 - 11:39 PM
Creator #6
Posted 01 November 2015 - 12:40 AM
What exactly so you mean?
Kristopher_Jones #7
Posted 01 November 2015 - 09:27 AM
This is really helpfull, thank you!
bbc100 #8
Posted 02 November 2015 - 08:25 AM
Now it comes up with 1:12: Expected string, boolean
Bomb Bloke #9
Posted 02 November 2015 - 09:28 AM
Without seeing the code you're using to generate that error, we can only guess; but a shot in the dark is that you might be miss-using redstone.setOutput(). Make sure you're passing it a string and a boolean.