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.
What is wrong here?
Started by KingofGamesYami, Apr 07 2014 02:57 AM
2 replies to this topic
#1
Posted 07 April 2014 - 02: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.
#2
Posted 07 April 2014 - 03:06 AM
KingofGamesYami, on 07 April 2014 - 02:57 AM, said:
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?
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".
#3
Posted 07 April 2014 - 03:39 AM
Bomb Bloke, on 07 April 2014 - 03:06 AM, said:
KingofGamesYami, on 07 April 2014 - 02:57 AM, said:
-snip-
If you're not aware that anything's wrong, then why are you posting in the first place?
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
Bomb Bloke, on 07 April 2014 - 03:06 AM, said:
Another point, I don't much like the look of this:
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".
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".
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 by KingofGamesYami, 07 April 2014 - 03:51 AM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











