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
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
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 alwaystrue; 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.
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
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.