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

Password Lock Code Problems

Started by whos_Blu, 21 November 2012 - 04:43 PM
whos_Blu #1
Posted 21 November 2012 - 05:43 PM

function lock()
term.clear()
term.setCursorPos(1,1)

correctpass = "bitchboy"
development = "console"

write("Please Enter Passcode: ")

pass = read()

if pass == (correctpass) then
write("Access Granted!")
redstone.setOutput("right",true)
sleep(1)
redstone.setOutput("right",false)
os.reboot()
lock()

if pass == (development)
write("Console Opened!")
return

else
write("Access Denied!")
sleep(1)
os.reboot()
lock()
end
end
lock()

Ok, so this is my code so far, that is messed up. My whole goal for this code, is to access the CraftOS 1.3 by typing in "console" so you can change things without going to a seperate computer. I coded it like so above.

THE PROBLEM: Everything works great, but when i type in anything other the correctpass it uses the console command. I dont know why. The console command was only supposed to work if you typed "console" when it asks for the password, but if you type in anything aside from the correct password, it still uses the Console command. Sorry if i'm explaining horribly. Please help! Thanks in advance!
Luanub #2
Posted 21 November 2012 - 07:09 PM
It's better to use if elseif else end rather then two if statements. You're missing a then on the second if, and you also do not need the () around the vars in your if statements although I think it will work with them. Here is how I would do the if statement.

if pass == correctpass then
  write("Access Granted!")
  redstone.setOutput("right",true)
  sleep(1)
  redstone.setOutput("right",false)
  os.reboot()
  lock()  <-- if this is your startup file you don't need this. It's not going to run after the reboot.
elseif pass == development then
  write("Console Opened!")
  return
else
  write("Access Denied!")
  sleep(1)
  os.reboot()
  lock()  <-- if this is your startup file you don't need this. It's not going to run after the reboot.
end

Instead of the reboot, or calling the function from within itself(nesting, which will cause a stack overflow) try using a while loop.

Spoiler

correctpass = "bitchboy"
development = "console"

while true do
  term.clear()
  term.setCursorPos(1,1)
  write("Please Enter Passcode: ")
  pass = read()
  if pass == correctpass then
    write("Access Granted!")
    redstone.setOutput("right",true)
    sleep(1)
    redstone.setOutput("right",false)
  elseif pass == development then
    write("Console Opened!")
    break
  else
    write("Access Denied!")
    sleep(1)
  end
end
Edited on 21 November 2012 - 06:14 PM
whos_Blu #3
Posted 21 November 2012 - 07:54 PM
It's better to use if elseif else end rather then two if statements. You're missing a then on the second if, and you also do not need the () around the vars in your if statements although I think it will work with them. Here is how I would do the if statement.

if pass == correctpass then
  write("Access Granted!")
  redstone.setOutput("right",true)
  sleep(1)
  redstone.setOutput("right",false)
  os.reboot()
  lock()  <-- if this is your startup file you don't need this. It's not going to run after the reboot.
elseif pass == development then
  write("Console Opened!")
  return
else
  write("Access Denied!")
  sleep(1)
  os.reboot()
  lock()  <-- if this is your startup file you don't need this. It's not going to run after the reboot.
end

Instead of the reboot, or calling the function from within itself(nesting, which will cause a stack overflow) try using a while loop.

Spoiler

correctpass = "bitchboy"
development = "console"

while true do
  term.clear()
  term.setCursorPos(1,1)
  write("Please Enter Passcode: ")
  pass = read()
  if pass == correctpass then
	write("Access Granted!")
	redstone.setOutput("right",true)
	sleep(1)
	redstone.setOutput("right",false)
  elseif pass == development then
	write("Console Opened!")
	break
  else
	write("Access Denied!")
	sleep(1)
  end
end

Thank YOU SO MUCH! Its working!!!!!!!!! I LOVE YOU!
Sammich Lord #4
Posted 21 November 2012 - 08:42 PM
Who sets their password to "bitchboy"? Meh, my sister's facebook password is "Fuckaduck24" so I shouldn't be typing this message.
whos_Blu #5
Posted 22 November 2012 - 09:24 AM
Who sets their password to "bitchboy"? Meh, my sister's facebook password is "Fuckaduck24" so I shouldn't be typing this message.
I put bitchboy there for the post, thats not my real password lol
Sammich Lord #6
Posted 22 November 2012 - 11:01 AM
Who sets their password to "bitchboy"? Meh, my sister's facebook password is "Fuckaduck24" so I shouldn't be typing this message.
I put bitchboy there for the post, thats not my real password lol
I thought it was… I was getting kinda concerned about your security.