首先,作为旁注,我建议不要将级别文件夹存储在game.Workspace 中,而是在ServerScriptStorage 中。我下面的例子就是以此为基础的。
您可以更轻松地遍历该表并获取您认为是检查点的所有部分,而不是从调用 GetChildren() 生成的表中删除部分,例如:
-- The pattern that will be provided to string.match: it contains anchors
-- to the start and end of a string, and searches for 'Tele' followed by
-- one or more digits. This should be treated as a constant, hence the
-- capital letters.
local CHECKPOINT_PATTERN = "^Tele%d+$"
local serverStorage = game:GetService("ServerStorage")
local levels = serverStorage:FindFirstChild("Levels")
local level3 = levels:FindFirstChild("Level3")
-- Helper function to check if a particular part is a checkpoint.
-- Requires: part is a BasePart.
-- Effects : returns true if part.Name matches CHECKPOINT_PATTERN,
-- false otherwise.
local function is_checkpoint(part)
return part.Name:match(CHECKPOINT_PATTERN) ~= nil
end
-- Get children of Level3, which should contain x number of checkpoints.
local children = level3:GetChildren()
-- Create a new table into which you will insert all parts you consider to
-- be checkpoints.
local checkpoints = {}
-- Iterate through the table children to find checkpoints.
for _, child in ipairs(children) do
if (is_checkpoint(child)) then
table.insert(checkpoints, child)
end
end
-- To further abstract this process, the above for-loop could be placed
-- inside a function that returns a table of checkpoints; but, this is
-- entirely up to you and how you want to set up your code.
-- Continue with code, now that all checkpoints within Level3 have been
-- located and inserted into the table checkpoints.
这比修改game.Workspace.Levels.Level3:GetChildren()生成的原始表要简单得多。
作为一种替代的、更高级的解决方案,您可以根据您设置地点的准确程度,使用CollectionService 标记您认为是检查点的所有部分;您可以通过调用CollectionService:GetTagged(<tag>) 来检索包含这些检查点的表。请参阅 Roblox Wiki 上的 this 页面以供参考。
编辑澄清:
变量(我将其视为常量)称为CHECKPOINT_PATTERN 是一个字符串模式;我使用它的角色与您的string.match(v.Name, "Tele") 相同,尽管我的版本略有不同。解释一下:^和$这两个字符被称为anchors——前者意味着模式匹配应该从字符串的前面开始,后者也一样但表示结束; "Tele" 与您的原始代码相同; "%d+" 匹配一个数字一次或多次。有关 Lua 中字符串模式的更多信息,请参阅 this 页面。
关于将Levels 文件夹放入ServerStorage 的问题,这是一个好主意,因为它可以更好地将资产组织在一个位置;而且,它比将东西存储在例如game.Lighting 中更安全。我建议将此作为一种良好做法,但这当然取决于您的场所的设置方式 - 如果这样做会删除您的整个 obby,那么需要对您的场所和代码进行一些重组才能使其正常工作。