How do I detect when a model is being destroyed?

I need to detect when a model is being destroyed. However, I can’t figure out how to actually detect it. (I have tried Instance.Destroying, but it didn’t fire when the model fell below FallenPartsDestroyHeight)

This is what I’m trying right now, but it doesn’t actually fire when the model is destroyed:

local block = script.Parent

block:GetPropertyChangedSignal("Parent"):Connect(function()
	print("parent changed")
	if block.Parent == nil then
		--things
	end
end)
1 Like

I don’t know about this, but a new event for instances called Instance.Destroying event will be coming out very soon. Another alternative is connect the ChildAdded event on the parent of the instance you wanna check on being destroyed.

yep that seems to be the case

adding this to the bottom and using Instance.Destroying works:

block.DescendantRemoving:Connect(function(instance)
	local passed = false
	for i, v in ipairs(block.blockContentContainer:GetDescendants()) do
		if v:IsA("BasePart") and v ~= instance then
			passed = true
			break
		end
	end
	if passed == false then
		block:Destroy()
	end
end)
local block = script.Parent

block.AncestryChanged:Connect(function(child, parent)
	if not parent then --block's parent is nil (likely being destroyed).
		print("Block destroyed!")
	end
end)
1 Like

Any instance put into the modelsExpectedToBeDestroyed table will fire the function. If the instance has a parent of nil, the object is considered to be destroyed, if not, the instance changed parents, which has another function if you wanted something to change if that were the case instead.

-- Script
local CollectionService = game:GetService("CollectionService")
local modelsExpectedToBeDestroyed = {workspace.Part} -- Instance(s) here will fire the function upon being destroyed, put into a table. (e.g. 'workspace.Part')

for index, instance in ipairs(modelsExpectedToBeDestroyed) do
	CollectionService:AddTag(instance, "modelsExpectedToBeDestroyed")
end

for index, instance in pairs(modelsExpectedToBeDestroyed) do	
	local function isChildDeleted()
		if instance.Parent == nil then -- Instance has been destroyed.
			print(instance.Name .. " has been destroyed.")
		else -- Instance has been moved to another parent.
			print(instance.Name .. " has been moved to " .. instance.Parent.Name .. ".")
		end
	end
	
	instance:GetPropertyChangedSignal("Parent"):Connect(isChildDeleted)
end

Let me know if this helps.

1 Like