Hello! This is kind of following up with my last question about the password gui, anyway I get that it's unsecure and all but it's a test. Anyway, I've got a table. Here's it:
local Keys = { "Key1", "Key2", "Key3" }
And with the TextButton and TextBox, I have this script under the TextButton:
script.Parent.MouseButton1Down:Connect(function() for _, Key in pairs(Keys) do if script.Parent.Parent.TextBox.Text == Key then script.Parent.Parent.OutputBox.Text = "Key activated successfully!" else script.Parent.Parent.OutputBox.Text = "Incorrect key, please try again!" end end end)
The issue I am having with this is Everytime I type in a key from the table such as Key1, or Key2, it doesn't work. But Key3 does. Can someone please help explain this to me?
I wrote the code that you have, and I see the problem. I dunno if this is a common problem or not. But this code should work
local Keys = {"Key1", "Key2", "Key3"} script.Parent.MouseButton1Down:Connect(function() for _, Key in pairs(Keys) do if script.Parent.Parent.TextBox.Text == Key then script.Parent.Parent.OutputBox.Text = "Key activated successfully!" return end end script.Parent.Parent.OutputBox.Text = "Incorrect key, please try again!" end)
The problem of your code is the if statement. It's hard to explain so I'll try my best
For example... You're input was "Key2", so it would iterate over the Keys
table, so when it iterates the first element the for loop is going to see is "Key1", so in the if statement, the result
will be
if "Key2" == "Key1" then
because of that it would run the else statement instead, and only run the code when it iterates through "Key2".
So in the code that I wrote, it would iterate through as usual and if it's not the correct key it wouldn't do anything, if it does find the correct one it would run the code inside the if statement and returns (return makes the code after it not run). If it's not the correct key and it finished iterating, it would stop the for loop and run the "Incorrect key" code.
So I hope I did a pretty good explanation, cause I ain't good at doing one.