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:
1waffle1:
Here’s a hacky way to do it:
local function Destroyed(x)
if x.Parent then return false end
local _, result = pcall(function() x.Parent = x end)
return result:match("locked") and true or false
end
local x = Instance.new("Part")
print(Destroyed(x)) -- false
x:Destroy()
print(Destroyed(x)) -- true
1 Like
XAXA
(XAXA)
March 10, 2019, 12:04am
#17
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
XAXA
(XAXA)
March 10, 2019, 12:17am
#22
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.
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
colbert2677
(ThisMangaOnHiatus)
January 25, 2022, 7:02am
#29
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
XAXA
(XAXA)
January 25, 2022, 8:13am
#30
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.
SelDraken
(SelDraken)
August 3, 2022, 7:08pm
#32
Sorry Necro Post, but it looks like this post still gets views.
Here is the updated solution
Greetings Developers,
Coming soon, calls to :Destroy() on the server will replicate to clients. What exactly does this mean?
TL;DR: Try changing the ReplicateInstanceDestroySetting under Workspace to Enabled for your experiences and see how they are affected, as we plan to enable it by default in the future.
Current Behavior
You may have noticed that currently when you call :Destroy() on an Instance, that Instance will be destroyed, meaning that it and all its descendants:
Will have their …
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