首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Roblox :为什么for循环不工作在Roblox上?

Roblox :为什么for循环不工作在Roblox上?
EN

Stack Overflow用户
提问于 2022-01-27 10:17:33
回答 1查看 362关注 0票数 1

所以我在roblox工作室上制作了一个CameraScript,当玩家接触到一个机器人时,摄像机会聚焦到机器人上。但是for循环似乎不起作用。

Game.StarterPlayer.StarterPlayerScripts中的脚本:

代码语言:javascript
复制
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

这是一段无法工作的代码:

代码语言:javascript
复制
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()放在调试中,但它只打印了以下内容:

代码语言:javascript
复制
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

...but,它没有打印其他的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-27 10:44:04

它不打印We're here loopin ur parts...,因此循环不运行。

不运行泛型for循环的唯一方法,如

代码语言:javascript
复制
for _,p in pairs(char:GetChildren()) do
end

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

所以char没有孩子。找出为什么你认为它有孩子,为什么它没有。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70876851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档