I was wondering because I just noticed it when checking out other scripts, the coding is different from what I know, it works like if (condition and second condition) or (third condition and fourth condition) then code end
, but I do not how it works, nor why it is coded like that, I'm just a bit curious, because, I've never seen this type before, I even saw if condition == (string) then code end
.
Using if
looks like this (when not using else
or elseif
):
if ex then -- body -- body end
Where ex
is any expression. if
cares only whether or not the evaluation of the expression is truthy or falsy (most values are truthy. nil
and false
are falsy)
It does not care how it is written (just like expressions anywhere else)
What do I mean by "expression"?
Literals are expressions:
5
or -3.9
."cat"
or true
or nil
Variables are expressions:
apple
){1,2,3}
or table
or game.Workspace
or BrickColor.new("Bright blue")
Part.Parent
or Part.Transparency
Operations on expressions are expressions (making new compound expressions)
Using math on expressions makes a new expression
5 + 4
is an expressionVector3.new(3,4,2) + Vector3.new(1,2,0)
is an expression5 * amt
is an expressionFunction calls are expressions
math.cos( angle )
is an expressionmath.sqrt( math.pow(5, 4) )
is an expressionOther operators, including logical operators and comparison operators also form compound expressions
a == b
is an expressiona < b
is an expressiona and b
is an expressionSo, what it "looks" like doesn't really matter. It just needs to be an expression.
Since that expression will essentially be either truthy or falsey, it is frequent to use ==
, <=
, <
, >=
and >
to compare numbers, since these produce true
and false
.
It's also common to use and
which requires that both the thing to the left and right of it are truthy; or or
which requires at least one of the thing to the left and right of it is truthy; or not
which inverts the truthiness of a particular value (not true
is false
and vice versa)
You can print any expression.
So you can easily play around with different structures for conditions.
apple = "cat" print( apple == "cat" ) -- true print( apple == "dog" ) -- false print( (apple == "cat") and (5 < 3) ) -- false print( (apple == "cat") and (5 > 3) ) -- true print( (apple == "dog") and (5 > 3) ) -- false print( not apple ) -- false print( (not apple) or (5 > 3) ) -- true
Programming languages are usually extremely consistent.
If you can do something in one place, you can probably do it anywhere else (unless there's a good reason you can't)
There are lots of unorthodox things that can be done:
game.Workspace.Parent.Workspace.Parent.Workspace.BlueTaslem:BreakJoints() -- Same as game.Workspace.BlueTaslem:BreakJoints()
Locked by TheeDeathCaster and Goulstem
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?