Disconnecting functions

Is it good practice to use disconnect() on functions wherever I can?

It’s not vital, but it’s good practise. This prevents your game from consuming more resources than what’s necessary, which can help overall server performance.

1 Like

Only call connection:Disconnect() when a connection is not being used, and are usually used on instance’s that are not destroyed as “one time listeners” or “toggle- able listeners”. You do not however need to call it if the object gets destroyed after it’s use as all connections tied to the object is disconnected automatically. And as the reply above stated, it is a good practice.

1 Like

Will running :Disconnect() on a function that’s already been disconnected return any errors?

1 Like

Yes, all you have to do to prevent this is: if connection then connection:Disconnect() end

1 Like

Yes, actually it will.

In order to avoid errors, you can just do an if statement check to see if the connection still exists.

local con

if con then
	con:Disconnect()
end
1 Like

:Disconnect() is generally ran whenever a connection is not needed. You can save memory because you are cleaning up the connection and stopping it from unnecessarily using up resources.

Another thing to note here is that running :Destroy() on an Instance cleans up all connections that are connected to that Instance.

1 Like

Basically what you want to do with the Disconnect function is to garbage collect (The garbage collector manages the allocation and release of memory for an application. For developers working with managed code, this means that you don’t have to write code to perform memory management tasks.)
You can use https://github.com/howmanysmall/Janitor or NevermoreEngine/src/maid at main ¡ Quenty/NevermoreEngine ¡ GitHub

1 Like

No, providing the variable is still a reference to the RBXScriptConnection object (hasn’t been set to ‘nil’).