【问题标题】:Roblox Studio: Why does for loop not working on Roblox Studio?Roblox Studio:为什么 for 循环在 Roblox Studio 上不起作用?
【发布时间】:2022-01-27 18:44:04
【问题描述】:

所以我在 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

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

【问题讨论】:

  • 我不熟悉为 Roblox 开发,所以我不知道 GetChildren() 究竟做了什么,但唯一的解释是 char 似乎没有任何孩子
  • CharacterAdded 检测玩家是否已加载到世界中,并且第 3 行的 function() 中的 char 参数获取玩家模型,并且事件仅在角色加载时运行(因此得名) ,所以我的角色不可能没有孩子。不过我还是会试试的。
  • 我对 Roblox 了解不多,但什么是儿童?为什么你认为 char 至少有一个?你的结果真的应该只是意味着它没有任何孩子
  • 这很奇怪,在调用 CharacterAdded 时,您应该在工作区中有一个角色模型。也许这是一个时间问题,尝试在调用tou 之前将wait() 放入函数中?也许尝试不同的信号,例如 Player.CharacterAppearanceLoaded?

标签: for-loop lua roblox


【解决方案1】:

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

不运行通用 for 循环的唯一方法

for _,p in pairs(char:GetChildren()) do
end

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

所以char 没有任何孩子。找出您认为它有孩子的原因以及为什么没有。

【讨论】:

  • 因此,如果该表为空并且 for 循环不会对非空表产生错误,那么 CharacterLoaded 事件就是问题所在?
  • 我没有看到你在这里处理 CharacterLoaded 事件。您只处理 CharacterAdded。你在哪里给了那个玩家的模型任何孩子?
  • 抱歉,我总是把 CharacterAdded 放到 CharacterLoaded 中,反正孩子是从 CharacterAdded 事件 developer.roblox.com/en-us/api-reference/event/Player/…987654321@的参数中得到的
  • 在我的本地测试中,虽然模型存在,但它在短时间内是空的。一个简单的解决方法是在迭代内容之前添加wait()docs 说这缺乏线程安全性,因此它可能是一个简单的竞争条件,可以通过简单地让步来避免。
  • 现在可以使用了。所以添加wait() 使函数等待我的所有部分加载然后for循环运行。
猜你喜欢
  • 2023-02-18
  • 2021-08-09
  • 2019-03-05
  • 2021-11-24
  • 2019-07-24
  • 2020-06-22
  • 2021-04-16
  • 2020-06-20
  • 2015-10-29
相关资源
最近更新 更多