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

[Lua][Help]If / Then Statement - help

Started by ChaddJackson12, 17 October 2012 - 08:34 PM
ChaddJackson12 #1
Posted 17 October 2012 - 10:34 PM
Please help me. I have been trying to do this for a while. What I want to happen is when someone is typing something in, what they type in isn't blank. Like " ". And that what they type in, exists, as a file. Here is some code to get a better idea.


input = read()
if not input == nil and fs.exists(input) then
   print("Success")
else
   print("Failure")
end

Looks to me that it would work. I have tried "nil" and the quotes. It doesn't, it prints "failure".

I also have tried not checking for blank input, this prints "success" when they type nothing in. Please help me. Thanks in advance.
Doyle3694 #2
Posted 17 October 2012 - 10:53 PM

input = read()
if input and fs.exists(input) then
   print("Success")
else
   print("Failure")
end

a string will return true if it's not nil :P/>/>
ChaddJackson12 #3
Posted 17 October 2012 - 10:54 PM

input = read()
if input and fs.exists(input) then
   print("Success")
else
   print("Failure")
end

a string will return true if it's not nil :P/>/>
I tried that just now, it doesn't work. Here is the actual code that I am working with… (I changed it for simplicity, but…) Also, the uRoot, and whatever else is defined in the actual file, so that isn't the problem.



write("Username: ")
x, y = term.getCursorPos()
write("nPassword: ")
w, h = term.getCursorPos()
term.setCursorPos(x, y)
Username = read()
term.setCursorPos(w, h)
Password = read("*")
if Username and fs.exists(uRoot .. Username) then
pr = fs.open(uRoot .. Username, "r")
CheckPassword = pr.readLine()
pr.close()
if Password == CheckPassword then
lw = fs.open(sRoot .. "LoggedIn", "w")
lw.writeLine(Username)
lw.close()
print("nLogged In Successfully!")
os.sleep(2)
LoggedIn = true
Function()
else
print("nInvalid Username / Password Combination2!")
os.sleep(2)
end
else
print("nInvalid Username / Password Combination1!")
os.sleep(2)
end
It goes to the part where it is supposed to open the file, and read the password (Right after the IF statement)…
Please help with that. THanks
jag #4
Posted 17 October 2012 - 11:16 PM
Do you get any errors? If so, please post them!
ChaddJackson12 #5
Posted 17 October 2012 - 11:21 PM
Do you get any errors? If so, please post them!
Here is the actual code:


Username = read()
term.setCursorPos(w, h)
Password = read("*")
if fs.exists(uRoot .. Username) then
pr = fs.open(uRoot .. Username, "r")
CheckPassword = pr.readLine()
pr.close()
if Password == CheckPassword then
lw = fs.open(sRoot .. "LoggedIn", "w")
lw.writeLine(Username)
lw.close()
print("nLogged In Successfully!")
os.sleep(2)
LoggedIn = true
Function()
else
print("nInvalid Username / Password Combination2!")
os.sleep(2)
end
else
print("nInvalid Username / Password Combination1!")
os.sleep(2)
end

Here is the Error: Filename:6: Attempt to index ? (a nil value) - If they enter nothing in for the username/password

If I have it check for a nil value for Username, then it does the 'else'.
jag #6
Posted 17 October 2012 - 11:31 PM
You could just do
if input ~= "" then
Wouldn't that work?
ChaddJackson12 #7
Posted 17 October 2012 - 11:32 PM
You could just do
if input ~= "" then
Wouldn't that work?
Let me try
ChaddJackson12 #8
Posted 17 October 2012 - 11:33 PM
You could just do
if input ~= "" then
Wouldn't that work?
Let me try
Nope, has that error…
jag #9
Posted 17 October 2012 - 11:33 PM

if not input == nil and fs.exists(input) then
Oh yeah, you can't either do not and == in the same statement, try ~= or as Doyle said do just input.
jag #10
Posted 17 October 2012 - 11:35 PM
You could just do
if input ~= "" then
Wouldn't that work?
Let me try
Nope, has that error…
Well try
if input and input ~= "" then
ChaddJackson12 #11
Posted 17 October 2012 - 11:39 PM
You could just do
if input ~= "" then
Wouldn't that work?
Let me try
Nope, has that error…
Well try
if input and input ~= "" then

