*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

数据类型可以存储多个任意非 nil 类型的值,包括 布尔值数字字符串函数 和其他表。使用花括号 ({}) 构造表:


-- 构造一个空表并赋值给变量 "t"
local t = {}
print(t) -- {}

您可以将表用作 数组字典。数组使用有序的数字列表作为索引,但字典可以使用数字、字符串和对象作为索引。

有关处理表的内置函数的更多信息,请参见 table 库。

数组

数组 是一个有序的值列表。数组对于存储数据集合很有用,例如具有特殊权限的玩家组。

创建数组

要使用 Luau 表创建数组,请按顺序声明值,以逗号分隔。


-- 构造一个包含三个项目的数组
local testArray = {"一个字符串", 3.14159, true}
print(testArray)

从数组中读取

要从数组中读取,请在引用后添加一对方括号并指定元素的索引号 ([pos]):


-- 构造一个包含三个项目的数组
local testArray = {"一个字符串", 3.14159, true}
print(testArray[1]) -- 一个字符串
print(testArray[2]) -- 3.14159
print(testArray[3]) -- true

写入数组

要定义或重写数组中某个索引的值,请在方括号中声明索引号 ([index]),后跟 = 和值:


local testArray = {"一个字符串", 3.14159, true}
testArray[2] = 12345
testArray[4] = "新字符串"
print(testArray[2]) -- 12345
print(testArray[4]) -- 新字符串

迭代数组

