Jump to content




Login/Password program issues


6 replies to this topic

#1 jewelshisen

  • Members
  • 164 posts

Posted 20 April 2014 - 04:39 PM

local UserList = {"Jewel", "Tester"}
local PassList = {"Neopet", "Bacon"}
rednet.open("top")
while true do
  local ID, User, Security = rednet.receive()
    for i=1, 2 do
	  local check = UserList[i]
	  if check == User then
	    local check2 = PassList[i]
	    if check == User and check2 == Security then
		  local UserCheck = true
		  break
	    else
		  local UserCheck = false
	    end
	  end
    end
  sleep(1)
  if UserCheck == true then
    rednet.send(ID, "Clear")
  else
    rednet.send(ID, "Invalid Username or Password")
  end
end


Can somebody help me understand what is going wrong here? It always returns false for me.

#2 CometWolf

  • Members
  • 1,283 posts

Posted 20 April 2014 - 04:45 PM

It actually returns nil, since you localize the userCheck variable to the last if statement.
			if check == User and check2 == Security then
				  local UserCheck = true
				  break
			else
				  local UserCheck = false
			end
Also, a better way of handling this would be to store the passwords in a table with the usernames as the key.
local tAcess = {
  Jewel = "Neopet",
  Tester = "Bacon"
}
--
if tAcess[user] == password then
  --login succeeded
else
  --login failed
end

Edited by CometWolf, 20 April 2014 - 04:45 PM.


#3 Dayrider10

  • Members
  • 32 posts

Posted 20 April 2014 - 08:49 PM

So about the if statements you don't need to check the user again on the second if statement. Now about it returning false is this:

rednet.receive() returns three different parameters. It returns the ID the message and then the distance. So in this case it is ID, User, Security = rednet.recieve(). So in your code your saying if check == User (which is in the table) and check2 == the distance computer A is from computer B. Try this instead:

users = {"User1", "User2"}
password = {"Password1", "Password2"}
rednet.open("top")
senderId, message, distance = rednet.receive()
for i = 1, 2 do
	 check = users[i]
	  if check == message then
			ID2, message2, distance2 = rednet.receive()
			check2 = password[i]
			 if check2 == message2 then
				  print("True")
			  else
				   print("False")
			 end
	  end
end

Obviously you are going to send twice in this case.

Edited by Dayrider10, 20 April 2014 - 08:49 PM.


#4 jewelshisen

  • Members
  • 164 posts

Posted 20 April 2014 - 08:55 PM

View PostDayrider10, on 20 April 2014 - 08:49 PM, said:

So about the if statements you don't need to check the user again on the second if statement. Now about it returning false is this:

rednet.receive() returns three different parameters. It returns the ID the message and then the distance. So in this case it is ID, User, Security = rednet.recieve(). So in your code your saying if check == User (which is in the table) and check2 == the distance computer A is from computer B. Try this instead:

users = {"User1", "User2"}
password = {"Password1", "Password2"}
rednet.open("top")
senderId, message, distance = rednet.receive()
for i = 1, 2 do
	 check = users[i]
	  if check == message then
			ID2, message2, distance2 = rednet.receive()
			check2 = password[i]
			 if check2 == message2 then
				  print("True")
			  else
				   print("False")
			 end
	  end
end

Obviously you are going to send twice in this case.

Actually it only passes distance if you do not provide the protocol. So in this case you are incorrect.

#5 Dayrider10

  • Members
  • 32 posts

Posted 20 April 2014 - 11:00 PM

Are you sure? I'm pretty sure that you need three parameters for rednet.receive() and you are using distance as the password which returns false all the time

#6 CometWolf

  • Members
  • 1,283 posts

Posted 20 April 2014 - 11:12 PM

Please try to keep up with recent updates...

Quote

Waits until it received a rednet message of the specified protocol has been received, or until timeout seconds have passed. Leave args empty to wait for any message indefinitely. If only a single, numerical argument is passed, will wait that many seconds for a message of any protocol. Versions of ComputerCraft prior to 1.6 may return the distance to the transmitting computer - 1.6 or later returns message protocols instead, though distance can still be obtained via direct use of the Modem API.
http://computercraft...ednet_%28API%29

#7 jewelshisen

  • Members
  • 164 posts

Posted 20 April 2014 - 11:43 PM

View PostDayrider10, on 20 April 2014 - 11:00 PM, said:

Are you sure? I'm pretty sure that you need three parameters for rednet.receive() and you are using distance as the password which returns false all the time

View PostCometWolf, on 20 April 2014 - 11:12 PM, said:

Please try to keep up with recent updates...

Quote

Waits until it received a rednet message of the specified protocol has been received, or until timeout seconds have passed. Leave args empty to wait for any message indefinitely. If only a single, numerical argument is passed, will wait that many seconds for a message of any protocol. Versions of ComputerCraft prior to 1.6 may return the distance to the transmitting computer - 1.6 or later returns message protocols instead, though distance can still be obtained via direct use of the Modem API.
http://computercraft...ednet_%28API%29

Exactly. Distance was returned prior to the 1.6 update.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users