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

问题描述 投票:1回答:1

所以,我创建一个按钮脚本,当点击时,如果某个条件为真,它会找到不同模型的所有子项,但是当我找到这些子项时,它会给我一个错误,说“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
            --This line is where im getting problems, when i do this :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)
lua roblox
1个回答
-1
投票

<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

    -- find the model you are looking for
    local targetModel = game.workspace:FindFirstChild("Obj", true)
    if not targetModel then
        warn("could not find Obj in the workspace, try looking somewhere else")
        return
    end

    -- if the button is green, make stuff invisible
    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

结束)

© www.soinside.com 2019 - 2024. All rights reserved.