Will this connection get GC'ed?

I have a StringValue in Workspace, that changes its value often.

A LocalScript in your PlayerGui is connected to like this;

workspace.StringValue.Changed:Connect(function()
   print('Changed')
end)

When your character dies, and your PlayerGui is reset, will this connection break and get GC’ed?

Essentially, what gets GC’ed in the script when the script gets destroyed?

1 Like

You can’t disconnect connections unless you tell it to do so. The only damage you can do at most is just give your client or server more tasks to do.
Additionally, you’re only printing so there’s no real damage in doing that over and over.
What does GC stand for?

EDIT: I misinterpreted. The connection will still be made again. The connection you made in the local script would be disconnected when the gui resets- but comes back again when the script runs again.

Garbage collected aka automatically disconnected by Lua.
(At least that is what GC stands for for me).

1 Like

Garbage collection is the freeing of unused objects from memory, which is done by determining whether it has any references left. It does not have anything to do with disconnecting events. Disconnecting events is done by calling :Destroy() on instances or calling :Disconnect on the RBXEventListener.

4 Likes

I did two tests, one with your specific example and one with checking if a table is gc-ed instead of the connection

prettysureeverythingisgced.rbxl (14.3 KB)
impossible.rbxl (14.5 KB)

(run them yourself with a local player and look at the code: two localscripts, one in ReplicatedFirst and one in StarterGui)

the first one shows how a table is gced when the localscript in playergui is removed (destroyed?-there is probaly something somewhere on the wiki that says they are destroyed) on player respawn

the second one shows how you can’t tell when/if the connection is gc-ed because roblox connections/events are implemented in c++ and there is no strong reference to a connection via lua (I guess it serves as proof of that they are implemented in c++)

but I am guessing that the script is destroyed when the player dies and that when a script is destroyed, the connections are disconnected too since the table was sucessfully gc-ed (maybe you could test to see if other objects are gc-ed besides tables~instances)

on a side note: i would avoid placing any [local/module]scripts in playergui and i would never destroy scripts
just use the characteradded event

I read on a Wiki post a while ago that Event connections are broken (and GC’ed) when any of the following occur:

  • The Instance the Event is a member of is destroyed (in your example, the Event would be broken and GC’ed when the StringValue is destroyed)
  • The Script or LocalScript in which the connection was defined is destroyed
  • The connection is manually broken using :Disconnect()

So, in your case, when the character dies, the LocalScript will be destroyed and automatically break the connection.

EDIT: You can find proof of the first and third conditions here. The original Wiki post I am speaking of was a victim of the recent changes to the Developer Portal, but the second condition can be proved simply enough with a short experiment.

2 Likes

Can you share the link in the developer portal where you found this information?

2 Likes