Does Roblox clean up event listeners for me?

I have code that runs for each character in my level on RenderStepped.

When the character dies, I have the script destroy itself.

Do I need to disconnect from RenderStepped before calling script:Destroy() or will Roblox do this for me?

In other words, does the code below leak memory?

image

3 Likes

The Roblox game engine will clean up event connections for you when your script is deleted. Memory will not be leaked from dead connections.

2 Likes

When a script is destroyed the connections created inside it are as well.

In ServerScriptService I had this script

game:GetService("Workspace").ChildAdded:Connect(print)

But when I delete the script:

it stops printing.

You should be fine.

No it doesn’t it will only clean up if the script is deleted or disabled. If your not destroying the script then you will have to Disconnect() them yourself if you dont need them anymore. I highly recommend you do this because having event listeners you don’t need will take a performance toll

1 Like