要迭代数组,可以使用 for 循环。由于数组具有数值索引,因此您也可以使用从 1 到数组长度 (#array) 的数字 for 循环。


local testArray = {"一个字符串", 3.14159, true, "新字符串"}
-- 使用一般迭代的循环
for index, value in testArray do
print(index, value)
end
-- 使用数组长度运算符 (#) 进行迭代
for index = 1, #testArray do
print(index, testArray[index])
end

插入项目

有两种内置方法可以将项目插入到 数组的末尾

  • 将数组的引用和项目的值传递给 Luau 的 table.insert() 函数。
  • 使用 array[#array+1] 语法将新项目添加到数组中。

local testArray = {"一个字符串", 3.14159}
table.insert(testArray, "新字符串")
testArray[#testArray+1] = "另一个新字符串"
print(testArray[3]) -- 新字符串
print(testArray[4]) -- 另一个新字符串

要在数组的开始和结束之间插入项目,请将位置值作为 table.insert() 的第二个参数。这将插入新项目并将后续项目向上推一个索引位置。


local testArray = {"第一个项目", "下一个项目"}
table.insert(testArray, 2, "新项目 #2")
print(testArray[1]) -- 第一个项目
print(testArray[2]) -- 新项目 #2
print(testArray[3]) -- 下一个项目

移除项目

要从数组中移除项目,请使用 table.remove()。这将移除指定位置的项目,并将任何后续项目向后移动一个索引位置。


local testArray = {"第一个项目", "下一个项目", "最后一个项目"}
table.remove(testArray, 2)
print(testArray[1]) -- 第一个项目
print(testArray[2]) -- 最后一个项目

字典

字典是数组的扩展。字典存储一组键值对,其中键可以是任何数字、字符串或对象。

创建字典

要创建字典表,请定义每个 ,后跟 =。用逗号分隔每个键值对:


local testDictionary = {
fruitName = "柠檬",
fruitColor = "黄色",
sour = true
}

字典的键可以是数字、字符串和对象。例如,键也可以是 Instance。要将对象用作键,请在方括号中声明键 ([key]):


local part = Instance.new("Part")
local testDictionary = {
partType = "块",
[part] = true
}

从字典中读取

要从字典中读取,请在其引用后添加一对括号并指定键名。直接使用字符串键引用,可以使用 (["key"]) 或 (.key),或者使用变量值 ([key])。


local part = Instance.new("Part")
local testDictionary = {
partType = "块",
[part] = true
}
-- 为字符串键包含引号
print(testDictionary["partType"]) -- 块
-- 或使用 . 对没有空格的字符串键进行索引
print(testDictionary.partType) -- 块
-- 为非字符串键省略引号
print(testDictionary[part]) -- true

写入字典

要定义或重写新键或现有字典键的值,请在方括号中声明键名 ([key]) 或者,如果键是字符串,使用 (.key) 后跟 = 和值:


local testDictionary = {
fruitName = "柠檬",
sour = true
}
-- 更改现有键的值
testDictionary["fruitName"] = "樱桃"
testDictionary.sour = false
-- 插入新键值对
testDictionary.fruitCount = 10
print(testDictionary.fruitName) -- 樱桃
print(testDictionary.sour) -- false
print(testDictionary.fruitCount) -- 10

迭代字典

要迭代字典,请使用 for 循环:


local testDictionary = {
fruitName = "柠檬",
fruitColor = "黄色",
sour = true
}
for key, value in testDictionary do
print(key, value)
end
--[[ 结果输出:
fruitName 柠檬
sour true
fruitColor 黄色
]]

移除键值对

要从字典中移除或擦除键值对,请将其键的值设置为 nil


local testDictionary = {
fruitName = "柠檬",
fruitColor = "黄色",
sour = true
}
testDictionary.sour = nil
for key, value in testDictionary do
print(key, value)
end
--[[ 结果输出:
fruitName 柠檬
fruitColor 黄色
]]

表作为引用

如果您将表存储在新变量中,Luau 不会创建该表的副本。相反,变量变成对原始表的 引用 或指针。对表的任何引用都反映了对原始表的任何更改:


local originalArray = {10, 20}
local arrayReference = originalArray
print("原始:", originalArray[1], originalArray[2])
print("引用:", arrayReference[1], arrayReference[2])
-- 更改原始数组中的值
originalArray[1] = 1000
originalArray[2] = 2000
print("引用:", arrayReference[1], arrayReference[2])
--[[ 结果输出:
原始: 10 20
引用: 10 20
引用: 1000 2000
]]

克隆表

浅克隆

要在没有任何嵌套表的情况下复制表,Luau 提供了 table.clone() 方法。


local original = {
key = "value",
engine = "Roblox",
playerID = 505306092
}
local clone = table.clone(original)

深克隆

要复制更复杂的表,其中包含嵌套表,您需要使用类似以下的递归函数:


-- 用于深克隆表的函数
local function deepClone(original)
-- 定义用于复制的新表
local clone = table.clone(original)
-- 遍历原始表以检查表值
-- 如果在值中找到表,则深度克隆到键(索引)中
for key, value in original do
if type(value) == "table" then
clone[key] = deepClone(value)
end
end
-- 返回深克隆表的最终副本
return clone
end

在设置函数后,您可以像下面这样进行深度复制:


local original = {
key = "value",
playerInfo = {
playerID = 505306092,
playerName = "玩家名称"
},
otherInfo = {
{
{1, 3, 5, 7, 9}
}
}
}
local clone = deepClone(original)

冻结表

冻结表使其变为只读,这对于创建您不想更改的常量值非常有用。冻结是永久的;没有 “解冻” 或 “融化” 方法。要检查表是否被冻结,请使用 table.isfrozen()

浅冻结

要冻结没有任何嵌套表的表,Luau 提供了 table.freeze() 方法。


local target = {
key = "value",
engine = "Roblox",
playerID = 505306092
}
table.freeze(target)
target.playerID = 1 --> 尝试修改只读表

深冻结

要冻结一个更复杂的表,其中包含嵌套表,使用类似以下的递归函数:


local function deepFreeze(target)
-- 浅冻结表
table.freeze(target)
-- 检查表的每个键,如果是表,则冻结它
for _, value in target do
-- 确保值没有被冻结;如果已经被冻结,将引发错误
if type(value) == "table" and table.isfrozen(value) == false then
deepFreeze(value)
end
end
end

在设置函数后,您可以像以下这样深度冻结一个表:


local target = {
key = "value",
playerInfo = {
playerID = 505306092,
playerName = "玩家名称"
},
otherInfo = {
{
{1, 3, 5, 7, 9}
}
}
}
deepFreeze(target)
target.playerInfo.playerID = 1 --> 尝试修改只读表
©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。