Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

If statement won't identify when using Or?

Hi all! The function below works fine when name has string value and name2 doesn't. But as soon as I give string values to both, it makes everything visible, rather than just the two that are stated. The problem is the line right here:if v.Name == name or name2 then ....The function works fine singularly. Any help is greatly appreciated!

function DisplayText(name, name2)
	for i,v in pairs(TextStore:GetChildren()) do
		if v.Name == name or name2 then
			v.Visible = true
		else
			v.Visible = false
		end
	end
end

1 answer

✓ Accepted answer

Logical Operators

When doing comparisons, you need to check each equality separately. Like this:

if v.Name  == name  or v.Name  == name2 then

What's actually happening here is this:

if v.Name  == name  or name2 then -- '==' is higher precedence than 'or'

if (v.Name  == name)  or name2 then -- The equality evaluates.

if bool  or name2 then -- If bool is true, then the expression evaluates to true. If bool is false, the expression evaluates to name2, which is considered 'true.' Regardless, the expression evaluates to true.


if true then -- This is what the if statement is equivalent to.


1 comment

0

Thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.