Some Clarification on 'and' inside variables

So I have noticed in some code where people use and in Variables, like this:

Variable = workspace.Item and workspace:FindFirstChild("otherItem")

What exactly does this do to the variable?

1 Like

(Not actually checked)
If this follows normal human’s logic, it would set the Variable to both Instances, even though I’m not sure, only times I tried using it it errored…

and & or are typically used as Lua ternary operators (since Luau didn’t have an official ternary operator). This is essentially what it does:

-- Example 1
local function functionWithOptional(optional: string?)
   print(optional and optional or "No argument provided")
   -- if 'optional' is a truthy value (not false or nil), then it will evalute the `and` expression, otherwise it will evalute the `or` expression
   -- this could also just be written as: print(optional or "No argument provided")
end

If you’re still confused, you can look at it like this:

(value1 and otherValue) or defaultValue
--      ^^ if this returns false, it will default to the or expression

-- You can also visualize it this way
if condition1 and condition2 then
   -- do something
else
   -- otherwise
end

Luau now does have an official way of doing this via if-then-else expressions, but I don’t use them as much because it inflates the length of the line I’m writing:

local value = if condition then "value" else "otherValue"
3 Likes

Review the above post for more information!


Variable = workspace.Item and workspace:FindFirstChild("otherItem")

Will check if workspace.Item exists, and if it does then it will assign Variable to workspace.otherItem. If it doesn’t exist, teh variable will be set to nil

1 Like

So basically its just “if this and that is false or nil, it will pick the other” for the and, and or?

So what exactly would this be used for, why is it used?

Pretty much.


Here’s an example of how I used this in one of the code bases I created:

In this example, if isShowing is true, the property will be set to Enum.AutomaticSize.Y, however, if it’s not true, it will be set to Enum.AutomaticSize.None.

I could write it like this:

if isShowing then
   Frame.AutomaticSize = Enum.AutomaticSize.Y
else
   Frame.AutomaticSize = Enum.AutomaticSize.None
end

However, the former method is much shorter.

1 Like

hmmmmm

-- Method 1:
Frame.AutomaticSize = isShowing and Enum.AutomaticSize.Y or Enum.AutomaticSize.None

-- Method 2:

if isShowing then
   Frame.AutomaticSize = Enum.AutomaticSize.Y
else
   Frame.AutomaticSize = Enum.AutomaticSize.None
end

-- Method 3:

t = {
    [true]  = Enum.AutomaticSize.Y;
    [false] = Enum.AutomaticSize.None;
}

Frame.AutomaticSize = t[(isSizing == true)]

```
1 Like

Small Question tho:

What if you did this:

variable = bool1 and bool2 and bool3 and bool4 and value

It would work the exact same?

1 Like

These are the rules for the and and or operators:

And: if its first operand is false or nil, then the first operand is used as the result; otherwise the second operand is used as the result
Or: If its first operand is something other than false or nil, then the first operand is used as the result; otherwise the second operand is used as the result.

Also remember that:

false and nil are the two false values, and every other value is a true value

from here you can deduce your own answers

If all the bool statements were True, then the variable will be set to value. If one or more is set to false, teh variable is set to Nil

1 Like

I mean: yes, but no.

Isnt nil non-existent?
Thenciaclly its no true, but it’s not false

Yes, but nil is considered as a false value, while the rest is considered true, which is why if FindFirstChild(Item) works how it does. (if you didnt know)

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