Skip to content
Operators: Arithmetic, Comparison, Logical
Roblox

Operators: Arithmetic, Comparison, Logical

Understand Lua operators in Roblox. Learn arithmetic, comparison, and logical operators for calculations, conditions, and script control.

By ···10 min read·Multi-source verified
1 reading this guide  

Understand Lua operators in Roblox. Learn arithmetic, comparison, and logical operators for calculations, conditions, and script control.

Operators are the fundamental building blocks of any programming language, and Lua, the scripting language used in Roblox, is no exception. They are symbols that perform operations on values (operands) to produce a result. Understanding arithmetic, comparison, and logical operators is crucial for writing effective scripts that can perform calculations, make decisions, and control the flow of your game.

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations. They work with numbers to produce numerical results. The most common arithmetic operators in Lua are:

Operator Description Example Result
+ Addition 5 + 3 8
- Subtraction 10 - 4 6
* Multiplication 6 * 7 42
/ Division 20 / 5 4
% Modulo (Remainder of division) 10 % 3 1 (10 divided by 3 is 3 with a remainder of 1)
^ Exponentiation (Power) 2 ^ 3 8 (2 to the power of 3)

These operators can be used in various combinations within your scripts. For instance, to calculate the total cost of items:


local itemPrice = 15
local quantity = 3
local taxRate = 0.05

local subtotal = itemPrice * quantity
local taxAmount = subtotal * taxRate
local totalCost = subtotal + taxAmount

print("Total cost: " .. totalCost)

2. Comparison Operators

Comparison operators are used to compare two values. They return a boolean value: `true` if the comparison is true, and `false` otherwise. These are essential for conditional statements (like `if` statements) and loops.

Operator Description Example Result
== Equal to 5 == 5 true
~= Not equal to 5 ~= 3 true
> Greater than 10 > 5 true
< Less than 3 < 7 true
>= Greater than or equal to 8 >= 8 true
<= Less than or equal to 2 <= 1 false

Comparison operators are vital for controlling game logic. For example, checking if a player's health is low:


local playerHealth = 20

if playerHealth <= 25 then
 print("Warning: Health is low!")
end

3. Logical Operators

Logical operators are used to combine or modify boolean expressions. They allow you to create more complex conditions for your `if` statements and loops.

Operator Description Example Result
and Logical AND: Returns true if both operands are true. (5 > 3) and (10 == 10) true
or Logical OR: Returns true if at least one operand is true. (5 < 3) or (10 == 10) true
not Logical NOT: Reverses the boolean value of its operand. not (5 == 5) false

Logical operators are powerful for creating nuanced conditions. For instance, granting a bonus only if a player meets multiple criteria:


local playerLevel = 10
local hasQuestItem = true

if playerLevel >= 5 and hasQuestItem then
 print("Bonus granted!")
end

Mastering these operators will significantly enhance your ability to write dynamic and interactive scripts in Roblox Studio, allowing you to build more complex and engaging game mechanics.

100% Human-Written. AI Fact-Checked. Community Verified. Learn how AntMag verifies content