Operators | Documentation - Roblox Creator Hub (2024)

An operator is a symbol for performing an operation or conditional evaluation.

Logical

Logical operators return values depending on the boolean values of the given arguments. If an argument isn't false or nil, then the operator evaluates it as true. Unlike many other languages, Luau considers both zero and the empty string as true. The following table summarizes how logical operators behave in conditionals.

OperatorDescriptions
andEvaluates as true only if both conditions are true
orEvaluates as true if either condition is true
notEvaluates as the opposite of the condition

and

The binary operator and returns one of the two arguments. If the first argument evaluates to true, then it returns the second argument. Otherwise, it returns the first argument.

print(4 and 5) -- 5

print(nil and 12) -- nil

print(false and 12) -- false

print(false and true) -- false

print(false and false) -- false

print(true and false) -- false

print(true and true) -- true

You can use and to test multiple conditions in control structures such as if statements and while loops. For example, the following ifthen statement tests that two conditions are both true:

local pasta = true

local tomatoSauce = true

if pasta == true and tomatoSauce == true then

print("We have spaghetti dinner")

else

print("Something is missing...")

end

-- Output: We have spaghetti dinner

or

The binary operator or returns one of the two arguments. If the first argument evaluates to true, then it returns the first argument. Otherwise, it returns the second argument.

local y = x or 1

print(y) -- 1 because x doesn't exist and is therefore nil

local d = false

local e = d or 1

print(e) -- 1 because d is false

print(4 or 5) -- 4

print(nil or 12) -- 12

print(false or 12) -- 12

print(false or true) -- true

print(false or false) -- false

print(true or false) -- true

print(true or true) -- true

You can use or to perform complex logical tests in control structures. For example, the following ifthen statement tests whether two conditions are true or a third condition is true:

local pasta = false

local tomatoSauce = true

local garlicBread = true

if (pasta == true and tomatoSauce == true) or garlicBread == true then

print("We have either spaghetti dinner OR garlic bread")

else

print("Something is missing...")

end

-- Output: We have either spaghetti dinner OR garlic bread

not

The unary operator not returns the opposite boolean value of the argument. If the argument is false or nil, then it returns true. Otherwise, it returns false.

print(not true) -- false

print(not false) -- true

print(not nil) -- true

print(not "text") -- false

print(not 0) -- false

You can use the not operator to trigger a conditional or loop if a variable is false or nil.

local nilVariable -- Variable is declared but has no value, so it's nil

local falseVariable = false -- Variable is declared with value of false

if not nilVariable then

print(nilVariable) -- Outputs "nil" because nil isn't true

end

if not falseVariable then

print(falseVariable) -- Outputs "false" because false isn't true

end

You can also use the not operator to test for the opposite of an entire multi-condition statement. In the following code sample, the ifthen conditional tests that it's neither true that three is greater than four nor is it true that five is greater than four.

local three = 3

local four = 4

local five = 5

if not (three > four or five < four) then

print("Three is less than 4 and five is greater than 4.")

end

-- Output: Three is less than 4 and five is greater than 4.

Relational

Relational operators compare two parameters and return a boolean: true or false.

OperatorDescriptionExampleAssociated metamethod
==Equal to3 == 5false__eq
~=Not equal to3 ~= 5true
>Greater than3 > 5false
<Less than3 < 5true__lt
>=Greater than or equal to3 >= 5false
<=Less than or equal to3 <= 5true__le

Arithmetic

Lua supports the usual binary operators along with exponentiation, modulus, and unary negation.

OperatorDescriptionExampleAssociated metamethod
+Addition1 + 1 = 2__add
-Subtraction1 - 1 = 0__sub
*Multiplication5 * 5 = 25__mul
/Division10 / 5 = 2__div
//Floor Division10 // 4 = 2
-10 // 4 = -3
__idiv
^Exponentiation2 ^ 4 = 16__pow
%Modulus13 % 7 = 6__mod
-Unary negation-2 = 0 - 2__unm

