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

[Lua]Login system with multiple users

Started by lysdal, 01 July 2012 - 11:10 AM
lysdal #1
Posted 01 July 2012 - 01:10 PM
On a tekkit server me and my 2 friends share a pc. I made a login system but i cant get it to work!


term.clear()
term.setCursorPos(1,1)
lysdalpass = "lasse09maia"
jimmypass = "null"
sammepass = "null"
write("Username: ")
user = read()
if user == "lysdal" then
	write("Password: ")
	p1 = read()
	if p1 == lysdalpass then
		print("Welcome Lysdal!")
	else
		print("Password Incorrect!")
		sleep(2)
		os.shutdown()
	end
end
if user == "jimmy" then
	write("Password: ")
	p2 = read()
	if p2 == jimmypass then
		print("Welcome Jimmy!")
	else
		print("Password Incorrect!")
		sleep(2)
		os.shutdown()
	end
end
if user == "samme" then
	write("Password: ")
	p3 = read()
	if p3 == sammepass then
		print("Welcome Samme!")
	else
		print("Password Incorrect!")
		sleep(2)
		os.shutdown()
	end
end
if not user == "samme" or "lysdal" or "jimmy" then
	print("Username Incorrect!")
	sleep(2)
	os.shutdown()
end

All goes fine until i write my password, then it says "Username Incorrect!" any fixes?
MysticT #2
Posted 01 July 2012 - 03:35 PM
You should use elseif and else, so it doesn't check all the conditions. The problem is that the last if:

if not user == "samme" or "lysdal" or "jimmy" then
	print("Username Incorrect!")
	sleep(2)
	os.shutdown()
end
Checks if the username is not "samme", or the strings "lysdal" and "jimmy" are not nil, wich will always be true, so it always enters the if and shutdows the computer.

Fixed code (might have other errors, haven't tested it):

term.clear()
term.setCursorPos(1,1)
lysdalpass = "lasse09maia"
jimmypass = "null"
sammepass = "null"
write("Username: ")
user = read()
if user == "lysdal" then
	write("Password: ")
	p1 = read()
	if p1 == lysdalpass then
		print("Welcome Lysdal!")
	else
		print("Password Incorrect!")
		sleep(2)
		os.shutdown()
	end
elseif user == "jimmy" then
	write("Password: ")
	p2 = read()
	if p2 == jimmypass then
		print("Welcome Jimmy!")
	else
		print("Password Incorrect!")
		sleep(2)
		os.shutdown()
	end
elseif user == "samme" then
	write("Password: ")
	p3 = read()
	if p3 == sammepass then
		print("Welcome Samme!")
	else
		print("Password Incorrect!")
		sleep(2)
		os.shutdown()
	end
else
	print("Username Incorrect!")
	sleep(2)
	os.shutdown()
end

BTW, the correct if would be:

if user ~= "samme" and user ~= "lysdal" and user ~= "jimmy" then
but it's better to use the else.