LinkModule - Linking destruction between instances

Hey! This is my first open source release, so please do correct me if I screw anything up in this post.

LinkModule is a module used to link objects together by destruction. Normally you could just use models for this (model is destroyed, children are destroyed as well) however some complications can arise. What if you want two or more objects to have a mutually assured destruction? What if your child objects which you want to be destroyed span across different ancestries? What if you want more than one object to trigger the destruction of a group? These are the problems that this module solves.

Here are some function examples:


function 1:


If any of these items are destroyed, regardless of ancestry, all other items in the group will be destroyed as well.

function 2:

linkmodule.linkinstance(instance1, instance2)

If the first instance is destroyed, the second will be destroyed also. However, if the second instance is destroyed, it will have no effect on the first.

function 3:

linkmodule.linkgroup(instance1, {tableofitems})

If the first instance is destroyed (instance1), the group of objects will also be destroyed. If an instance inside of the group is destroyed, there are no effects (one way destruction).

function 4:


Un-links an instance from whatever group it was in. Also returns a bool of whether the operation succeeded or not.

function 5:


Un-links a table of instances from whatever group they were in. Also returns a bool of whether the operation succeeded or not.

function 6:


Returns an array of all linked groups (groups are tables).

An important thing to note is that instances within the group can be under different hierarchies/ancestries without error. Even if an instance is parented to nil, it still is linked to the group. Also, changing the ancestry of instances does not sever any group connections unless specified with unlinkinstance() or unlinkgroup().

The only things that you can link are instances which can be destroyed. Trying to link anything otherwise such as lua objects or workspace.Terrain will cause an error.

Thats it! thanks for reading, I hope this is useful to someone haha. If you encounter any issues such as bugs or memory leaks, please post them down below!

link to the module

please note that the model is packaged so the module is subject to updates and change


Parenting an instance to nil treats it as destroy and destroying an instance parented to nil doesn’t do anything

1 Like

afaik parenting to nil doesn’t destroy event connections and :Destroy() does It also locks the instance to nil.
Is this false?

1 Like

I think what you said is true, but my statement is also still true

1 Like

I did consider adding that functionality, however instances parented to nil are not destroyed as they still have references and can be re-parented. I could add a configuration with this setting, though.

1 Like

You currently treat parenting an instance to nil as though it were destroyed (opposite to what you just said)
It should not be treated as destroying because of what you just said

1 Like

What I mean is that cases exist where one might temporarily parent an object to nil, before re-parenting them to something valid such as workspace. It could be problematic if, when you want to temporarily parent an object to nil, it’s linked group gets destroyed.

workspace.PartA.Parent = nil
workspace.PartA.Parent = workspace -- instance still exists, and is still being used. 

Edit: I misunderstood what you said. I thought you were talking about a feature you thought should be added, but you were really talking about a design oversight/bug. If so, i’ll definitely look into this, thanks.

1 Like
local linkmodule = require(script.Parent)


print(p1.Parent)--nil, should be workspace
1 Like

Edit: After some testing, the issue of instances being parented to nil causing destruction has been fixed. However, the issue of detecting the destruction of nil instances seems impossible right now, so I’ve simply added the following warning when an instance is parented to nil:

"-LinkModule- Warning: instances cannot affect linked groups while parented to nil."

You can check if an instance is destroyed and not just parented to nil if pcall(parent change) errors

But AFAIK it is impossible to check WHEN an instance is destroyed if it is parented to nil (because ancestrychanged does not fire) <- which is the second ‘bug’ I mention in my first post about your module

You could make your own destroy function in the module to detect it. The only downside is they’d have to use that function for this module to work.

To determine whether something is destroyed, add some complicated dummy collectionservice tag to it and listen for this tag being removed. That way you can distinguish between “parent to nil” and destroy cases.

EDIT: Apparently this doesn’t let you distinguish between Destroy and parent to nil, because parenting to nil fires the removed signal on the collectionservice tag.

1 Like

Ill definitely experiment with this, thanks. As of now, I have no problems distinguishing between an object being parented to nil and an object being destroyed; my current problem is that I can’t seem to detect when a nil-parented object is destroyed. (since any sort of parent change event wont fire).