Npc 没有按顺序移动到 waypoints?

Npc not moving to waypoints in order?

我一直在尝试按顺序将 npc 移动到一个航路点,它工作了一段时间,但随后它开始乱序并开始表现得很奇怪

Waypoints:

移动脚本:

for i,node in pairs(script.Parent.Parent.Parent.Cartnodes:GetChildren()) do
if node:IsA("BasePart") then
    print(node.Name)
    script.Parent.Parent:WaitForChild("Rafthuman"):MoveTo(node.Position)
    

    script.Parent.Parent:WaitForChild("Rafthuman").MoveToFinished:Wait()
    
    node:Destroy()
end
end

不直观的是,children 的顺序不是按字母顺序确定的。如果您查看 Instance:GetGhildren() 的文档,您会看到这条注释:

The children are sorted by the order in which their Parent property was set to the object.

因此,为了让它们按正确的顺序排列,您需要在迭代之前对列表进行排序。

local raftHuman = script.Parent.Parent:WaitForChild("Rafthuman")
local nodes = script.Parent.Parent.Parent.Cartnodes
local children = nodes:GetChildren()
local sortedChildren = table.sort(children, function(a, b) 
    return a.Name < b.Name
end)
for i,node in ipairs(sortedChildren) do
    if node:IsA("BasePart") then
        print(node.Name)
        raftHuman:MoveTo(node.Position)
        raftHuman.MoveToFinished:Wait()
        node:Destroy()
    end
end