r/ComputerCraft • • Dec 24 '17

simple Door Lock Problem

i copied some youtuber's code to make a simple passworded lock, and wanted to try and add a message if you put the incorrect password. any help on how to do that would be appreciated.

Code:

` while true do

   term.setBackgroundColor(colors.blue)

   term.clear()

   term.setCursorPos(1, 1)

   print("Enter password:")

   local input = read("x")

   if input == "1234" then

   redstone.setOutput("right", true)

   print("Access granted. Door opening...")

   sleep(5)

   redstone.setOutput("right", false)

   print("Incorrect password, try again.")

              end

              end

`

1 Upvotes

5 comments sorted by

View all comments

3

u/ImNotADeer Dec 24 '17 edited Dec 24 '17

If I understood your question correctly, what you need is the "else" statement. I also reorganized a little :)

local password = "1234"
local rsSide = "right"
local CORRECT_MESSAGE = "Acess granted. Door opening..."
local WRONG_MESSAGE = "Incorrect password. Try again."

function openDoor(length)
	print(CORRECT_MESSAGE)
	redstone.setOutput(rsSide, true)
	sleep(length)
	redstone.setOutput(rsSide, false)
end

while true do

	term.setBackgroundColor(colors.blue)

	term.clear()

	term.setCursorPos(1, 1)

	print("Enter password:")

	local input = read("x")

	if input == password then

		openDoor(5)
	else
		print(WRONG_MESSAGE)
            sleep(2)
	end

end

edit: Added "sleep(2)" after printing the WRONG_MESSAGE so that it is displayed for two seconds before clearing the terminal again

2

u/afjaf Dec 24 '17

holy crap, that's awesome! thank you!

1

u/ImNotADeer Dec 24 '17

:) It has been a loooong time since I last fiddled with CC, but If you wonder about anything else just send me a message. I think I still have a lot of my fun programs in a github repo somewhere

1

u/afjaf Dec 24 '17

Thanks, man. If you still got any of those programs, I'd love to use them!

0

u/[deleted] Dec 25 '17

[deleted]

1

u/afjaf Dec 25 '17

Thank you.