Do nested connections disconnect when the script is deleted?

game.Players.PlayerAdded:Connect(function(plr)
        plr.ChildAdded:Connect(function(obj)
                --blabla
        end)
end)

Will the ChildAdded connection disconnect when the script gets deleted?

1 Like

Yes indeed, they disconnect when the script is destroyed or the object the connection is tied to is destroyed.

Correction: The above statement is true for objects that dont persist after the scripts lifecycle, however for objects/services that do such as PlayerService and ChildAdded, they will persist regardless if the script gets deleted - except no code will be ran because the script is gone, just that the event will still be there since neither of the objects the events are tied to will get deleted until the player disconnects and their object goes away with it or the server goes down and well everything gets cleaned up and destroyed.

Yes. You do not need to disconnect them. :Destroy() disconnects them.

Description of :Destroy():

edit: sorry for repeating a reply i misread the one above mine

Ok thanks, my game has a memory leak due to connections and I just want to rule that out as a probable cause.

1 Like

You can freely post what you suspect is causing those leaks, and maybe we can help you figure out the cause. My initial thought is that you’re keeping a reference to some variable/object or not disconnecting/cleaning things properly, but i cant tell for sure until we see the code.

I looked at LuauHeap Object Classes and I saw that whenever I spawned a model containing this script and then deleted it 1 connection didn’t get deleted so I thought nested connections don’t get disconnected

local connections = {}
function MakeGrabHandler(parent,funct)
	local function check(v)
		local conn
		conn = v.ChildAdded:Connect(function(v)
			if v.Name=="LastGrabbedPart"and v.Value.Parent==parent then 
				funct(v)
			end 
		end)
		connections[v] = {
			ChildAdded = conn
		}
	end
	game.Players.PlayerAdded:Connect(function(v)
		check(v)
	end)	
	for _,v in pairs(game.Players:GetPlayers()) do
		check(v)
	end
end
game.Players.PlayerRemoving:Connect(function(plr1)
	if connections[plr1] then
		if connections[plr1].ChildAdded then connections[plr1].ChildAdded:Disconnect() connections[plr1].ChildAdded = nil end
	end
end)

Code may be a bit messy because of all the things I tried to make that one connection disconnect (I don’t know which one doesnt disconnect)

When do you call the MakeGrabHandler? once? many times? because if its many times then youre consistently creating connections for the PlayerAdded function which will never get cleaned up automatically… also are you passing some sort of function to the MakeGrabHandler? what does that function do?

To me this is super messy and causes a lot of coupling and functions/scripts depending on one another which will ofc cause leaks and harder ways to track where the problems come from…

MakeGrabHandler(script.Parent, function(v)
	if activePlaylist.Name ~= v.Value.Name then
		PressButton(v.Value)
	end
end)

Only once

1 Like

Alright and can you tell me more what youre trying to achieve with this code? Like, what is its purpose - im trying to figure out if there is a simpler (very likely) way of doing what you want to do

Its a jukebox where you grab the buttons and it plays a song, also whats weird is when I create 10 of those and delete them all only one connection remains, not 10.

How do you know only one remains?

I looked at dev console LuauHeap

1 Like

If only one connection persists then it should be the PlayerAdded one as that service doesnt get deleted. Which is normal. Unless you create that same connection every time you run MakeGrabHandler, but you said you run it once so it should be okay. You do clean it up on playerRemoving too but I suggest you start naming your variables/arguments for functions precisely clear, as they get confusing when they’re one letter.

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