Why :SetAttribute doesn't work with this if statement?

Here I want all of the children of the folder ButtonsFolder to change their attributes to true, except Button1 and Button2. For some reason with this if statement none of the Button’s attributes are set to true. (The code does work if you remove the if statement)

I guess I need some explanation as to why this if statement does not what I expect it to do.

for i, v in pairs(ButtonsFolder:GetChildren()) do
		if v.Name == not "Button1" or not "Button2" then
			v:SetAttribute("SomeAttribute", true)
		end
	end
1 Like

This is always false. It will not surpass.

That’s because of the way you used the if statement in your code.

This is incorrect. To use the if statement for two different checks, you have to repeat v.Name

for i, v in pairs(ButtonsFolder:GetChildren()) do
		if (v.Name ~= "Button1") or (v.Name ~= "Button2") then
			v:SetAttribute("SomeAttribute", true)
		end
	end

Additionally, you have to use the ~= operator, which means “not equal to”.

1 Like

This code just made all the buttons change their attributes to true

for i, v in pairs(ButtonsFolder:GetChildren()) do
		if (v.Name ~= "Button1") or (v.Name ~= "Button2") then
			v:SetAttribute("SomeAttribute", true)
		end
	end

The code changes all buttons that are not called “Button1” or “Button2” to true. Is that not what you wanted?

No, ALL of the buttens, including Button1 and Button2, changed to true

1 Like

I played around with the code and this version does work, but I am still confused as to why this does work but the rest doesn’t. Any ideas why the previous version doesn’t work?

for i, v in pairs(ButtonsFolder:GetChildren()) do
	if v.Name ~= "Button1" then
		if v.Name ~= "Button2" then
			v:SetAttribute("SomeAttribute", true)
		end
	end
end
1 Like

maybe because of the parentheses it included…

I removed the parentheses thinking it’s unnecessary

for i, v in pairs(ButtonsFolder:GetChildren()) do
	if v.Name ~= "Button1" or v.Name ~= "Button2" then
		v:SetAttribute("SomeAttribute", true)
	end
end

or perhaps, the placement of parentheses

I changed the position of the parentheses…

for i, v in pairs(ButtonsFolder:GetChildren()) do
	if (v.Name ~= "Button1" or v.Name ~= "Button2") then 
		v:SetAttribute("SomeAttribute", true)
	end
end
1 Like

I tried different variations, parentheses seem to change nothing

Reading the (updated) code I see nothing wrong. It’s literally just a simple name check, lol. Check the button names themselves and make sure they’re right, or nothing is being changed on runtime that you don’t notice.

If you want to set the SomeAttribute attribute to true for every child of the ButtonsFolder and the code works correctly when you remove the if statement, then why not remove the if statement? It’s not like they’re obligatory to use, or is the problem that the attributes each have a different name?

That’s because that if statement is still always true; v.Name cannot be both "Button1" and "Button2" simultaneously, so one of the two operands to that or will always be true, so the whole thing is always true.

This working code:

is logically equivalent to

for i, v in pairs(ButtonsFolder:GetChildren()) do
	if v.Name ~= "Button1" and v.Name ~= "Button2" then
		v:SetAttribute("SomeAttribute", true)
	end
end

And no, the parentheses @YAYAYGPPR added were not an issue. and and or already have lower precedence than the comparison operators, so the parentheses didn’t do anything except (arguably) increase legibility.

3 Likes

Here (charssssssssssssssssssss)

In that case one way you could achieve that is by doing this:

for _, button in ButtonsFolder:GetChildren() do
	if button.Name == "Button1" then continue end
	if button.Name == "Button2" then continue end

	button:SetAttribute("SomeAttribute", true)
end

The continue skips the lines below it without stopping the loop

Edit: @TheDoom_Boy This could get messy if you have many buttons you’d like to skip though, so in that case I recommend this instead:

local buttonsToSkip = {
	Button1 = true, -- They'll need to be true to be skipped by the loop
	Button2 = true}

for _, button in ButtonsFolder:GetChildren() do
	if buttonsToSkip[button.Name] then continue end

	button:SetAttribute("SomeAttribute", true)
end
1 Like

They definitely /should/ both work. Like I said, they’re logically equivalent. Two if statements directly nested like that are exactly the same thing as a single if statement with the logical-and of their conditions.

EDIT: Note that there is an and between the two comparisons, not an or.

1 Like

I’ll explain how and, not and or works since it has to do a lot with your question

not is the easiest, if the value is true the result is false and if the value is false the result is true

and works like this:

true and true == true
true and false == false
false and true == false
false and false == false

or works like this:

true and true == true
true and false == true
false and true == true
false and false == false

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.