~= not working properly

I recently faced a problem where ~= does not work properly

I have a piece of code here

local function VisibleOff()
	local Mains = SettingMain:GetChildren()
	
	for _,v in pairs(Mains) do
		if v.Name ~= "ChoosingFrame" or "Close" or "UICorner" then
			v.Visible = false
		end
	end
end

It would always error

Visible is not a valid member of UICorner “Players.TwyPlasma.PlayerGui.Settings.Settings.UICorner”

So I had to resort to doing

local function VisibleOff()
	local Mains = SettingMain:GetChildren()
	
	for _,v in pairs(Mains) do
		if v.Name == "ChoosingFrame" or "Close" or "UICorner" then
		else
			v.Visible = false
		end
	end
end

This problem only happened recently, so am I doing anything wrong?

2 Likes

Try using the and operator, you could also use the not operator.

if v.Name ~= "ChoosingFrame" and v.Name ~= "Close" and v.Name ~= "UICorner" then

or

if not (v.Name == "ChoosingFrame" or "Close" or "UICorner") then

You can read up on those here:

https://www.lua.org/pil/3.3.html

2 Likes

Try

local function VisibleOff()
	local Mains = SettingMain:GetChildren()
	
	for _,v in pairs(Mains) do
		if v.Name ~= "ChoosingFrame" and v.Name ~= "Close" and v.Name ~= "UICorner" then
			v.Visible = false
		end
	end
end

I managed to get it working.
@7z99, @AtomTostcuOzgurUsta, @infiniteRaymond

This actually works, thank you

For the

Somehow, it does not work and the frames were still visible

1 Like

When you write code as if Val == "a" or "b" or "c", it’s not written to the effect of if Val is a or b or c, it’s to the effect of if Val == a is true or b is true or c is true. To fix this, you have to compare the value individually with each comparison, that being

if Val == "a" or Val == "b" or Val == "c" then

Alternatively, you could also use a dictionary to avoid this.

local Allowed = {
    ["ChoosingFrame"] = true,
    ["Clone"] = true,
    ["UICorner"] = true
}
...
if not Allowed[v.Name] then
    v.Visible = false
end

I hope this helped. :slight_smile:

4 Likes

I agree you, I would use a table. You could also do something like this so you could quickly jot down names as well:

local disallowedExceptions = {
    'ChoosingFrame';
    'Clone';
    'UICorner';
}

if not table.find(disallowedExceptions, v.Name) then
    v.Visible = false
end
1 Like