運算子

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

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

邏輯

邏輯運算子根據給定參數的布林值返回值。如果參數不是 falsenil,則運算子將其評估為 true。與許多其他語言不同,Luau 將零和空字串都視為 true。以下表格總結了邏輯運算子在 條件語句 中的行為。

運算子描述
and只有當兩個條件都為真時,才評估為 true
or如果任一條件為真,則評估為 true
not評估為條件的相反

and

二元運算子 and 返回兩個參數中的一個。如果第一個參數評估為 true,則返回第二個參數。否則,返回第一個參數。


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

你可以使用 and控制結構 中測試多個條件,例如 if 語句while 迴圈。例如,下面的 ifthen 語句會測試兩個條件是否都為真:


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

二元運算子 or 返回兩個參數中的一個。如果第一個參數評估為 true,則返回第一個參數。否則,返回第二個參數。


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

你可以使用 or控制結構 中執行複雜的邏輯測試。例如,下面的 ifthen 語句測試兩個條件是否為真第三個條件是否為真:


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

一元運算子 not 返回參數的相反布林值。如果參數為 falsenil,則返回 true。否則,返回 false


print(not true) -- false
print(not false) -- true
print(not nil) -- true
print(not "text") -- false
print(not 0) -- false

你可以使用 not 運算子在變數為 falsenil 時觸發條件或迴圈。


local nilVariable -- 變數被聲明但沒有值,因此是 nil
local falseVariable = false -- 變數被聲明為 false 的值
if not nilVariable then
print(nilVariable) -- 輸出 "nil" 因為 nil 不是 true
end
if not falseVariable then
print(falseVariable) -- 輸出 "false" 因為 false 不是 true
end

你還可以使用 not 運算子測試整個多條件語句的相反。在以下代碼示例中,ifthen 條件測試三大於四及五大於四中的任意一個都不成立。


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.

關係

關係運算子比較兩個參數並返回 boolean: truefalse

運算子描述範例關聯的元方法
==等於3 == 5false__eq
~=不等於3 ~= 5true
>大於3 > 5false
<小於3 < 5true__lt
>=大於或等於3 >= 5false
<=小於或等於3 <= 5true__le

算術

Luau 支援通常的二元運算子以及指數運算、取餘數和一元取反。

運算子描述範例關聯的元方法
+加法1 + 1 = 2__add
-減法1 - 1 = 0__sub
*乘法5 * 5 = 25__mul
/除法10 / 5 = 2__div
//整數除法

10 // 4 = 2
-10 // 4 = -3

__idiv
^指數運算2 ^ 4 = 16__pow
%取餘數13 % 7 = 6__mod
-一元取反-2 = 0 - 2__unm

複合賦值

你可以使用複合賦值運算子將變數設置為一個運算的結果,其中第一個參數是變數的當前值。

複合賦值中的操作執行一次。例如,如果一個表達式在表格中生成隨機索引,Luau 將在操作和賦值中使用相同的索引。

在以下示例中,假設 local x = 3

運算子操作範例x 的新值
+=加法x += 25
-=減法x -= 21
*=乘法x *= 26
/=除法x /= 21.5
//=整數除法x //= 21
%=取餘數x %= 21
^=指數運算x ^= 29
..=字串串接x ..= " World!" "3 World!"

雜項

雜項運算子包括 字串串接長度

運算子描述範例關聯的元方法
..串接兩個字串print("Hello " .. "World!")__concat
#表格的長度如果 tableVar = {1, 2, 3},則 #tableVar == 3__len
©2026 Roblox Corporation、Roblox、Roblox 標誌及 Powering Imagination 是我們在美國及其他國家地區的部分註冊與未註冊商標。