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

[FIXED]Whats Wrong with this Function? (Not Error)

Started by 3ydney, 06 March 2013 - 06:56 PM
3ydney #1
Posted 06 March 2013 - 07:56 PM
Well if I type login("username1", "password1") it returns false when it should return true.

Here is the function code


function login(arg1, arg2)
local arg1, arg2 = nil
local user = { "username1" }
local pass = { "password1" }
if app.debug == true then
  for i, v in ipairs(user) do
   if arg1 == v then
	local valid = true
	local password = pass[i]
	break
   else
	return false
   end
  end
  if arg2 == password then
   return true
  else
   return false
  end
end
end
tesla1889 #2
Posted 06 March 2013 - 08:20 PM
well, you set arg1 and arg2 to nil, so it will never work
get rid of that line and it will
you dont have to declare variables if they are arguments in a function
etopsirhc #3
Posted 06 March 2013 - 08:21 PM
well it could be the local arg1,arg2 = nil inside the function

XP beat me to it
Doyle3694 #4
Posted 06 March 2013 - 08:22 PM
Only arg1 is set to nil. @tesla1889

Also, @FreePaidRemake, replace that for loop's return false with some fancy check if a user has been selected, right now it will just run once.
immibis #5
Posted 06 March 2013 - 09:39 PM
Also, local variables are only visible inside the block they're declared in.


if 5 < 6 then
  local test = "hi"
  print(tostring(test)) -- prints: hi
end
print(tostring(test)) -- prints: nil
tesla1889 #6
Posted 08 March 2013 - 11:17 PM
Only arg1 is set to nil. @tesla1889
–snip–
if you have more variables than values in parallel assignment, the last variables are set to nil
Doyle3694 #7
Posted 09 March 2013 - 01:08 AM
The linked page tells nothing about such assignment logic. You could very well be right but that page doesn't tell me anything in terms of logic like that
Lyqyd #8
Posted 09 March 2013 - 04:25 AM
I will not be locking this. We generally don't lock Ask a Pro topics.