Compound Assignment

You can use compound assignment operators to set a variable equal to the result of an operation where the first parameter is the variable's current value.

The operation in a compound assignment occurs once. For example, if an expression generates a random index in a table, Luau uses the same index for both the operation and the assignment.

In the following examples, suppose local x = 3.

OperatorOperationExampleNew Value of x
+=Additionx += 25
-=Subtractionx -= 21
*=Multiplicationx *= 26
/=Divisionx /= 21.5
//=Floor Divisionx //= 21
%=Modulusx %= 21
^=Exponentiationx ^= 29
..=Concatenationx ..= " World!" "3 World!"

Miscellaneous

Miscellaneous operators include concatenation and length.

OperatorDescriptionExampleAssociated metamethod
..Concatenates two stringsprint("Hello " .. "World!")__concat
#Length of tableIf tableVar = {1, 2, 3}, then #tableVar == 3.__len
Operators | Documentation - Roblox Creator Hub (2024)

References

Top Articles
This Hunter Build Did So Much Damage! | Will It Build?
Baldurs gate 3 eldritch knight x female
Nullreferenceexception 7 Days To Die
Printable Whoville Houses Clipart
Best Pizza Novato
Www.1Tamilmv.cafe
Blorg Body Pillow
Ffxiv Palm Chippings
Is pickleball Betts' next conquest? 'That's my jam'
Craigslist Parsippany Nj Rooms For Rent
My Boyfriend Has No Money And I Pay For Everything
San Diego Terminal 2 Parking Promo Code
Mohawkind Docagent
Mail Healthcare Uiowa
True Statement About A Crown Dependency Crossword
Giovanna Ewbank Nua
Xm Tennis Channel
Indiana Immediate Care.webpay.md
Busted Newspaper S Randolph County Dirt The Press As Pawns
Burn Ban Map Oklahoma
Minecraft Jar Google Drive
Bnsf.com/Workforce Hub
Gemita Alvarez Desnuda
Georgia Vehicle Registration Fees Calculator
Roll Out Gutter Extensions Lowe's
Panic! At The Disco - Spotify Top Songs
2021 Volleyball Roster
Knock At The Cabin Showtimes Near Alamo Drafthouse Raleigh
Wkow Weather Radar
Loslaten met de Sedona methode
Vernon Dursley To Harry Potter Nyt Crossword
Craigslist Pennsylvania Poconos
Encore Atlanta Cheer Competition
By.association.only - Watsonville - Book Online - Prices, Reviews, Photos
Stickley Furniture
Warren County Skyward
Mumu Player Pokemon Go
Petsmart Distribution Center Jobs
Tas Restaurant Fall River Ma
Metro 72 Hour Extension 2022
Western Gold Gateway
My.lifeway.come/Redeem
Skip The Games Grand Rapids Mi
Mbfs Com Login
13 Fun &amp; Best Things to Do in Hurricane, Utah
Fatal Accident In Nashville Tn Today
New Starfield Deep-Dive Reveals How Shattered Space DLC Will Finally Fix The Game's Biggest Combat Flaw
Online College Scholarships | Strayer University
2000 Ford F-150 for sale - Scottsdale, AZ - craigslist
Craigslist Indpls Free
Bunbrat
La Fitness Oxford Valley Class Schedule
Latest Posts
Article information

Author: Annamae Dooley

Last Updated:

Views: 6072

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Annamae Dooley

Birthday: 2001-07-26

Address: 9687 Tambra Meadow, Bradleyhaven, TN 53219

Phone: +9316045904039

Job: Future Coordinator

Hobby: Archery, Couponing, Poi, Kite flying, Knitting, Rappelling, Baseball

Introduction: My name is Annamae Dooley, I am a witty, quaint, lovely, clever, rich, sparkling, powerful person who loves writing and wants to share my knowledge and understanding with you.