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

Password door

Started by tysciman7, 28 January 2013 - 12:13 PM
tysciman7 #1
Posted 28 January 2013 - 01:13 PM
I cant get this to work as soon as the first incorrect pass is typed it says the right text but then the program ends so if you can please help me



here it is

os.shutdown = os.shutdown
rednet.open("back")
os.pullEvent = os.pullEventRaw
local mon = peripheral.wrap("top")
i = 0
local alarm = false
local door = false
mon.clear()
mon.setTextScale(1)
mon.setCursorPos(1,1)
mon.write("Welcome to TyCo's")
mon.setCursorPos(1,3)
mon.write("CC store")
while door == false do
term.clear()
term.setCursorPos(14,1)
print("Welcome to Temporary Tyco")
term.setCursorPos(1,3)
term.write("Please Enter Password:")
pass = read("*")
if pass == "inside" then
term.setCursorPos(16,7)
print("Password Authorized")
redstone.setOutput("right", true)
sleep(5)
redstone.setOutput("right", false)
else
if pass == "computers" then
print("Welcome Admin")
door = true
else
if pass == "leave" then
redstone.setOutput("right", true)
sleep(5)
redstone.setOutput("right", false)
else
i = i + 1
while alarm == false do
if (i == 1) then
print("Incorrect 2 more trys")
alarm = true
end
if (i == 2) then
print("Incorrect 1 more try")
alarm = true
end
if (i == 3) then
print("Alarm Activated")
print("Administrator is being advised")
rednet.send(id, "alarm")
alarm = true
end
end
end
end
end
end
GravityScore #2
Posted 28 January 2013 - 02:25 PM
Firstly, please use
 and 
tags to surround your code, and please indent it to make it readable, so we can check if you're missing an end, etc…

I'll step through the code:

os.shutdown = os.shutdown accomplishes nothing. It has no effect on the code at all. It's like saying

local a = "hello"
a = a

And I also think you don't understand the concept of elseif. Here is an example:

var = "hello"
if var == "hello" then
  -- Will occur if var is equal to hello
elseif var == "not hello" then
  -- If var does not equal hello, and also equals "not hello", then this will execute
elseif var == "test" then
  -- If var does not equal any of the above, and var equals "test", this will execute
elseif var == "meh" then
  -- Again, if var does not equal any of the above, and var equals "meh", then this will execute
else
  -- If none of the above are true, then this will execute
end

So basically this:

else
  if pass == "computers" then
  -- Code here
Should be this:

elseif pass == "computers" then
  -- Code here

Next, you don't need that while loop at all in the else statement. You can make do without. You should also use elseif statements when checking i == 3, i == 2, etc…

Fixed code:
Spoiler

os.pullEvent = os.pullEventRaw
local mon = peripheral.wrap("top")
i = 0
local alarm = false
local door = false
mon.clear()
mon.setTextScale(1)
mon.setCursorPos(1,1)
mon.write("Welcome to TyCo's")
mon.setCursorPos(1,3)
mon.write("CC store")
while door == false do
term.clear()
term.setCursorPos(14,1)
print("Welcome to Temporary Tyco")
term.setCursorPos(1,3)
term.write("Please Enter Password:")
pass = read("*")
if pass == "inside" then
  term.setCursorPos(16,7)
  print("Password Authorized")
  redstone.setOutput("right", true)
  sleep(5)
  redstone.setOutput("right", false)
elseif pass == "computers" then
  print("Welcome Admin")
  door = true
elseif pass == "leave" then
  redstone.setOutput("right", true)
  sleep(5)
  redstone.setOutput("right", false)
else
  i = i + 1
  if i == 1 then
   print("Incorrect 2 more trys")
  elseif i == 2 then
   print("Incorrect 1 more try")
  elseif i == 3 then
   print("Alarm Activated")
   print("Administrator is being advised")
   rednet.send(id, "alarm") -- ID here is nil. You are sending to nil. You need to change this.
  end
  -- OR you could just do this:
--  if i == 3 then
--   -- Activate alarm
--  else
--   print("Incorrect. " .. 3 - i .. " more tries.")
--  end
  end
end
tysciman7 #3
Posted 28 January 2013 - 03:59 PM
when i have one wrong password it says "2 more trys" (like it should) but then it breaks the program
ChunLing #4
Posted 28 January 2013 - 04:48 PM
Hmm…how did you copy the code into your computer?

Can you verify that each line containing an instance of the variable door is exactly as in the above post?
tysciman7 #5
Posted 29 January 2013 - 08:49 AM
I type it all in since i do this on my friends server yeah I can check