Detecting When a Part is Destroyed

Hello there!
I was wondering if there is a way to detect if a part in workspace was destroyed, and if so, run the script.
Would I have to add into the script to run the other code before destroying the part? I would appreciate any and all help!

7 Likes

I have never used this event before but I do believe it should do the trick

workspace.DescendantRemoving:Connect(function()
	--Run Code
end)

How would I get it to detect a certain part being removed, though?

run an if statement and check if the parts name is a set name

You could also do something like this:

local Part = -- part name

if not game.Workspace:FindFirstChild(Part) then
    -- code will run here if part is not found
end
local Part = -- index part completely

if not Part then
      -- code will run here if part is not found
end

Those operations will return true or false depending on if the script can successfully index your part.

1 Like

For this you would do:

workspace.DescendantRemoving:Connect(function(part)
    if part.Name == "String" then
        print("Wow this part called String was destroyed!")
    end
end)

@Aerosphia This must be in a loop or else it will not detect the part being destroyed if it is destroyed after the script starts.

9 Likes

Right, thanks. I have been extremely rusty lately.

1 Like

what about detecting it in client?

same way. just tinker it to your needs, no need for anything to be changed.

Why is it printing too much though??

1 Like

Dont deal with these just do
part.Destroying:Connect(Function()
–code here
end)

1 Like