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

Password Door Lock First Advanced Lua Script

Started by Sheefou, 05 August 2013 - 04:53 PM
Sheefou #1
Posted 05 August 2013 - 06:53 PM
i need help with my code and how to fix it from another computer incase it fails and i cant enter the bypass code (Eg: program loops and you cant ente]r anything in with out it rebooting every tick -_-/>)

here is the current code i have

Spoiler

os.pullEvent = os.pullEventRaw
local adminpw
local side = "left"
local password = "creeper"
local opentime = 5
while true do
term.clear()
term.setCursorPos(1,1)
write("Access Code Or Admin")
local input = read("*")
if input == password then
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowPrint("Access Granted")
  rs.setOutput(side,true)
  sleep(opentime)
  rs.setOutput(side,false)
else
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowPrint("Access Denied Activating Alarm Systems!")
  textutils.slowPrint("System Timeout Retry in 60 seconds")
  sleep(60)
  os.reboot
if input == adminpw then
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("Access Code Bypassed")
os.pullEvent = pullEvent
end
end

current problem when i enter the password nothing happens when i enter the bypass code (adminpw) it grants access but does whats under else and it locks out and wont open door HALP
Kingdaro #2
Posted 05 August 2013 - 07:22 PM
You're only checking for the admin password if the user didn't enter in the main password. Try using an elseif for checking the admin pass, rather than putting it under the last else.

I've also commented some other things I've noticed with the script, and improved it a little bit.


local pullEvent = os.pullEvent --# make sure you actually store the old pullevent so you can restore it later
os.pullEvent = os.pullEventRaw

local adminpw = "creeper2" --# just a dummy password, change if needed.
local side = "left"
local password = "creeper"
local opentime = 5

while true do
  term.clear()
  term.setCursorPos(1,1)
  write("Access Code Or Admin")
  local input = read("*")

  term.clear() --# added this here and removed it from the ifs; it's done in every case, so we only do it once here.
  term.setCursorPos(1,1)

  if input == password then
    textutils.slowPrint("Access Granted")
    rs.setOutput(side,true)
    sleep(opentime)
    rs.setOutput(side,false)
  elseif input == adminpw then
    textutils.slowPrint("Access Code Bypassed")
    os.pullEvent = pullEvent
    break --# we need to get into the computer, so we break out of the loop
  else
    textutils.slowPrint("Access Denied Activating Alarm Systems!")
    textutils.slowPrint("System Timeout Retry in 60 seconds")
    sleep(60)
    --# os.reboot not necessary.
  end
end
Sheefou #3
Posted 05 August 2013 - 07:32 PM
thx it worked it was glitchy -_-/> and the ifelse script Really helped also where do i store the old os.pullEvent
Kingdaro #4
Posted 05 August 2013 - 07:39 PM
At the very top:
local pullEvent = os.pullEvent
Sheefou #5
Posted 05 August 2013 - 09:04 PM
My fail i also need to know how to wireless access computer #0 from computer #1 and to make a backup disk of the file (i already did that) and how to load it :(/> sry if im making you work hard but i appreciate the Help thx
Sheefou #6
Posted 11 March 2014 - 09:45 PM
now that im apro after some years i wanna know how do you check for multiple passwords without using elseif
CometWolf #7
Posted 11 March 2014 - 09:59 PM
it's been half a year, not "some years" but yeah anyways…
Index your passwords in a table, then loop through the table and set a variable to true if it finds a match

local tPasswords = {
  "derp",
  "notDerp"
  "thisIsAPassword",
  "You shall not pass!"
}
local input = read()
local correct = false
for k,v in pairs(tPasswords) do
  if input == v then
    correct = true
    break
  end
end
if correct then
  --correct password
else
  --incorrect password
end