How should I destroy a model when clicked?

Hello, I was wondering how I could destroy a model when clicked. Could anyone answer my question?

You can place a click detector a part inside the model, and when the click detector is clicked, destroy the model:

local ClickDetector = -- the clickdetector
local Model = -- the model to destroy

CD.MouseClick:Connect(function()
    Model:Destory()
end)
5 Likes

For ClickDetector instances the signal .MouseClick is fired when they’re clicked not .Clicked

3 Likes

You’d have to insert a ClickDetector inside a part located inside the model, because you can’t actually click a model.

2 Likes

whoops i mean when you click a few set models inside a folder how do i destroy them?

Insert a Click Detector and add a script. Make a Variable for the Click Detector and the object you want to destroy. Make a function with MouseClick in it and do ‘Object’:Destroy

local ClickDetector = 'The Click Detector
local Object = ‘Object to destroy’

game.Workspace.‘Object’.ClickDetector.MouseClick:connect(function()
‘Object’:Destroy()

If you want to destroy the set models in a folder then same thing applies. Give the variable for the folder. And switch it for ‘Object’

I think that’s how it goes but I could be wrong. :slightly_smiling_face:

.MouseButton1Click.

Anyways, what do you mean by a few set models @ilovedevex?

1 Like

I mean like any model inside a folder so I don’t have to do so many scripts.

Using :ClearAllChildren() you can get rid of all contents inside of a folder / model. This might be what you’re looking to do for your situation.

    local FolderOfModelsToDestroy = game:GetService("Workspace"):WaitForChild("ModelsToDestroy")
    local ClickDetector = -- directory to clickdetectory

    ClickDetector.MouseClick:Connect(function()
    FolderOfModelsToDestroy:ClearAllChildren()
    end)

I said click detector and linked the documentation?

Using what @Cytheur said, here is an example script:

local folder = workspace.Models -- folder with models you want to destroy
local clicker = workspace.Clicker.ClickDetector -- idk, maybe you want to click a green button on a wall or something.

clicker.MouseClick:Connect()
    folder:ClearAllChildren()
end)

Uh yeah… Is there a problem?

I was correcting @Todzler

1 Like

I was confused, I’m sorry for the inconvenience!

What I mean is i want to click a model and destroy just that model but how could I do it from just one script?

Use a for loop then.

for i, v in pairs(game.Workspace.Folder:GetDescendants()) do -- :GetChildren() just gets the models, but :GetDescendants() gets everything.
    if v:IsA("ClickDetector") then -- checking if the descendant is a click detector
        v.MouseClick:Connect(function() -- fires when the click detector is clicked
            v.Parent.Parent:Destroy() -- v.Parent is the part the click detector is in, and v.Parent.Parent is the model.
        end)
    end
end

And make sure your click detectors are in a part inside a model. Or else the script won’t work.

Hierarchy:

Model = v.Parent.Parent

Part = v.Parent

ClickDetector = v

1 Like

Wouldn’t that just delete the click detector though?

Oh wait never mind it would work.

It worked thanks a lot! :DD (30 characters)

1 Like

Wait @IAmPinleon I have to click it two times for it to destroy, so whats the problem?