diff --git a/lib/instances/BaseInstance.lua b/lib/instances/BaseInstance.lua index 83c7da9..28d8a6b 100644 --- a/lib/instances/BaseInstance.lua +++ b/lib/instances/BaseInstance.lua @@ -56,6 +56,10 @@ BaseInstance.properties.ChildAdded = InstanceProperty.readOnly({ getDefault = Signal.new }) +BaseInstance.properties.ChildRemoved = InstanceProperty.readOnly({ + getDefault = Signal.new +}) + BaseInstance.properties.Parent = InstanceProperty.normal({ set = function(self, key, value) local instance = getmetatable(self).instance @@ -74,6 +78,7 @@ BaseInstance.properties.Parent = InstanceProperty.normal({ if instance.properties.Parent ~= nil then getmetatable(instance.properties.Parent).instance.children[self] = nil + instance.properties.Parent.ChildRemoved:Fire(self) end instance.properties.Parent = value diff --git a/lib/instances/BaseInstance_spec.lua b/lib/instances/BaseInstance_spec.lua index fcbf337..0f9c9b4 100644 --- a/lib/instances/BaseInstance_spec.lua +++ b/lib/instances/BaseInstance_spec.lua @@ -671,4 +671,36 @@ describe("instances.BaseInstance", function() assert.spy(spy).was_called_with(child) end) end) + + describe("ChildRemoved", function() + it("should fire when a child is destroyed", function() + local parent = BaseInstance:new() + local child = BaseInstance:new() + + local spy = spy.new(function() end) + parent.ChildRemoved:Connect(spy) + + child.Parent = parent + assert.spy(spy).was_not_called() + + child:Destroy() + assert.spy(spy).was_called_with(child) + end) + + it("should fire when a child's parent is set to nil", function() + local parent = BaseInstance:new() + local child = BaseInstance:new() + + local spy = spy.new(function() end) + parent.ChildRemoved:Connect(spy) + + child.Parent = parent + + assert.spy(spy).was_not_called() + + child.Parent = nil + + assert.spy(spy).was_called_with(child) + end) + end) end) \ No newline at end of file