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

My First Login System

Started by Noobly, 24 October 2012 - 10:45 PM
Noobly #1
Posted 25 October 2012 - 12:45 AM
Another one of my beginning programs. Again, I am open to suggestions on how to make my programs better, or just hints on how to be a better programmer overall.

term.clear()
term.setCursorPos(1,1)
io.write("What is your name?\n")
name = io.read()
if name == "Name" then
  print("Welcome, Name. What is the password?")
  password = io.read()
   if password == "Password" then
	print("Access granted! You may now use this computer.")
   else
	print("Access denied!")
  end
else
  print("Access denied!")
end
ChunLing #2
Posted 25 October 2012 - 01:37 AM
Well, you should probably do something to lock up the computer until the right password is entered.

So, use a repeat until loop around the whole password thing, and don't let the program exit until the correct password is entered. There is also a trick for preventing the user from using Ctrl+T to escape your program, you should look that up.

Actually, you should have the repeat until around pretty much the whole program you've got now, because you're going to want to repeat the term.clear and asking for a name as well.

Then, when you've got that going, you can have a three strikes and you're in hot lava with activated TNT rule. If you use mining turtles rather than sticky pistons on the floor, you can have an obsidian trap floor pull. That should surprise someone who didn't read this post.
Shazz #3
Posted 25 October 2012 - 01:43 AM
Another one of my beginning programs. Again, I am open to suggestions on how to make my programs better, or just hints on how to be a better programmer overall.

term.clear()
term.setCursorPos(1,1)
io.write("What is your name?n")
name = io.read()
if name == "Name" then
  print("Welcome, Name. What is the password?")
  password = io.read()
   if password == "Password" then
	print("Access granted! You may now use this computer.")
   else
	print("Access denied!")
  end
else
  print("Access denied!")
end

That very good for a first login program. Here is some things you should consider.
* Use local variables. Ex:
local x = 2
instead of just
x = 2


* Since it is a login script, you might want to change
io.read()
to
io.read("*")
. This will change every character the user inputs into a '*', which means other people (on SMP) won't be able to read what you are typing. However the function will still return what the user typed in (not the '*'s).

* You can use 'write()' and 'read()' instead of 'io.write()' and 'io.read()'. It just makes it shorter, not much difference really.

* From what I can see, the program exits to shell after you put in a password (either wrong or right). To avoid this, use a while loop. Ex:

while true do
  term.clear()
  term.setCursorPos(1,1)
  io.write("What is your name?n")
  name = io.read()
  if name == "Name" then
	print("Welcome, Name. What is the password?")
	password = io.read()
	if password == "Password" then
	  print("Access granted! You may now use this computer.")
	  break
	else
	  print("Access denied!")
	end
  else
	print("Access denied!")
  end
end
. This will run the code again unless the password was correct. 'break' escapes out of the loop.

* As your program is right now, the user can still CTRL+T out of your program. There is a way to prevent this. I'll let you search for that.

That's all I can offer, hope you take this advice into consideration.
Noobly #4
Posted 25 October 2012 - 02:52 AM
I thank you both on your feedback, and it helped me out a lot.
Dlcruz129 #5
Posted 25 October 2012 - 03:08 AM
Also, to make setting up users a little easier, use the file system. There are plenty of tutorials out there, and while it is quite tricky to grasp at first, it will work wonders later.
Auesome_man #6
Posted 10 March 2013 - 03:25 PM
In order for users to not be able to Ctrl-T out of it do this line as your first line:

os.pullEvent = os.pullEventRaw
Spongy141 #7
Posted 10 March 2013 - 03:35 PM
In order for users to not be able to Ctrl-T out of it do this line as your first line:

os.pullEvent = os.pullEventRaw
Lol thats what I was going to say, but no offense… you spelled awesome wrong.
theoriginalbit #8
Posted 10 March 2013 - 03:38 PM
In order for users to not be able to Ctrl-T out of it do this line as your first line:

os.pullEvent = os.pullEventRaw
If you're going to do that do it this way

At the top of your code

local oP = os.pullEvent
os.pullEvent = os.pullEventRaw

then at the end of your code do this

os.pullEvent = oP

This way it restores the pull event back to what it was so programs can be terminable after the script runs
Shnupbups #9
Posted 10 March 2013 - 03:40 PM
NECROMANCY

NOOOOOOOO
SuicidalSTDz #10
Posted 10 March 2013 - 03:41 PM
There is no need to use io.read(). Use read() instead. Other than that, it looks solid. I like your avatar btw. ^_^/>/>

Uh oh. Erm. It was all Dlcruz, I swear! *runs away quickly*
Dlcruz129 #11
Posted 10 March 2013 - 05:37 PM
There is no need to use io.read(). Use read() instead. Other than that, it looks solid. I like your avatar btw. ^_^/>/>/>

Uh oh. Erm. It was all Dlcruz, I swear! *runs away quickly*

This means war. I'm just gonna look at your first few posts, if you don't mind. ;)/>
Cranium #12
Posted 10 March 2013 - 06:39 PM
*sigh*
Why does this need to be reopened?