Script Removal and Garbage Collection

Hello hope you are feeling well! Let’s say you have a script with code:

workspace.DescendantAdded:Connect(function(Object)
        print(Object.Name)
end)

If this script gets destroyed will all the memory used by this script get GC and also, will the event disconnect?

Calling script:Destroy() sets it to nil while also disconnecting all connections

Read more here:

disconnects all connections and calls Destroy on all children.

1 Like

To be fair, Destroy() disconnects all connections on that object, e.g. if another script had a .Changed event hooked up to this script, that Changed event would be disconnected.

That wiki page doesn’t necessarily say “disconnects connections made by that object”, since that’s only relevant for scripts.

In any case, I still agree and think the answer is probably “yes”.

3 Likes

Deleting a script will make all of the variables eligible for garbage collection. If you want more information on garbage collection read this post: Garbage Collection and Memory Leaks in Roblox - What you should know

1 Like

I’d also like to clarify, this requires that the threads in the script also stop running (either by yielding, or ending).

1 Like