Operators in Luau

Introduction to Operators in Luau

Operators are essential in Luau, allowing you to perform various operations on data. Understanding these operators is crucial for effective scripting in Roblox. This article will cover the main types of operators in Luau: arithmetic, relational, logical, string concatenation, and the length operator.

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations. The basic arithmetic operators in Luau are:

  • Addition (+): Adds two numbers.
  • Subtraction (-): Subtracts one number from another.
  • Multiplication (*): Multiplies two numbers.
  • Division (/): Divides one number by another.
  • Floor Division (//): Divides and rounds down to the nearest integer.
  • Modulus (%): Returns the remainder of a division operation.
  • Exponentiation (^): Raises a number to the power of another.

Here’s an example demonstrating these operators:

local a = 10
local b = 3

print(a + b)  -- Output: 13
print(a - b)  -- Output: 7
print(a * b)  -- Output: 30
print(a / b)  -- Output: 3.333...
print(a // b) -- Output: 3
print(a % b)  -- Output: 1
print(a ^ b)  -- Output: 1000

Relational Operators

Relational operators are used to compare values. They return a boolean result (true or false). The relational operators in Luau are:

  • Equal to (==): Checks if two values are equal.
  • Not equal to (~=): Checks if two values are not equal.
  • Less than (<): Checks if the left value is less than the right.
  • Greater than (>): Checks if the left value is greater than the right.
  • Less than or equal to (<=): Checks if the left value is less than or equal to the right.
  • Greater than or equal to (>=): Checks if the left value is greater than or equal to the right.

Here’s an example of using relational operators:

local x = 5
local y = 10

print(x == y)  -- Output: false
print(x ~= y)  -- Output: true
print(x < y)  -- Output: true
print(x > y)  -- Output: false
print(x <= y) -- Output: true
print(x >= y) -- Output: false

Logical Operators

Logical operators are used to combine boolean values. The main logical operators in Luau are:

  • And (and): Returns true if both operands are true.
  • Or (or): Returns true if at least one operand is true.
  • Not (not): Returns true if the operand is false.

Here’s how to use logical operators:

local a = true
local b = false

print(a and b)  -- Output: false
print(a or b)   -- Output: true
print(not a)    -- Output: false

String Concatenation Operator

In Luau, the string concatenation operator (..) is used to join two or more strings together. This operator is very useful when you want to create dynamic strings.

Here’s an example:

local firstName = "John"
local lastName = "Doe"
local fullName = firstName .. " " .. lastName

print(fullName)  -- Output: John Doe

Length Operator

The length operator (#) is used to return the number of elements in a table or the length of a string. This operator is essential for working with arrays and strings.

Example of using the length operator:

local myString = "Hello World"
local myTable = {1, 2, 3, 4, 5}

print(#myString)  -- Output: 11
print(#myTable)   -- Output: 5

Operator Precedence

Understanding operator precedence is important when performing complex calculations. In Luau, the order of precedence is as follows (from highest to lowest):

  1. Exponentiation (^)
  2. Unary minus (-), Unary not (not)
  3. Multiplication, Division, Floor Division, Modulus (*, /, //, %)
  4. Addition, Subtraction (+, -)
  5. Relational operators (==, ~=, <, >, <=, >=)
  6. Logical and (and)
  7. Logical or (or)

By understanding operator precedence, you can avoid unexpected results in your calculations.

With this knowledge of operators in Luau, you can enhance your scripting skills in Roblox and create more dynamic and interactive experiences.