Disconnecting Connections in Local Script

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! : )

2 Likes

If you don’t need to use a connection anymore disconnect it, applies to client and server

1 Like

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

3 Likes

Ahh okay got it, thanks for the clarification! : )

2 Likes

i need to clear something up

is it true that

local function Something(plr: Player)
   print("Hello " .. plr.Name .. "!")
end

game:GetService("Players").PlayerAdded:Connect(Something)

this will only make a connection once and NOT use up memory forever?

1 Like

No, but this would:

local function Something(plr: Player)
   print("Hello " .. plr.Name .. "!")
end

game:GetService("Players").PlayerAdded:Once(Something)
1 Like

okay and wait a second

this would run everytime a player joins, yes?

1 Like

It would fire the event once. Then make sure it never runs again.

1 Like

yea like what that guy said, you need to use :Once instead of :Connect if you want it to only run once and auto disconnect to save memory

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

1 Like

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! : )

Yup . That is why most vidual effects should be done on the client to save “short term” memory and improve performance and also network connectivity

I don’t really know if there is an in-depth documentation about disconnecting connections but i know that roblox has made a ‘performance optimization’ guide: Performance Optimization | Documentation - Roblox Creator Hub

1 Like

Sweet! I’ll be sure to take a look at this, thank you! : )

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