Question about the "do" and "and" keywords

Hello, person reading this post. I’ve been wondering about the usage of and when setting variables and properties. For example:

local var = 1 and 2
local vec = Vector3.new(0,0,0)
somePart.Position = vec and Vector3.new(255,255,255)
-- what is the logic behind?

and do, on non-loop situations, such as:

do
	print("???")
end
-- is there anything different than not having "do end"?

Seen those used in some places, but I wasn’t able to find how it works out by myself.
Thanks in advance!

I don’t know why someone did that. a and b evaluates to a if a is falsey, b otherwise. var would get 2 and somePart.Position gets Vector3.new(255, 255, 255).

The difference is it creates a new scope. You can declare locals in that do ... end and they won’t be accessible to the outer scope.

do
    local x = 5
    print(x) -- 5
end

print(x) -- nil
7 Likes

Does that mean and would be the contrary to or?

local a = false or 1 -- if first value is false/nil, the second value is valid(1)
local b = false and 1 -- if first value is false/nil, the first value is valid(false)
1 Like

Yes, that would be correct. false or 1 evaluates to 1 since false is falsey. false and 1 evaluates to false.

3 Likes

do is commonly followed by conditional staments in loops (off the top of my head thats the only place i really use them) like:

while chicken == not fat do
feed the chicken

and
is used to usually check that two conditions are met before executing a block of code e.g:

if chicken == fat and chicken == eating then
kill it before it gets to fat lol

it wont kill it if its just fat or if its just eating it has to be doing both

the or statement on the other hand
will execute the block of code if either of the conditions are met e.g:

if chicken == fat or chicken == eating then
kill it because its either fat or its eating

it will execute if the chicken is fat or if its eating one or the other and it will do it

2 Likes

You haven’t read the question correctly.

You gave me an example of usage on an if statement, not setting variables/properties.

Here, you gave me an example with while, which is a loop.

Though, I do appreciate the reply.

have u got a working example of any code that actually uses
and when setting a variable because i havent seen it before

people might use:

local pie, chicken, turkey = game.workspace.pie, game.workspace.chicken, game.workspace.turkey
not 
local pie, chicken, turkey = game.workspace.pie and game.workspace.chicken and game.workspace.turkey

in other higher level languages like python you would use a simillar thing to do you would use a colon however in lua the “do” / “:” is automatically added in, in a sense

it would theoretically go after an else statement or an if statment but you only put them after a loop is declared in lua so no the “do”'s are autmatically added in (invisibly is a way to think about it) now im not 100% sure on this but thats all there really is to know about it

On Creating A Furniture Placement System, there is this line:
local axis = (math.abs(dot) == 1) and Vector3.new(-dot, 0, 0) or up

It works like this:

-- result = *statement* and *if it's true* or *if it's false*

local statementIsTrue = true and 'Is true' or 'Is false'
print(statementIsTrue)  -- "Is true"

local statementIsTrue = false and 'Is true' or 'Is false'
print(statementIsTrue)  -- "Is false"

You can use this instead of using if statements for simple stuff.

For example:

local a = 1
local b = 2

local higherNumber

if a > b then
   higherNumber = a
else
   higherNumber = b
end

That could be converted to:

local a = 1
local b = 2

local higherNumber = (a > b) and a or b
1 Like

Not exactly, if the second operand evalulates to false (false/nil) it’ll return the third operand.

Lua example

print(true and nil or 2) --> 2

C example (to demostrate the tenary conditional operator because Lua don’t have it)

#include <stdio.h>
int main() {
    printf("%d\n", 1 ? 0 : 2); /* 0 */
    printf("%d", 1 && 0 || 2); /* 2 */
    return 0;
}
2 Likes