【问题标题】:Not a valid member of a model [Help]不是模型的有效成员 [帮助]
【发布时间】:2019-09-12 04:14:36
【问题描述】:

所以,我创建了一个按钮脚本,当单击时,如果某个条件为真,它会找到不同模型的所有孩子,但是当我找到孩子时,它给我一个错误,说“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)

【问题讨论】:

  • 尝试 :waitforchild 而不是 obj 并在引号中包含 obj,如下所示:script.Parent.Parent:WaitForChild("Obj"):GetChildren()) 做

标签: lua roblox


【解决方案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

结束)

【讨论】:

  • 从工作区搜索可防止您拥有此模型的多个副本。它还可能与玩家控制的名称(角色、帽子或可能由玩家选择的名称,如类人生物)发生冲突。
  • 这是一个很好的观点,我试图建议某种绝对路径,但我猜 game.workspace 太笼统了。特别是因为我也不确定这个按钮脚本存在于工作区的什么位置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多