Hi! I’ve been wondering about this and have looked through numerous forums regarding disconnecting connections within local scripts (specifically connections that remain throughout the player’s time on a game) but have found differing opinions. To elaborate on what type of connections I’m referring to, here’s an example connection:
-- Example connection script whose parent is 'StarterPlayerScripts':
remoteEvent.OnClientEvent:Connect(eventFunc); -- Event that lasts "forever"
I know that for temporary connections (at least on the server side), it’s best to manually disconnect them for better performance but does the same apply for connections in local scripts that last “forever” or are temporary? Many thanks! : )
All connections take up memory FOREVER until you disconnected it or it disconnects itself.
Eg) :Connect() last forever and is usually the go to . You can even add a variable before the connection so that you can disconnect it remotely
local c
c = stuff:Connect(function()
end)
when you set a variable to the connection, you have the ability to disconnect it by writing
c:Disconnect()
there is also :Once() which is like Connect but it automatically disconnects after it detected one event. It is the go to for connecting Events that you know would only run once like Humanoid.Died
after this yapping. YES connections in Server script are same as connections in Client scripts
Sorry, but I do have another question: I want to disconnect connections on the client side to free up memory whenever the player leaves (hence the question of “local disconnecting.”) However, from what I’ve seen, people seem to be using Players. PlayerRemoving. Given that it executes for any player and not solely for the local player, are there better alternatives for performing client-side actions whenever the player leaves the game? Many thanks! : )
Hmmm . I assume you want to disconnect connection in a local script whenever that local player leaves? If that is thr case , you do not need to disconnect the connection. This is because all client side memory is stored in the client’s computer and not the server . When the client computer leaves the game , the memory would automatically be cleared . If this is not what you are trying to ask, please clarify with me cuz I am a bit confused
I’m addition, if you want to detect the client leaving using a local script, you can do so by writing this
local plr = game.Players.LocalPlayer
game.Players.PlayerRemoving:Connect(function(p)
if p ~= plr then return end
--if it pass the check above, the client is leaving
end)
Interesting! I didn’t know the client’s memory was used in such a way, always thought it was managed in one whole piece haha. Any idea where I can find the documentation that goes in-depth about this? Thanks in advance! : )