Disconnect Events only on the Listener-Side?

Hello everyone :hugs:

This might turn out to be a stupid question, but I can’t figure it out or think of a way to test it in Studio by myself.

Short Version:
Do I need to disconnect remote events only on the listener side or on both (the sender and listener)?

I would very much appreciate if you read the following, more detailed version to be sure there is no misunderstanding :smiley:

Until now I just connected Remote Events like this (without worrying about disconnecting them):

"Sender"-Script:

-- a simple Button in StarterGUI that sends a request to a server-script with the argument <10>
local button = script.Parent.sendrequest
local requestEvent=  game:GetService("ReplicatedStorage"):WaitForChild("requestEvent")

button.Activated:connect(function()
	print("Button pressed")
	requestEvent:FireServer(10)
	print("Request-Event fired")
end)

"Listener"-Script:

local requestEvent=  game:GetService("ReplicatedStorage"):WaitForChild("requestEvent")

local function checkrequest(player, argument)
 -- check request and either follow or dismiss it
end
requestEvent.OnServerEvent:Connect(checkrequest)

Due to the memory usage in my game being three to four times higher than the given target (even though there is nothing in the game but a little waterbody) I learned that disconnecting events is necessary to prevent memory leaks/ keep memory usage lower. Although this won’t solve the memory issue, it still seems very important to know.
I found this and would conclude that I only have to disconnect on the listener side.

--taken from the linked page, does not fit my example before
local connection
 
local function onPointsChanged(newPoints) 
	print("Points: " .. newPoints)
	if newPoints >= 50 then
		-- Stop listening for changes if we have at least 50 points
		connection:Disconnect()
	end
end
 
connection = points.Changed:Connect(onPointsChanged)

I think I understand, how and why it is assigned to a local variable, since it should disconnect, when the variable is destroyed. (Right? Or is this to be able to make a reference inside the function?)
But I am still uncertain, if I should to do it both on the listener and the sender side.

I hope I made my problem clear :smiley:
If not, I’ll try to provide additional or more precise explanations

Side question:
Are there situations where disconnecting events isn’t necessary

If for some reason you need to disconnect your events, you need to disconnect them on both sides, because the sender doesn’t care if there is a receiver connected or vise versa, its only job is to listen/send.

And if you are concerned about memory usage, you shouldn’t be, the connection itself barely affects anything, your code inside the events is the deciding factor for the memory usage. For example, you can connect 100 events that do nothing when called and you’d have no performance hit, but you can connect 1 event that does something major when called and that will affect your performance.

And if you are concerned about memory usage, you shouldn’t be, the connection itself barely affects anything

This is true, just make sure you’re not making connections inside of loops, because if you are then you should be disconnecting them at the end of each cycle of said loop.

Only disconnect functions if you don’t have to ever use them again. The only times you don’t have to disconnect functions are when it’s already disconnected (if you have a touched event on a part, and the part gets destroyed, that touched connection will be disconnected immediately).

Thanks for the answers everyone :smiley:

When I read this post about connections causing memory leaks, I was very unsure when and how to prevent it.
Because of your explanations I reread it a couple of times.
Seems that I misunderstood the article and the disconnection is only helpful when the connection holds a reference to an object that in turn can’t be garbage-collected.

I’ll continue working on my game without worrying too much now.
Thank you :smiley: