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

What is wrong here?

Started by KingofGamesYami, 07 April 2014 - 01:57 AM
KingofGamesYami #1
Posted 07 April 2014 - 03:57 AM
Normally I would never resort to simply posting my code and saying "What's wrong?", but I have been forced to resort to this as I have no idea what is wrong.

function promptForUser(...)
 local Args = {...}
 print("Please Enter Username & Password")
 term.write("Username: ")
 local Username = read()
 term.write("Password: ")
 local uPass = read("*")
 local User = {}
 local cPass = {}
 for i = 1, #Args/2, 2 do
  User[i] = Args[i]
  cPass[i] = Args[i+1]
 end
 for k,v in ipairs(User) do
  if v == Username then
   local x = k
  end
 end
 if Username == User[x] and uPass == cPass[x] then
  print("Welcome "..Username.."!")
  return Username
 else
  print("Incorrect Username or Password!")
  return promptForUser(...)
 end
end
The only thing I can say is the error seems to be related to defining x. If I forcefully change x to 1, the values input for that username & password return correctly and are accepted if correct.
Bomb Bloke #2
Posted 07 April 2014 - 04:06 AM
Normally I would never resort to simply posting my code and saying "What's wrong?", but I have been forced to resort to this as I have no idea what is wrong.

If you're not aware that anything's wrong, then why are you posting in the first place? ;)/> There's always symptoms to describe.

Anyway, in this case you're lucky and the issue is obvious - variable localisation. When you declare "x" as local, you're doing so within an "if" block - hence "x" only exists within it. Guess what happens when you try to access that variable outside of that block.

Try something like this:

 local x  -- "x" is now local to the whole function.
 for k,v in ipairs(User) do
  if v == Username then
   x = k
  end
 end

Another point, I don't much like the look of this:

 for i = 1, #Args/2, 2 do
  User[i] = Args[i]
  cPass[i] = Args[i+1]
 end

Because you've not included the rest of your script, I can't say whether it'll do what you want. I rather suspect, however, that you'd want to ditch the "/2".
KingofGamesYami #3
Posted 07 April 2014 - 04:39 AM
-snip-

If you're not aware that anything's wrong, then why are you posting in the first place? ;)/> There's always symptoms to describe.

Anyway, in this case you're lucky and the issue is obvious - variable localisation. When you declare "x" as local, you're doing so within an "if" block - hence "x" only exists within it. Guess what happens when you try to access that variable outside of that block.

Try something like this:

 local x  -- "x" is now local to the whole function.
for k,v in ipairs(User) do
  if v == Username then
   x = k
  end
end
Thanks! That definitely sounds like the problem.
Another point, I don't much like the look of this:

 for i = 1, #Args/2, 2 do
  User[i] = Args[i]
  cPass[i] = Args[i+1]
end

Because you've not included the rest of your script, I can't say whether it'll do what you want. I rather suspect, however, that you'd want to ditch the "/2".
What I am doing there is simply splitting the arguments into two different arrays. Yes, I do want to divide by two because if I don't, it will run twice as many times as needed (due to the increment being 2) Edit: After testing it, yes I do need to take out that.
Edit: This is the entire script. It is a function.
If you mean how I use it:

os.loadapi("api")
User = api.promptForUser("Username", "Password", "Username", "Password", "Username", "Password") --#note you can have infinite usernames and passwords
if User == "Username" then
--#do this (haven't actually made it do anything here yet)
else
--# do something else
end
Edited on 07 April 2014 - 02:51 AM