Get when Instance is removed from game

  1. What do you want to achieve? I want to detect when an Instance is removed from the game or destroyed

  2. What is the issue? There doesn’t seem to be a way to do this that works for every case

  3. What solutions have you tried so far? I have tried the following:

-- workspace.Part is the part I want to detect when it gets destroyed

workspace.Part.Destroying:Connect(function()
   -- does not fire when object is destroyed via workspace FallenPartsDestroyHeight aka when it falls into the void the void
end)

workspace.Part:GetPropertyChangedSignal('Parent'):Connect(function()
   -- does not fire when object is destroyed
   -- just doesn't work at all
end)

this bug has been causing me a headache for a while :confused: , if you need me to clarify anything feel free to ask

the part changes parents a lot so I cant use DescendantRemoving or :FindFirstChild since the parent always changes

how do I check if DescendantRemoving is just removing a child or deleting the actual part

You could maybe use DescendantRemoving to achieve what you want.

local part= game.Workspace.Part

game.DescendantRemoving:Connect(function(descendant)
	if descendant==part then
		print("Part removed.")
	end
end)

Use the DescendantRemoving event.

local part To Watch = game.Workspace.Part

workspace.DescendantRemoving:Connect(function(descendant)
    if descendant.Parent == nil and descendant == partToWatch then
        -- Part has been destroyed and not just removed because destroyed objects have their parent set to nil
    end
end)