Are scripts' connections inside Player Scripts automatically garbage collected/disconnected?

Let’s suppose I have a local script inside StarterPlayer -> StarterCharacterScripts, and the script creates a RunService connection. When the character dies/resets, is the connection automatically deleted, or do I need to disconnect it manually?

This is something you can easily test yourself; generate a random value, print it repeatedly, and if the old value keeps printing after the Character is reset, the connection isn’t automatically disconnected.

It turns out that they are automatically disconnected, after the Character has respawned:

local r = math.random()
local i = 0

game:GetService("RunService").RenderStepped:Connect(function()
	if i == 0 then
		print(r)
	end
	i = (i + 1)%60
end)

image

If you need the event to be disconnected the moment the Character dies, then you should still disconnect it manually.

1 Like

Makes sense, I was dumb for not testing it :skull:

Connections regarding characters and players have been announced through this update last year:

2 Likes

Nah, not dumb at all! It’s a good question to ask, it just didn’t occur to you how or that you could figure it out on your own!

There’s zero shame in asking for help, even if the answer is simple or easy to obtain. :slight_smile:

1 Like

Thanks for the information, I will take a look into it.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.