Do i need to disconnect functions?

lets say i have

	Plr.Character.Changed:Connect(function()
                --Do Stuff
	end)

do i still need to disconnect the function somehow even if the player already left the game? because i heard that having so many things like this will take so much resources and the client will lagg, so do i need to disconnect these when the player leaves the game? or it automatically stops taking resources?

1 Like

Yes you should disconnect events you’re not using anymore.
You can disconnect events by either calling :Disconnect() on them or by deleting the instance the event is connected to. In this case when the player leaves the game the player instance is destroyed and the event is automatically disconnected.

Actually i didn’t properly read your code. Are you aware what this code is doing?
It listens when a property of the player’s character’s model is changed in this case the event will automatically disconnect when the player’s character is destroyed.

If you’re going to use an RBXScriptSignal only once, then you should use the :Once() method so the signal disconnects itself after code has been executed.

Something.Changed:Once(function() -- The event will disconnect itself after all code under the function runs.
    -- ...
end)
1 Like

You should definitely disconnect functions when you are not using them anymore yes. If you have too much function connected that are not used it will cause a memory leak which will like you said make the client lag.

oh, so it means these kind of functions are always connected to the instances they are watching?, so it means as long as the instance has been deleted in the game, the function no longer takes resources aswell

Exactly, this is also explained in the documentations of Instance:Destroy().

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