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

How to make multiple users on a computer.

Started by WizardHD, 06 November 2013 - 09:32 AM
WizardHD #1
Posted 06 November 2013 - 10:32 AM
I based my coding off a previous post on these forums, and I thought I'd cracked the code but turns out I hadn't and ended locking myself out of my PC.

oldPull = os.pullEvent
os.pullEvent = os.pullEventRaw
while true do
		term.clear()
		term.setCursorPos(1,1)
		print("Borta Computer Security 1.1")
		write("Username: ")
		if read() == "WizardHD" then
		write("Password: ")
		if read("*") == "pass1" then
				print("Welcome back, WizardHD!")
				sleep(2)
				term.clear()
				term.setCursorPos(1,1)
				break
		elseif read() == "DrSandwich" then
				write("Password: ")
				if read("*") == "pass2" then
				print("Welcome back, DrSandwich!")
				sleep(2)
				term.clear()
				term.setCursorPos(1,1)
				break
	  else
				print ("Access Denied.")
				sleep(1)
		end
end
os.pullEvent = oldPull

This worked with WizardHD user, but whenever DrSandwich was typed in as the username it restarted the whole thing and took you to where you type in your username.

This is probably a really stupid mistake or something, I've tried coding things before but never been bothered to read the books and this is fun and easy and in a game so I started on this, so in essence this is my first day of coding.

All help will be appreciated! Thanks.
Lyqyd #2
Posted 06 November 2013 - 08:46 PM
Split into new topic.

In order to log in as DrSandwich, you'd have to type in the other username, then at the password prompt, type in something wrong, then type in DrSandwich's username. See if you can figure out why. :)/>
WizardHD #3
Posted 07 November 2013 - 11:50 AM
Is it because the elseif is if the password's not WizardHD's, not because the username's not WIzardHD's?
Edited on 07 November 2013 - 10:55 AM
ElvishJerricco #4
Posted 07 November 2013 - 12:17 PM
Hint, it's a syntax mistake, not a logic error.
Lyqyd #5
Posted 07 November 2013 - 12:28 PM
Hint, it's a syntax mistake, not a logic error.

It's both.
WizardHD #6
Posted 07 November 2013 - 03:43 PM
I can't find the syntax error.
ElvishJerricco #7
Posted 07 November 2013 - 03:56 PM
Hint, it's a syntax mistake, not a logic error.

It's both.

Oh yea I missed the other thing.

Anyway, you forgot the "end" keywords at the end of your if statements.
Edited on 07 November 2013 - 02:57 PM
WizardHD #8
Posted 07 November 2013 - 05:20 PM
Aight, I reattempted. I managed to not lock myself out this time at least, WizardHD works but DrSandwich just messes up.

Code in screenshot form (sorry):

http://puu.sh/5bFHg
http://puu.sh/5bFI0
http://puu.sh/5bFIn.png
Micheal Pearce #9
Posted 07 November 2013 - 06:23 PM
this should work

os.pullEvent = os.pullEventRaw
users = { -- list of users that are accepted on the computer
WizardHD = "pass1";
DrSandwich = "pass2";
}
while true do
term.clear()
term.setCursorPos(1,1)
write("username: ")
term.setCursorPos(1,2)
write("password: ")
term.setCursorPos(11,1)
user = read()
term.setCursorPos(11,2)
pass = read("*")
if users[user] ~= null and pass == users[user] then -- checks if user exists and if the password is correct
  term.setCursorPos(5,3)
  write("Welcome Back, "..user)
  break
else
  print("Access Denied.")
  sleep(1)
end
end
MudkipTheEpic #10
Posted 08 November 2013 - 03:08 PM
this should work

os.pullEvent = os.pullEventRaw
users = { -- list of users that are accepted on the computer
WizardHD = "pass1";
DrSandwich = "pass2";
}
while true do
term.clear()
term.setCursorPos(1,1)
write("username: ")
term.setCursorPos(1,2)
write("password: ")
term.setCursorPos(11,1)
user = read()
term.setCursorPos(11,2)
pass = read("*")
if users[user] ~= null and pass == users[user] then -- checks if user exists and if the password is correct
  term.setCursorPos(5,3)
  write("Welcome Back, "..user)
  break
else
  print("Access Denied.")
  sleep(1)
end
end

Nil is Lua's null.
jay5476 #11
Posted 09 November 2013 - 10:11 PM
even though if null isn't defined its nil :P/>
Ninja_swag #12
Posted 23 November 2013 - 12:35 AM
try this


if not fs.exists("users") then
fs.makeDir("users")
shell.run("startup")
--if users directory doesnt exist then it makes it
else
  local check = fs.open("users/"..username, "r") --opens file
  local pass = check.readLine() --reads the first line from file
  write("username: ")
  local username = read()
  write("password: ")
  local password = read()
  if not fs.exists("users/"..username) then
   print("user does not exist")
   os.reboot()
else --if user exists then
   if password == pass then --if password is correct
    term.clear()
    term.setCursorPos(1,1)
  else
   print("password was incorrect")
   os.reboot
  end
end
--to create a user file run this command "edit users/yourusernamehere" and write the password in and save