OR and AND conditionals not working as I have expected them to behave

When you’re making certain conditions, you tend to use ‘or’ and ‘and’.

OR works like this:
either (A) or (B) have to be true.

AND works like this:
both (A) and (B) have to be true.

However, I am writing a simple condition. As in

if Instance.Name ~= "ThatName" or Instance.Name ~= "ThisName" then
   print("WOW")
end

In my case, the Instance has both names (different Instances). It shouldn’t pass to print(“WOW”), but it does…

I am sleepy, I have to say that, but this feels like my mind is tricking me extra-extra.
When I replace OR, with AND, it works… It shouldn’t work. Why is it working?
:confused: am I really that tired?

Thank you for the help. Really appreciate it.

1 Like

It works, because the condition is true.

~= is a logical operator for “not equal to”/“different”. In your case, if Instance.Name is not equal to ThatName or ThisName, the condition is going to be true. Both are probably true, but as long as the first one is, lua is going to stop at it and won’t even proceed to look at the second one.

A couple of more examples
local function DoesNameFulfillConditions(name)
	if name == "This" or name == "That" then
		return true
	end
	return false
end

print(DoesNameFulfillConditions("Something")) --> false
local x = 5
local y = 3

-- Short-circuit evaluation
local z = (x + y == 21) and "value1" or "value2"
local z = if x + y == 21 then "value1" else "value2" -- only works in Roblox luau
print(z) --> value2
-- Anything that is not nil or false is a truthy value.
-- Even an empty string.
if nil or false or "" then
	print("true") --> true
end
-- In practice (local script)
local localPlayer = game:GetService("Players").LocalPlayer
-- If .Character (property of player instance) is nil, :Wait() for character.
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
2 Likes

Thank you so much for the well-written response, MeerkatSpirit!

Now that I am more awake (really should stop coding while tired), I see the case much clearer ^ ^

Not only that, I really like the examples you gave. Just to further conversation, do you mind linking me a coding page you’d recommend? I tend to check lua.org from time to time, but I’d like to get more invested into learning things I’m still missing out on.

Thank you, again, for the awesome response :smiley: Very helpful!

1 Like

Roblox has an official Luau site:

1 Like

Woah! Fast response! Thanks ^ ^

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