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

Login/Password program issues

Started by jewelshisen, 20 April 2014 - 03:39 PM
jewelshisen #1
Posted 20 April 2014 - 05: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.
CometWolf #2
Posted 20 April 2014 - 05: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 on 20 April 2014 - 03:45 PM
Dayrider10 #3
Posted 20 April 2014 - 09: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 on 20 April 2014 - 07:49 PM
jewelshisen #4
Posted 20 April 2014 - 09:55 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.

Actually it only passes distance if you do not provide the protocol. So in this case you are incorrect.
Dayrider10 #5
Posted 21 April 2014 - 12:00 AM
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
CometWolf #6
Posted 21 April 2014 - 12:12 AM
Please try to keep up with recent updates…
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.info/wiki/Rednet_%28API%29
jewelshisen #7
Posted 21 April 2014 - 12:43 AM
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
Please try to keep up with recent updates…
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.