Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Password Accessable Gui (Keys Table) only working with one value in table! [help plz?]

Asked by 4 years ago

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?

1 answer

Log in to vote
0
Answered by 4 years ago

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.

0
Thank you very much TheBigBro122 Its_Monkey 27 — 4y
0
You're welcome TheBigBro122 427 — 4y
Ad

Answer this question