Why does this single line of code not work?

Am I typing this correctly?

if Selected.ClassName == "Frame" or "ImageButton" or "ImmageLabel" or "ScrollingFrame" or "TextBox" or "TextButton" or "TextLabel" then```
2 Likes
local _ = {"Frame","ImageButton","ImageLabel","ScrollingFrame",'TextBox','TextBox','TextButton','TextLabel'}
if table.find(_,Selected.ClassName) then
code here
end

why it didnt work: you must put Selected.ClassName in each “or” statement, you mispelled ImageLabel, having more than 4 “or” statement in 1 line

You can’t use or like that, you must re write it:


if Selected.ClassName == "Frame" or Selected.ClassName == "ImageButton" or  Selected.ClassName == "ImmageLabel" or Selected.ClassName == "ScrollingFrame" or Selected.ClassName == "TextBox" or  Selected.ClassName == "TextButton" or Selected.ClassName == "TextLabel" then```

Plus what is the need for all of these, to check if it’s a UI object:

if Selected:IsA("GuiObject") then
1 Like

You are using the or statements incorrectly. It’s easy to think that you can use or’s by themselves to compare a value to several others. However, with each or you need to have a full comparison again. So in this case, your if statement should look like this:

local class = Selected.ClassName
if class == "Frame" or class == "ImageButton" or class == "ImageLabel" or class == "ScrollingFrame" or class == "TextBox" or class == "TextButton" or class == "TextLabel" then
	
end

I added a variable that gets the class in order to shorten the length of the if statement.

Now the if statement should run when the class is equal to one of the ones listed in the if statement.

Hope this helps!

In addition to what @domboss37 said,
You could have a table with the names you want to check, and simply check if your thing’s name is there.

Example:

local names = {"Test1","Test2"}

if table.find(names,button.Name) then
--code
end