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

Password door

Started by mrpoopy345, 28 November 2013 - 06:55 AM
mrpoopy345 #1
Posted 28 November 2013 - 07:55 AM
Hello! So today I created a password door in my startup. But now when I run it, it seems like it the program doesn't even exist! I know I didn't delete it, if you do edit startup it is right there! But when I reboot the computer, it just says CraftOS 1.5 like normal.
It has pretty long code:

function clear()
term.clear()
term.setCursorPos(1,1)
end
function menu()
function menu()
term.clear()
term.setCursorPos(1,1)
print(" ")
print("Door Admin Menu.")
print(" ")
print("		   Press A for new password.")
print(" ")
print("		   Press S for new admin password.")
print(" ")
print("		   Press D for a new side")
print(" ")
print("		   Press F to exit.")
end
function a()
clear()
h = fs.open("pass", "r")
local oldPass = h.readAll()
h.close()
print("Old password:")
local oldInput = read()
if oldInput == oldPass then
print("New password:")
local newPass = read()
h = fs.open("pass", "w")
h.write(newPass)
h.close()
sleep(1)
menu()
else
print("Incorrect.")
sleep(1)
menu()
end
end
function s()
clear()
h = fs.open("admin", "r")
oldPass = h.readAll()
print("Old admin pass:")
local oldInput = read()
if oldInput == oldPass then
  print("New admin pass:")
  local newPass = read()
  h = fs.open("admin", "w")
  h.write(newPass)
  h.close()
  menu()
else
  print("Incorrect.")
  sleep(1)
  menu()
end
end
function d()
clear()
print("New side:")
local newSide = read()
h = fs.open("side", "w")
h.write(newSide)
h.close()
menu()
end
function f()
clear()
os.reboot()
end
clear()
print("SafeCo Door Lock.")
print("Please input password:")
f = fs.open("pass", "r")
local rPass = f.readAll()
f.close()
local f = fs.open("admin", "r")
local aPass = f.readAll()
f.close()
local pass = read("*")
if pass == rPass then
f = fs.open("side", "r")
local side = f.readAll()
f.close()
sleep(1)
print("Correct!")
rs.setOutput(side, true)
sleep(3)
rs.setOutput(side, false)
elseif pass == aPass then
sleep(1)
print("Welcome, admin.")
menu()
ev,p1 = os.pullEvent("char")
if p1 == "a" then
  a()
elseif p1 == "s" then
  s()
elseif p1 == "d" then
  d()
elseif p1 == "f" then
  f()
end
end
end  
Any problems with this?
By the way, I would also like to congratulate everyone who posted on my 'How did you learn lua?' topic.
Everyone really helped! Thanks!
But back to the subject.
What happened?
Thank you!
Bomb Bloke #2
Posted 28 November 2013 - 08:57 AM
function menu()
function menu()

Once you've removed one of those, chop of that extra "end" you put at the bottom of the program and give it another shot.

Correct indentation would've made the answer to this issue clearer to you. The idea is to move your margin a little to the right every time you enter a function/if/while/for block (that is to say, anything that needs an "end" on the end), then move it back to the left when you finish that block. If you've made no mistakes, your final line will not be indented at all.

Correct indentation is not needed for your code to work, but makes it FAR easier to read and hence troubleshoot.
Edited on 28 November 2013 - 08:01 AM
mrpoopy345 #3
Posted 28 November 2013 - 09:02 AM
I did put correct indentation, I just didn't when I was copying the program to the forums.
Ill update you in a minute when I try it.
Edit: It worked! Thanks!
Edited on 28 November 2013 - 08:04 AM
mrpoopy345 #4
Posted 28 November 2013 - 09:13 AM
New problem:
How do I make it so that if it is the wrong password it reboots? If I do an else then it thinks the else is for the if at line 97.
Any help on that front?
Edit: Never mind. This door is a helpless endeavor. So many bugs! And not just with the door, with computercraft!!!!
Edited on 28 November 2013 - 08:15 AM
Bomb Bloke #5
Posted 28 November 2013 - 05:05 PM
How do I make it so that if it is the wrong password it reboots? If I do an else then it thinks the else is for the if at line 97.

You'd need to make sure you'd ended the "if" block started at line 97 before trying to "else" the one above that, then. Again, I'm not sure you've got your indentation right - I'm aware what this forum tends to do to what you copy'n'paste here (though you can avoid that if you toggle off the rich-text editor - try the light switch up the top left of your posting box), I say this because the vast majority of beginners get it wrong.

if pass == rPass then
  f = fs.open("side", "r")
  local side = f.readAll()
  f.close()
  sleep(1)
  print("Correct!")
  rs.setOutput(side, true)
  sleep(3)
  rs.setOutput(side, false)
elseif pass == aPass then
  sleep(1)
  print("Welcome, admin.")
  menu()
  ev,p1 = os.pullEvent("char")
  if p1 == "a" then
    a()
  elseif p1 == "s" then
    s()
  elseif p1 == "d" then
    d()
  elseif p1 == "f" then
    f()
  end
else
  os.reboot()
end

However, having a script restart itself by rebooting is rather inelegant. Instead of having the whole script repeat itself, you could just loop the password prompt, eg:

f = fs.open("pass", "r")
local rPass = f.readAll()
f.close()
local f = fs.open("admin", "r")
local aPass = f.readAll()
f.close()

while true do  -- Repeat the below block indefinitely.
  clear()
  print("SafeCo Door Lock.")
  print("Please input password:")
  local pass = read("*")

  if pass == rPass then
    f = fs.open("side", "r")
    local side = f.readAll()
    f.close()
    sleep(1)
    print("Correct!")
    rs.setOutput(side, true)
    sleep(3)
    rs.setOutput(side, false)

  elseif pass == aPass then
    sleep(1)
    print("Welcome, admin.")
    menu()
    ev,p1 = os.pullEvent("char")

    if p1 == "a" then
      a()
    elseif p1 == "s" then
      s()
    elseif p1 == "d" then
      d()
    elseif p1 == "f" then
      f()
    end

  elseif pass == "quit" then
    break -- Halt the while loop.
  end
end

Insert "break"s to taste.

Edit: Never mind. This door is a helpless endeavor. So many bugs! And not just with the door, with computercraft!!!!

Now that's just being inflammatory - don't make statements like that without backing them up. ;)/>/>

You'll get there if you keep at it!
mrpoopy345 #6
Posted 29 November 2013 - 07:39 AM
Thank you so much! And thanks for the pep talk too. I worked really hard, and I fixed all the problems! (Except for the one with the naked cookie but there is no need to mention that.) :D/>