If statement in for loops will override checks

Hello, I am trying to make a white list system, the checks and stuff work well, only problem is when its valid, it will make a for loops through the instance with (:GetDescendants), and it would check the classnames for each particular instance. In this case, it just overrides and ignores the statement, and just continues with enabling the .Visible function. Any ideas?

Error:

Problem:

Code:

script.Parent.MouseButton1Click:Connect(function()
	local plr = game.Players.LocalPlayer
	local WL = require(script.Parent.Parent:WaitForChild("Panel"):WaitForChild("WL"))
	local whitelisted = WL.whitelisted
	for i,v in ipairs(whitelisted) do
		if plr.UserId == v then
			local opened = script.Parent.Parent:WaitForChild("Panel"):WaitForChild("Opened")
			if opened == false then
				opened = true
			else
				opened = false
			end
			if opened == true then
				for _, UI in pairs(script.Parent.Parent:WaitForChild("Panel"):GetDescendants()) do
					if (UI.ClassName == "TextButton" or "Frame" or "TextLabel") then
						UI.Visible = true
					end
				end
			else
				for _, UI in pairs(script.Parent.Parent:WaitForChild("Panel"):GetDescendants()) do
					if (UI.ClassName == "TextButton" or "Frame" or "TextLabel") then
						UI.Visible = false
					end
				end
			end
		end
	end
end)

Explorer:
image

You need to compare the classname each time:

if UI.ClassName == 'TextButton' or UI.ClassName == 'Frame' or UI.ClassName == 'TextLabel' then

Or if you want something shorter:

if UI:IsA('TextButton') or UI:IsA('Frame') or UI:IsA('TextLabel') then

Basically what yours is doing is checking if (UI.ClassName == ‘TextButton’) or (‘Frame’) or (‘TextLabel’), essentially checking if the string ‘Frame’ or the string ‘TextLabel’ is truthy–and it is, hence why the if-statement is evaluating to true; because strings are not false or nil.

1 Like

To shorten this further, you can just do:

if UI:IsA('GuiObject') then

That will return true if UI is something other than a frame, text label or text button though, eg. if it’s an image label or image button which I wasn’t sure if OP wanted.

1 Like