Do local events automatically end when a function stops execution?

Hey, so just recently I got some information in regards to disconnecting events in functions to clean up memory and so no memory leaks occur. With that being said, I wondered if I declared local events in my functions, if there would be no need to disconnect those events, but rather, when the function finishes execution it’ll just go off of the stack by itself? Or would I still need to disconnect it?


What do you mean by local events? Bindable events? Remote events? Please can you clarify what you mean.

Also, can you show some code to illustrate what you mean by “declared local events in my functions”. It might be me, but I don’t understand what you mean.

Please clarify these :grinning:

1 Like

I think what @C_Sharper means by local events could be something like this:

local function aFunction()
    local event ="BindableEvent")
    -- other code that uses the event

When aFunction finishes execution, what happens to the event if you don’t call :Destroy on it? Does it destroy itself, along with its connections, or does it leak into memory?

1 Like

In this case, yes it would. You made the event parented to nil and left the reference as a local variable.

This means both will be garbage collected when the function ends.

If you don’t want this to happen, you need to have a variable to store the reference in outside the function and set it to that. Also, parent the bindable event to workspace (or wherever you want). This makes sure it won’t be destroyed when the function exits.

EDIT: I’m sorry, I thought you were @C_Sharper for a second there. This post is meant for him


Thanks for the replies guys!

And whoops, didn’t realize my original question could mean multiple things…I was actually referring to the built-in events that you can connect to functions.

So to back up my original question, an example like this:

   function blah()
   local connection = bricky.Touched:Connect(function()

Sorry for poor formatting, still can’t figure out how to write code efficiently on these forums, while maintaining the syntax or what-not.

The event will not self-disconnect once the function blah exits, if that’s what you mean.


Oh hm, I see. Sort of surprised by that actually, as I figured if I made the variable local to the connection made, that would rid of it’s location in memory after the function stops executing. Any clarifications as to why doing this doesn’t end the connection all together, that’d be great. Not really necessary, but I’d be interested in hearing as to how this happens.

Thanks Kiriot!

It happens because the RBXScriptConnection returned by Connect() is only used to eventually disconnect the event handler later on. It isn’t the actual internal connection between the function and the event. That’s also why connections work properly even if you don’t declare them as variables.


If you want to double check something is still connected (although as @Amiaa16 said) it should be, you can use the RBXScriptConnection.Connected property of the connection.

However, (as also said above) the connection reference will get garbage collected, so you need to store it in a global variable to use this.


Ahh I see, that makes sense. Thanks for that info!