ไม่ใช่สมาชิกที่ถูกต้องของโมเดล [ความช่วยเหลือ]

ดังนั้น ฉันกำลังสร้างสคริปต์ปุ่ม เมื่อคลิกแล้ว หากเงื่อนไขใดเงื่อนไขหนึ่งเป็นจริง มันจะค้นหารายการย่อยทั้งหมดของรุ่นอื่น แต่เมื่อฉันพบรายการย่อย มันทำให้ฉันมีข้อผิดพลาดโดยแจ้งว่า "Obj ไม่ใช่สมาชิกที่ถูกต้องของ model" แล้วไม่ได้ทำอะไรเลย

นี่คือรหัสของฉัน:

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)

person King Duck    schedule 23.04.2019    source แหล่งที่มา
comment
ลอง :waitforchild แทน obj และมี obj ในเครื่องหมายคำพูดเช่นนี้: script.Parent.Parent:WaitForChild(Obj):GetChildren()) do   -  person mgracer    schedule 23.04.2019


คำตอบ (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

จบ)

person Kylaaa    schedule 23.04.2019
comment
การค้นหาจากพื้นที่ทำงานจะป้องกันไม่ให้คุณมีสำเนาของโมเดลนี้หลายชุด นอกจากนี้ยังเสี่ยงต่อการชนกับชื่อที่ผู้เล่นควบคุม (ตัวละคร หมวก หรืออาจเป็นชื่อที่ผู้เล่นเลือก เช่น หุ่นยนต์) - person Curtis Fenner; 24.04.2019
comment
นั่นเป็นจุดที่ดีมาก ฉันพยายามแนะนำเส้นทางที่แน่นอน แต่ฉันเดาว่า game.workspace นั้นกว้างเกินไป โดยเฉพาะอย่างยิ่งเพราะฉันไม่แน่ใจว่าสคริปต์ปุ่มนี้มีอยู่ที่ไหนในพื้นที่ทำงาน - person Kylaaa; 24.04.2019