How do I check that a part was :Destroy()'ed?

Depends how you’re doing the loop. This would be the best.

while true do
    part.AncestryChanged:Wait()
    if not part:IsDescendantOf(game) then
        break
    end
end

-- destroyed at this point

Or you can just connect AncestryChanged itself.

2 Likes

Why does this work on the client but not on the server?

1 Like

It does, what are you detecting?

1 Like

Well if something like this works

part.AncestryChanged:connect(function()
if (not part:IsDescendantOf(workspace)) then
end
end)

why would we need this:

1 Like

You don’t need that.

1 Like

That only detects when the part was parented to nil, not when it’s destroyed.

waffle’s way of checking if a part is destroyed is pretty much the only way.

1 Like

No it works when it’s destroyed i just tested it

EDIT: nvm

1 Like

How often do you need to know when something is destroyed specifically instead of just when it’s out of the game world?

Inaccurate information

If you truly want a destroyed signal, you can use CollectionService.

CollectionService:AddTag(instance, "__DESTROYED")

…then…

CollectionService:GetInstanceRemovedSignal("__DESTROYED", function(instance)
   -- Called when anything with __DESTROYED is destroyed
end)

To check if it’s destroyed, CollectionService:HasTag(instance, "__DESTROYED").

But you never really need to do that. Again, how often do you need to know when specifically :Destroy() has been called?

2 Likes

So say that my part should only be parented to the workspace, and if it’s not, it’s destroyed in my game (bc I won’t ever parent the part to anything else other than workspace).

So would this code suffice for my scenario?

part.AncestryChanged:connect(function()
if (not part:IsDescendantOf(workspace)) then
--stuff
end
end)
1 Like

Yes, it would. The cases where you actually need to check if something is destroyed specifically are niche.

1 Like

The GetInstanceRemovedSignal still fires if the part is parented to nil but not explicitly :Destroyed.

As for my usecase… well I can’t remember exactly why, to be honest. It’s been a year since I posted the OP, after all. But there are several threads out there (this and to some extent this) which show some reasons why one would want to check if something was destroyed.

EDIT: Meant to reply to @Kampfkarren

2 Likes

That’s strange, I don’t remember it working like that.

1 Like

Is there still a way to fix this problem? I am setting parent to nil and using AncestryChanged. It still says its destroyed.

Or you could use the fact that :Destroy() removes every children of an instance when called.
image
If CheckIfImInsidePart’s parent changes and you’re sure it wasn’t cuz of you you can assume Part is destroyed.

When a part is destroyed, the parent is set to nil.

if part.Parent == nil then
    print("Part is destroyed!")
end

You didn’t read the thread before posting that. OP couldn’t check just the parent because a nil parent doesn’t imply destruction. Instances can live in nil (memory) without being destroyed or garbage collected if your code keeps them alive.

See the upcoming event Instance.Destroying from Release 500:

4 Likes

Not true. A part programmatically parented to nil (Part.Parent = nil) isn’t necessarily :Destroy()'ed.

4 Likes

Thank you very much! This worked for my rainbowify function, It kept memoryleaking but thanks to this it finally stops. Absolute life saver.

Sorry Necro Post, but it looks like this post still gets views.

Here is the updated solution

1 Like

There’s a nice way to do this with Tags now that we have the .Destroying event

local CollectionService = game:GetService("CollectionService")

local DESTROYED_TAG: string = "Destroyed"

local part = Instance.new("Part")
part.Parent = workspace
part.Destroying:Once(function()
	CollectionService:AddTag(part, DESTROYED_TAG)
end)

print(CollectionService:HasTag(part, DESTROYED_TAG)) -- false

part:Destroy()

print(CollectionService:HasTag(part, DESTROYED_TAG)) -- true