Why does this work on the client but not on the server?
It does, what are you detecting?
Well if something like this works
part.AncestryChanged:connect(function()
if (not part:IsDescendantOf(workspace)) then
end
end)
why would we need this:
You don’t need that.
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.
No it works when it’s destroyed i just tested it
EDIT: nvm
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?
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)
Yes, it would. The cases where you actually need to check if something is destroyed specifically are niche.
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
That’s strange, I don’t remember it working like that.
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.
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:
Not true. A part programmatically parented to nil (Part.Parent = nil
) isn’t necessarily :Destroy()
'ed.
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
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
Wouldn’t a simple
if not model.Parent return --[[ or continue or break whatever fits your use case]] end
do the trick though?