Roblox Studio:为什么 for 循环在 Roblox Studio 上不起作用?

Roblox Studio: Why does for loop not working on Roblox Studio?

所以我在 roblox studio 上制作了一个 CameraScript,当玩家触摸机器人时,相机会聚焦在机器人上。但是 for 循环似乎不起作用。

game.StarterPlayer.StarterPlayerScripts中的脚本:

workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable

game.Players.LocalPlayer.CharacterAdded:Connect(function(char)
    local g = char.Name
    print(g) --Just for debugging purposes
    print("Player Loaded!")
    
    tou(char)
end)

function tou(char)
    print("Function had ran")
    for _,p in pairs(char:GetChildren()) do
        print("We're here loopin ur parts...")
        p.Touched:Connect(function(hit)
            print("Someone touched?")
            if hit.Parent.Name == "Robot" and hit.Parent:IsA("Model") then
                print("It's the robot!")
                workspace.CurrentCamera.CFrame = hit.Parent.Look.CFrame
                workspace.CurrentCamera.Focus = hit.Parent.Head.CFrame
                print("Camlock should be successfull...")
            else
                print("That ain't a robot tho...")
            end
        end)
    end
end

这是一段不起作用的代码:

for _,p in pairs(char:GetChildren()) do
    print("We're here loopin ur parts...")
    p.Touched:Connect(function(hit)
        print("Someone touched?")
        if hit.Parent.Name == "Robot" and hit.Parent:IsA("Model") then
            print("It's the robot!")
            workspace.CurrentCamera.CFrame = hit.Parent.Look.CFrame
            workspace.CurrentCamera.Focus = hit.Parent.Head.CFrame
            print("Camlock should be successfull...")
        else
            print("That ain't a robot tho...")
        end
    end)
end

我尝试将 for 循环直接放在 CharacterAdded 事件中,将 print() 用于调试,但它只打印了这些:

17:55:24.242  <username>  -  Client - CamLockOnKill:5
17:55:24.243  Player Loaded!  -  Client - CamLockOnKill:6
17:55:24.243  Function had ran  -  Client - CamLockOnKill:12

...但它没有打印其他的。

它不打印 We're here loopin ur parts... 所以循环不是 运行。

不运行像

这样的通用for循环的唯一方法
for _,p in pairs(char:GetChildren()) do
end

没有错误就是给pairs提供一个空的table。

所以char没有任何children。找出您认为它有 children 和没有的原因。