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

This is a lock/computer login code

Started by Saxguy99, 02 April 2013 - 08:24 AM
Saxguy99 #1
Posted 02 April 2013 - 10:24 AM
name this code startup and then plug this in and edit where is says
if input == "open" then

you can change where it says open but keep the "s

heres the code


print ("password")

input = read()

if input == "open" then

redstone.setOutput("left", true)
sleep(3)
redstone.setOutput("left", false)
sleep(1)
print ("Access Granted")

else

end

os.reboot()
Spongy141 #2
Posted 02 April 2013 - 10:49 AM
Here I fixed you code, there was nothing wrong with it, other than you never needed to use os.reboot() and other things like redstone. can also be rs. also your spacing is kinda bad, also you can add a function to detect a side that has redstone, but I'll leave that up to you to add. But anyways, here:

while true do  -- No need to reboot the computer every time
  term.write("Password: ")
  input = read()
  os.pullEvent = os.pullEventRaw
  if input == ("open") then
	rs.setOutput("left", true)
	sleep(3)
	rs.setOutput("left", false)
	sleep(1)
	print("Access Granted")
   else
	print("Incorrect Password")
	sleep(2)
   end
end
remiX #3
Posted 02 April 2013 - 11:06 AM
You may want to put the write("Password: ") and input = read() inside the loop :)/>
robhol #4
Posted 02 April 2013 - 11:09 AM
Improvement on the improvement.


local password = "1234" -- declared here to make the program easier to edit
local side = "left" -- same
os.pullEvent = os.pullEventRaw -- just need to do this once, no point having it in the loop

while true do

  -- moved these here; need to do them every time or the program is useless
  term.write("Password: ")
  local input = read() -- use local unless you NEED a global, which you ideally shouldn't.
  --

  if input == password then --don't use () around strings, pointless and slightly confusing syntax
	print("Access granted.") --moved here, we want it to show at once rather than 4 seconds later; fixed capitalization and punctuation
	
	rs.setOutput(side, true)
	sleep(3)
	rs.setOutput(side, false)
	sleep(1)
   else
	print("Incorrect password.") -- capitalization and punctuation
	sleep(2)
   end
end

You could also do "if read() == password then" since you're not using the input variable anywhere else. That's just a remark though, not a recommendation.

Other tips:
- Don't show the password in plain text - read more about read()
- Modifying the program so you can actually exit it without a special disk without compromising the "security" is left as an exercise to the reader. Has to do with what you're doing with pullEvent..
KleptoKat #5
Posted 03 April 2013 - 01:32 PM
Improvement on the improvement.


local password = "1234" -- declared here to make the program easier to edit
local side = "left" -- same
os.pullEvent = os.pullEventRaw -- just need to do this once, no point having it in the loop

while true do

  -- moved these here; need to do them every time or the program is useless
  term.write("Password: ")
  local input = read() -- use local unless you NEED a global, which you ideally shouldn't.
  --

  if input == password then --don't use () around strings, pointless and slightly confusing syntax
	print("Access granted.") --moved here, we want it to show at once rather than 4 seconds later; fixed capitalization and punctuation
	
	rs.setOutput(side, true)
	sleep(3)
	rs.setOutput(side, false)
	sleep(1)
   else
	print("Incorrect password.") -- capitalization and punctuation
	sleep(2)
   end
end

You could also do "if read() == password then" since you're not using the input variable anywhere else. That's just a remark though, not a recommendation.

Other tips:
- Don't show the password in plain text - read more about read()
- Modifying the program so you can actually exit it without a special disk without compromising the "security" is left as an exercise to the reader. Has to do with what you're doing with pullEvent..
That's not even his code anymore…
robhol #6
Posted 03 April 2013 - 08:29 PM
Part of it is. :P/>

What's your point?
Saxguy99 #7
Posted 11 July 2013 - 10:34 PM
Hey guys this is a code i posted when i was a noob at programming i have a more and improved version
rhyleymaster #8
Posted 12 July 2013 - 02:12 PM
You should probably rename this to a door lock.
> Not a computer screen lock
Tjakka5 #9
Posted 12 July 2013 - 03:12 PM

local password = "12345"
local password2 = "doodlydoo"
local side = "left"
os.pullEvent = os.pullEventRaw
while true do
   term.clear()
   term.setCursorPos(1, 1)
   term.write("Password: ")
   local input = read(*)
	 if input == password then
	   print("Access granted.")
	   rs.setOutput(side, true)
	   sleep(3)
	   rs.setOutput(side, false)
	 elseif input == password2 then
		 break
	 else
	   print("Incorrect password.")
	   sleep(2)
	 end
  end
end
rhyleymaster #10
Posted 12 July 2013 - 05:32 PM
Well, Heres my go at your code.
Sorry for some errors. Just wrote it.

os.pullEvent = os.pullEventRaw
user = ("username")
pass = ("password")
side = ("left")
while true do
   term.clear()
   term.setCursorPos(1,1)
   write("Username:")
   local u = read()
   if u == user
   then
	  term.clear()
	  term.setCursorPos(1,1)
	  write("Password:")
	  local p = read("*")
	  if p == pass
	  then
	  term.clear()
	  term.setCursorPos(1,1)
	  print("Welcome!")
	  rs.setOutput(side, true)
	  sleep(3)
	  rs.setOutput(side, false)
	  else
	  term.clear()
	  term.setCursorPos(1,1)
	  print("Incorrect Username or Password.")
	  sleep(3)
	  end
   else
	  term.clear(1,1)
	  term.setCursorPos(1,1)
	  write("Password:")
	  local f = read("*")
	  if f == ("")
	  then
	  term.clear()
	  term.setCursorPos(1,1)
	  print("Incorrect Username or Password.")
	  sleep(3)
	  else
	  term.clear()
	  term.setCursorPos(1,1)
	  print("Incorrect Username or Password.")
	  sleep(3)
	  end
end