Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How does this type of 'if then end' statement work? [closed]

Asked by 10 years ago

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.

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?

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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:

  • An expression can be something like a number, e.g., 5 or -3.9.
  • An expression can be other literals like "cat" or true or nil

Variables are expressions:

  • A simple variable is an expression (apple)
  • ROBLOX or Lua objects like {1,2,3} or table or game.Workspace or BrickColor.new("Bright blue")
  • Properties are expressions like 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 expression
    • Vector3.new(3,4,2) + Vector3.new(1,2,0) is an expression
    • 5 * amt is an expression
  • Function calls are expressions

    • math.cos( angle ) is an expression
    • math.sqrt( math.pow(5, 4) ) is an expression
  • Other operators, including logical operators and comparison operators also form compound expressions

    • a == b is an expression
    • a < b is an expression
    • a and b is an expression

So, 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()
1
Wow, I've never known all that until now, I didn't even know what a 'Literal' or 'Expression' was in .lua. :P I have one more question, is it optional to use the '()' in a 'if' statement? TheeDeathCaster 2368 — 10y
1
The parenthesis are completely optional. Since `if` starts and `then` ends, it's already completely unambiguous where the condition is, and Lua prefers using keywords over extra punctuation. Most programming languages (that are C-like) *require* parenthesis around conditions, so some programmers use that style in Lua too, even though Lua doesn't care BlueTaslem 18071 — 10y
Ad