Code System not displaying a code is invalid

So, I am making a code system where players can enter a code and they get something from redeeming the code. Although for me it is not displaying when a code is invalid but when you type a valid code it displays it’s valid. Also, this is not going to be the full script so I know there are no checks to stop the player from redeeming the code again. If someone could help me fix this that would be good.

local plr = game.Players.LocalPlayer
local CodeInput = script.Parent.Parent:WaitForChild("CodeInput")
local ValidCode = script.Parent.Parent:WaitForChild("ValidCode")
local InvalidCode = script.Parent.Parent:WaitForChild("InvalidCode")

local Codes = {"Test","Test2"}

script.Parent.MouseButton1Click:Connect(function()
	if CodeInput.Text == Codes[1] or Codes[2] then
		if CodeInput.Text == Codes[1] then
			print("Code1 is valid!")
			InvalidCode.Visible = false
			ValidCode.Visible = true
		elseif CodeInput.Text == Codes[2] then
			print("Code2 is valid!")
			InvalidCode.Visible = false
			ValidCode.Visible = true
		end
		
	else
		print("Code is invalid!")
		ValidCode.Visible = false
		InvalidCode.Visible = true
	end
end)

This is invalid syntax. Proper syntax would be:

if CodeInput.Text == Codes[1] or CodeInput.Text == Codes[2] then

And even if it was valid, it’s not scalable. Use a finder function.

local function existsInTable(array, value)
    for _, v in pairs(array) do
        if v == value then
            return true
        end
    end
    return false
end

Place this above the MouseButton1Click connection and instead of what you wrote there for the if statement, replace it with the following:

if existsInTable(Codes, CodeInput.Text) then
2 Likes