Hello everyone
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
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
If not, I’ll try to provide additional or more precise explanations
Side question:
Are there situations where disconnecting events isn’t necessary