If a script is destroyed, are any connections I made from the script automatically disconnected?

Suppose I have a simple Script or LocalScript such as

SomePart.Touched:Connect(function()
    script:Destroy()
end)

Should I disconnect the Touched connection manually to save memory?

What I know is that if SomePart is destroyed then signal will be disconnected but what if the Script or LocalScript that created the connection is destroyed?

Destroying an instance will disconnect all connections.

https://developer.roblox.com/en-us/api-reference/function/Instance/Destroy

According to the Developer API linked above, the Destroy function

“Sets the Instance.Parent property to nil, locks the Instance.Parent property, disconnects all connections and calls Destroy on all children. This function is the correct way to dispose of objects that are no longer required. Disposing of unneeded objects is important, since unnecessary objects and connections in a place use up memory (this is called a memory leak ) which can lead to serious performance issues over time.”

2 Likes

Does this mean if SomePart is destroyed or if the Script or LocalScript is destroyed?

After some research, it applies to both either the script or the part instance.

According to the Developer API of Scripts:

Additionally, the thread will be stopped if the Script or one of its ancestors is destroyed. A script will continue to run even if the Parent property is set to nil (and the Script is not destroyed).

The :Destoy() function under the Script API:

Sets the Instance.Parent property to nil, locks the Instance.Parent property, disconnects all connections and calls Destroy on all children.

Also According to the Handling Events API:

when an object is destroyed, all connections to that object’s events are disconnected automatically.

Found similar threads that can confirm:

6 Likes

The answer is yes. It keeps going for about two frames because of the time it takes for the server to replicate to the client, but that’s about it.

3 Likes