不是模型的有效成员 [求助]

所以,我正在创建一个按钮脚本,当单击时,如果某个条件为真,它会找到另一个模型的所有子项,但当我找到子项时,它会给我一个错误,说“Obj不是模型的有效成员”,然后什么也不做

这是我的代码:

script.Parent.Touched:Connect(function(hit)
    if(hit.Name == "RightFoot" or hit.Name == "LeftFoot") then
        if(script.Parent.Color == Color3.fromRGB(0, 255, 0)) then
            --这一行是我遇到问题的地方,当我执行这个:GetChildren时
            for _, object in pairs(script.Parent.Parent.Obj:GetChildren()) do
                if(object:IsA("BasePart")) then
                    object.CanCollide = true
                    object.Transparency = 0
                end
            end
        end
    end
end)
点赞
用户2860267
用户2860267

当您尝试访问不存在的值时会出现<something> is not a valid member of model错误。所以无论script.Parent.Parent是什么,它都没有名为Obj的子对象。

建议不要使用相对路径(如script.Parent.Parent)来导航到对象,而是从可靠的地方使用绝对路径。比如…

local button = script.Parent

button.Touched:Connect(function(hit)
if(hit.Name == "RightFoot" or hit.Name == "LeftFoot") then

    -- 找到你要找的模型
    local targetModel = game.workspace:FindFirstChild("Obj", true)
    if not targetModel then
        warn("在工作区找不到Obj,请尝试其他地方查找")
        return
    end

    -- 如果按钮是绿色的,使一些东西看不见
    if(button.Color == Color3.fromRGB(0, 255, 0)) then
        for _, object in pairs(targetModel:GetChildren()) do
            if(object:IsA("BasePart")) then
                object.CanCollide = true
                object.Transparency = 0
            end
        end
    end
end

end)

2019-04-23 23:03:50