Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Question about OnTouched, Models?

Asked by
Xl5Xl5 65
9 years ago

How would I remove a specific model OnTouched? Like if theres a Model called "Monkey" then remove?

function onTouched(hit) d.
    local d = hit.Parent:GetChildren() 
    for i=1, #d do 
        if (d[i].className == "Model") then .
            d[i]:remove() 
        end 
    end
end

script.Parent.Touched:connect(onTouched) 


0
Are you trying to remove a model when it is touched, or when a different part is touched? BlackJPI 2658 — 9y
0
random period and d. HungryJaffer 1246 — 9y
0
When the Model called Monkey, any part of Monkey (Model) is touched then it will remove Monkey (Whole model) Xl5Xl5 65 — 9y
0
remove is depricated, use destroy. HungryJaffer 1246 — 9y

1 answer

Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
9 years ago

If you want to remove a model, first access the model, then remove it. If you want to do that when someone touches something, put it in that event.

Part.Touched:connect(function()
    Model:Destroy()
end)

If you want to remove the model if any part of it is touched, then you need to iterate through every part of the model:

function DestroyModelOnTouch(x,Model)
    Model=Model or x
    for _,v in pairs(x:GetChildren())do
        if v:IsA("BasePart")then
            v.Touched:connect(function()
                Model:Destroy()
            end)
        end
        DestroyModelOnTouch(v,Model)
    end
end
DestroyModelOnTouch(Model)
Ad

Answer this question