陣列的內容經常需要更改,例如當需要從玩家的庫存中刪除一個物品時。Luau 提供了 內建函數 來與表格一起使用,使這一過程變得更簡單。
本文將概述用於表格的預建函數,如添加和刪除,同時描述如何搜索一個值。
為了應用這些技能,您將創建一個幫助管理玩家庫存的腳本。

向陣列添加值
要向陣列添加新值,使用 table.insert(array, valueToInsert)。第二個參數可以是任何值,例如字符串、數字或整個對象,如 Player 或 IntValue。
為了練習,您將創建一個腳本,將玩家物品存儲在一個表格中,然後添加到其中。由於這個示例僅是一個腳本,因此可以與任何文件或項目一起使用。將使用輸出窗口來測試代碼。
創建一個名為 playerItems 的空陣列。
playerItems = {}通過輸入 table.insert() 將庫存物品添加到陣列中。在括號中,輸入 playerItems,要添加的表格,然後為每個物品輸入一個字符串。至少插入三個值。
playerItems = {}table.insert(playerItems, "Potion")table.insert(playerItems, "Bread")table.insert(playerItems, "Sleeping Bag")print(playerItems)運行項目。在輸出中,展開三個點 ... 以查看打印的表格。
從陣列中刪除值
要刪除一個值,例如如果玩家使用了一個物品或在活動玩家列表中的某人離開遊戲,使用 table.remove()。根據提供的參數,該函數可以刪除表格的最後一個值,或在特定索引處刪除。
刪除最後一個值
有時腳本需要刪除特定的物品。例如,刪除玩家庫存中的第一個物品,或選擇列表中的第一個玩家。要刪除陣列中的最後一個值,使用 table.remove(arrayName)。在這種用例中,唯一需要的參數是表格本身。
使用前一示例中的物品陣列。然後,為了從表格中刪除最後一個值,在這種情況下是 "Sleeping Bag",輸入 table.remove(playerItems)
playerItems = {}table.insert(playerItems, "Potion")table.insert(playerItems, "Bread")table.insert(playerItems, "Sleeping bag")table.remove(playerItems)print(playerItems)運行項目。在輸出窗口中,最後一個值 "Sleeping Bag" 不應該打印。
按索引刪除
要在陣列中的特定位置刪除一個值,將要刪除的索引作為第二個參數輸入,例如 table.remove(arrayName, 1)。
使用相同的表格,輸入 table.remove()。在參數中,輸入表格的名稱和 1,要刪除的值。
playerItems = {}table.insert(playerItems, "Potion")table.insert(playerItems, "Bread")table.insert(playerItems, "Sleeping bag")table.remove(playerItems, 1)運行項目。檢查第一個值 "Potion" 是否已被刪除。
在陣列中搜索值
要查找陣列中的特定值,例如獲勝玩家的名字,使用 table.find() 函數。或者,您可以使用 for 循環和 if 語句編寫自己的搜索函數。
查找並返回單個值
要在陣列中查找一個值,創建一個名為 findValue() 的函數,該函數遍歷一個陣列,並在第一次找到匹配值時停止。
一旦找到該值,該函數將使用 return 關鍵字返回該值的索引。通過索引,您可以從陣列中刪除它。
複製下面名為 playerItems 的陣列。
local playerItems = {"Potion","Bread","Bread","Sleeping Bag"}編寫一個名為 findValue() 的新函數,帶有兩個參數:
whichArray - 要搜索的陣列。
itemName - 要檢查的特定字符串。
local function findValue(whichArray, itemName)end
在 findValue() 中,使用 for 循環檢查陣列中的任何值是否與 itemName 匹配。如果值匹配,使用關鍵字 return 返回在 currentIndex 中找到的值。
local function findValue(whichArray, itemName)for currentIndex = 1, #whichArray doif whichArray[currentIndex] == itemName thenreturn currentIndexendendend通過創建一個名為 valueFound 的變量並調用 findValue() 來測試搜索函數。傳入要搜索的陣列,以及應該搜索的值。運行 代碼以確認預期的輸出是索引 2。
local function findValue(whichArray, itemName)for currentIndex = 1, #whichArray doif whichArray[currentIndex] == itemName then--將 currentIndex 的值返回return currentIndexendendendlocal valueFound = findValue(playerItems, "Bread")print("該值位於索引 " .. valueFound)
刪除一個值
如果使用查找函數找到了某個值,可以將其刪除。使用 if 語句檢查是否找到了某個值。
檢查 valueFound 中是否有值;如果有,使用 table.remove() 刪除該值。
if valueFound thentable.remove(playerItems, valueFound)end使用以下代碼打印出陣列。
for index = 1, #playerItems dolocal itemString = playerItems[index]print("索引 " .. index .. ": " .. itemString)end進行遊戲測試,檢查第一個 "Bread" 值是否已從陣列中刪除。嘗試通過更改 findValue() 中的第二個參數來刪除其他值。
注意,由於此函數僅被調用一次,因此僅刪除了第一個 "Bread" 的實例。接下來的部分將介紹如何查找並刪除所有實例。
查找並刪除所有特定值
雖然之前的代碼只能刪除找到的值的第一個實例,但這段代碼將查找並刪除陣列中的所有出現。例如,如果玩家想在遊戲內商店出售所有的麵包。
使用一個名為 playerItems 的陣列,至少包含四個值和一組 重複項。
local playerItems = {"Potion","Bread","Bread","Sleeping Bag"}為了遍歷陣列,創建一個 for 循環,向後 遍歷 playerItems,從 #playerItems 開始,結束於 1,並以 -1 增量。
for index = #playerItems, 1, -1 doend在循環中,使用 if 語句檢查 playerItems[index] 中的值是否等於 "Bread",如果是,則刪除該項目。
for index = #playerItems, 1, -1 doif playerItems[index] == "Bread" thentable.remove(playerItems, index)endend使用以下代碼添加第二個 for 循環來打印陣列。
for index = 1, #playerItems dolocal itemString = playerItems[index]print("索引 " .. index .. ": " .. itemString)end運行腳本,檢查所有名為 "Bread" 的值是否已被刪除。
總結
可以從陣列中刪除或添加值,但在這樣做時要小心索引的移動。使用循環遍歷表格以刪除值的所有實例,或僅刪除找到的第一個實例。
關鍵字 return 可用於停止循環,並在需要的地方返回信息。
下面可以參考腳本的完成版本。
以下項目包含本教程中的所有腳本。下載 這裡。
請注意,所有腳本都在 ServerScriptService 中並且是 禁用 的。要使用腳本,請在其屬性中取消選中禁用字段並運行 Studio。