Here I tried this, without the AND, look:


Username = read()
term.setCursorPos(w, h)
Password = read("*")
if not Username ~= "" and Username then
   if fs.exists(uRoot .. Username) then
   pr = fs.open(uRoot .. Username, "r")
   CheckPassword = pr.readLine()
   pr.close()
   if Password == CheckPassword then
	  lw = fs.open(sRoot .. "LoggedIn", "w")
	  lw.writeLine(Username)
	  lw.close()
	  print("nLogged In Successfully!")
	  os.sleep(2)
	  LoggedIn = true
	  Function()
   else
	  print("nInvalid Username / Password Combination2!")
	  os.sleep(2)
	  end
   end
else
   print("nInvalid Username / Password Combination1!")
   os.sleep(2)
end
This still has that error around the readLine statement.

I put Invalid Username / Password Combination1 and 2, to tell where it was failing if it were to work. It will go the the "1" if I have it check like this "If username == nil"
jag #12
Posted 17 October 2012 - 11:45 PM
What's in the variables uRoot and sRoot?
ChaddJackson12 #13
Posted 17 October 2012 - 11:48 PM
What's in the variables uRoot and sRoot?
What's in the variables uRoot and sRoot?

This:

uRoot = ".Extension/Users/"
sRoot = ".Exienstion/Security/" -- Used for locks, but is used in telling whether the user is logged in or not. (See Below)
if fs.exists(sRoot .. "LoggedIn") then
   LoggedIn = true
else
   LoggedIn = false
end
-- That statement works.
jag #14
Posted 17 October 2012 - 11:50 PM
This:

uRoot = ".Extension/Users/"
sRoot = ".Exienstion/Security/" -- Used for locks...
I don't know if you misspelled Exienstion or if your folder is named that.
But otherwise, are you doing this on a server, because servers can have some weird errors some times…
ChaddJackson12 #15
Posted 17 October 2012 - 11:52 PM
This:

uRoot = ".Extension/Users/"
sRoot = ".Exienstion/Security/" -- Used for locks...
I don't know if you misspelled Exienstion or if your folder is named that.
But otherwise, are you doing this on a server, because servers can have some weird errors some times…
Not on a server, I will redo the code. Also, that was misspelled only here on the forums.
jag #16
Posted 17 October 2012 - 11:56 PM
Ok, well then I really can't find any major error in your code.
Hope that you get it right when you rewrite it!
ChaddJackson12 #17
Posted 18 October 2012 - 12:00 AM
Ok, well then I really can't find any major error in your code.
Hope that you get it right when you rewrite it!
Thanks for the help. I don't understand the error either, because I have wrote quite a few (working) login systems.. :/
ChaddJackson12 #18
Posted 18 October 2012 - 12:33 AM
Ok, well then I really can't find any major error in your code.
Hope that you get it right when you rewrite it!
Thanks for the help. I don't understand the error either, because I have wrote quite a few (working) login systems.. :/

I fixed it, by adding an extention to the username… This is how:

if fs.exists(uRoot .. Username .. uExtention, "r") then

It isn't the best because I wasn't planning on extentions for usernames, but at least it works… :P/>/>
Doyle3694 #19
Posted 18 October 2012 - 07:57 AM
you still have 'if not input ~= "" '. Just 'if input ~= "" ' will return true if the value is not "". That if statement basically said ' if input is not not equal to "" '

Short said, double invertion
ChaddJackson12 #20
Posted 18 October 2012 - 05:28 PM
you still have 'if not input ~= "" '. Just 'if input ~= "" ' will return true if the value is not "". That if statement basically said ' if input is not not equal to "" '

Short said, double invertion
Ok well I got if fixed, (Without actually finding the error). But, I tried what you said, what ​jag_e_nummer_ett said, nothing would work. I did not see a double inversion, but what I wanted to do was NOT have the input == nil, so I put "if not input == "" " and that did not work, even with input. I also tried inverting it to have what you said, and that did not work, because it was basically just the same thing.