PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

AncestryChanged

Fires when the Instance/Parent property of the object or one of its ancestors is changed.

This event includes two parameters, child and parent. Child refers to the Instance whose Instance/Parent was actually changed. Parent refers to this Instance's new Instance/Parent.

A common use for this function is detecting when an object has been removed or destroyed (using Instance/Destroy). This is done by checking if the parent has been set to nil. For example:

object.AncestryChanged:Connect(function(_, parent)
	if not parent then
		print("object destroyed!")
	end
end)

Parameters

Name Type Default Description

The Instance whose Instance/Parent has been changed.

The new Instance/Parent of the Instance whose Instance/Parent was changed.


Code Samples


Instance.AncestryChanged

The below example would print “Part is now a child of Model”.

local p = Instance.new("Part")
p.Parent = game.Workspace
p.AncestryChanged:Connect(function(child, parent)
    print(child.Name.." is now a child of "..parent.Name)
end)
p.Parent = Workspace.Model