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

[Lua] [Question] Why doesnt my lock security work?

Started by TheWoakus, 15 July 2012 - 10:30 AM
TheWoakus #1
Posted 15 July 2012 - 12:30 PM
Well i started with computercraft yesterday when i decided to make a lock for my house. Everythign went great, i found some tutorials and read them. But the problem is why doesnt my lock security work?

Everything work great but i got to terminate it and start it up again for it to work properly. Ok i will tell you what happens.

When i start it up everything goes as it should. It says it version and asks for the password. When i enter the wrong password it says "Incorrect Password" like it should. But when i type the right password it also says that. But when i type the right password again for the second time it then says "Password Correct", And the door opens. But then if i write the wrong password it just makes a new line and ask for the password again. It doesnt say "Password Incorrect" and it neither clears the screen. I have tried to fix it for a while but it doesnt want to work But here is the code so you can se for yourself whats wrong:

while password ~= "Fish" do
print ("TheWoakus Lock Security V0.1")
write ("Enter Password: -")
password = io.read()
print ("Password Incorrect")
sleep (3)
term.clear()
term.setCursorPos(1,1)
end
while true do
print ("TheWoakus Lock Security V0.1")
write ("Enter Password: -")
password = io.read()
if password == "Fish" then
print ("Password Correct")
redstone.setOutput("back", true)
sleep (5)
redstone.setOutput("back", false)
term.clear()
term.setCursorPos(1,1)
end
end
flaminsnowman99 #2
Posted 15 July 2012 - 01:40 PM
Where are you getting an error? Also on your line "password = io.read(), make it "password = io.read("*"). That way people cant see what you are typing.
Tiin57 #3
Posted 15 July 2012 - 03:54 PM
Fixed your code by putting the same content into a different format. This may be simpler, but that means it's much easier to edit and control. I think the problem was that you had was that you told it to print "Password incorrect." the first time, no matter what, in line 5. Here's the modified code, fully operational. It loops back into the lock program after the five seconds is up, or if it is wrong, it puts you at the start immediately, as I assumed you wanted.

-- I suggest you save this as "lock" or modify lines 12 and 16 to what you want the program to be named.")
print ("TheWoakus Lock Security V0.1")
write ("Enter Password: -")
written = read("*")
if written == "Fish" then
print ("Password Correct")
redstone.setOutput("back", true)
sleep (5)
redstone.setOutput("back", false)
term.clear()
term.setCursorPos(1,1)
shell.run("lock")
else
print("")
print("Password incorrect.")
shell.run("lock")
end
MysticT #4
Posted 15 July 2012 - 04:20 PM
It's a really bad idea to run the program inside the program to make a loop, it will overflow the stack and crash the computer after some time.
The correct way would be using a loop:

local sPass = "YourPasswordHere"

while true do
  term.clear()
  term.setCursorPos(1, 1)
  print("TheWoakus Lock Security V0.1")
  write("Enter Password: ")
  local password = read("*")
  if password == sPass then
    print("Password Correct")
    rs.setOutput("back", true)
    sleep (5)
    rs.setOutput("back", false)
  else
    print("Password Incorrect")
    sleep(2)
  end
end
You should add protection for Ctrl+T, so people can't terminate the program and use the computer. Adding "os.pullEvent = os.pullEventRaw" at the top should be enough.
TheWoakus #5
Posted 15 July 2012 - 04:50 PM
Ahh thanks. It works now :P/>/>