Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Checking TextBox test against a table?

So I'm trying to make a GUI where you enter codes for benefits, and it checks your code against a table.

sp = script.Parent
validCodes = {
	["Test"] = true;
	}
sp.TextButton.MouseButton1Click:connect(function()
	if sp.Input.Text[validCodes] then --Here is where I don't know what to do, I'm not sure if it is right.
		sp.TextButton.Text = "Success!"
		sp.Input.Text = ""
	else
		sp.TextButton.Text = "Error"
		sp.Input.Text = ""
		wait(3)
		sp.TextButton.Text = "Submit"
		sp.Input.Text = ""
	end
end)

1 answer

✓ Accepted answer

You were so close! Here, I'll help you fix it!

What you are trying to use (and honestly should be using) is a Dictionary! They are similar to tables, except that you can set their keys as well as the values (whereas in tables, the keys are always numbers). The problem you had is the fact that you aren't referencing the Dictionary in the correct way. First you have to put the Dictionary's name, and then put the reference to a specific spot in it. Here's an example: validCodes[sp.Input.Text]. I fixed your code up below. Also, when you put references into a dictionary, if the key is a complete string without numbers or special characters, you can remove the quotes and the brackets. I changed that just because I personally believe it looks nicer :)

Also, when writing entries in the Dictionary, they should all be separated by commas. You can also put a comma after the last entry, but it isn't required.

sp = script.Parent
validCodes = {
    Test = true,
    Example = true
    }
sp.TextButton.MouseButton1Click:connect(function()
    if validCodes[sp.Input.Text] then
        sp.TextButton.Text = "Success!"
        sp.Input.Text = ""
    else
        sp.TextButton.Text = "Error"
        sp.Input.Text = ""
        wait(3)
        sp.TextButton.Text = "Submit"
        sp.Input.Text = ""
    end
end)

If you need any help or it doesn't work correctly, just ask! (I am typing this on an iPad after all. Haha)

3 comments

0

Also, you don't need to set the values in the dictionary to true for the validCodes[] line to work. If the validCodes[] line is given a non-existing key, it will return nil and not run. If it does find the key, it'll return the value.

0

Great answer.

1

Thanks :)

This discussion is preserved as it was. New answers, comments, and votes are closed.