I’ve seen this on several Scripts but i had no idea of how it worked…
This time i found this on Ugh_Lily’s Admin Panel .
local IsPlayerBanned = GameFunction:InvokeServer("CheckBan", DataTable.UnbanUserID)
IsPlayerBanned = IsPlayerBanned and "Yes" or "No"
Now this is what i can’t understand:
IsPlayerBanned = IsPlayerBanned and "Yes" or "No"
I need someone to fully explain how it works and the logic behind it, this could be handy in the future
1 Like
It’s essentially a short-form condition statement. If IsPlayerBanned
is true, then it will set it as “Yes”, otherwise “No”
This is for turning a boolean into a Yes/No for showing to the user.
2 Likes
RamJoT
(RamJoT)
July 23, 2019, 4:11pm
#3
The and part would return And if the value of IsPlayerBanned was Yes and the or part would return No if the previous part was not Yes
1 Like
This is a faked ternary statement because Lua does not have an explicit ternary operator. Afaik boolean expressions in Lua will return the last truthy value in an evaluated boolean expression, or else false.
and
has a higher precedence than or
so the first two operands are evaluated first, returning “yes”. Strings are truthy so the evaluation quits at the or
and “no” isn’t returned. If the first two operands evaluated to false, then “no” would be returned because it’s the last truthy value in the expression after the first two operands collapse to false and the or
is done.
7 Likes
The Lua PiL has some useful information on this, Source: Programming in Lua : 3.3
3.3 – Logical Operators
The logical operators are and , or , and not . Like control structures, all logical operators consider false and nil as false and anything else as true. The operator and returns its first argument if it is false; otherwise, it returns its second argument. The operator or returns its first argument if it is not false; otherwise, it returns its second argument:
print(4 and 5) --> 5
print(nil and 13) --> nil
print(false and 13) --> false
print(4 or 5) --> 4
print(false or 5) --> 5
Both and and or use short-cut evaluation, that is, they evaluate their second operand only when necessary.
A useful Lua idiom is x = x or v
, which is equivalent to
if not x then x = v end
i.e., it sets x
to a default value v
when x
is not set (provided that x
is not set to false ).
Another useful idiom is (a and b) or c
(or simply a and b or c
, because and has a higher precedence than or ), which is equivalent to the C expression
a ? b : c
provided that b
is not false. For instance, we can select the maximum of two numbers x
and y
with a statement like
max = (x > y) and x or y
When x > y
, the first expression of the and is true, so the and results in its second expression (x
) (which is also true, because it is a number), and then the or expression results in the value of its first expression, x
. When x > y
is false, the and expression is false and so the or results in its second expression, y
.
The operator not always returns true or false :
print(not nil) --> true
print(not false) --> true
print(not 0) --> false
print(not not nil) --> false
The PiL is good for answering any other Lua-specific questions you may have, like coroutines and classes. You should still look into the dev hub for Luau / just Roblox specific stuff though, like events or its API Reference.
4 Likes