Should I disconnect an event when it will never trigger again?
Asked by
5 years ago Edited 5 years ago
Let's say I have the following code in a local script located under StarterPlayerScripts:
01 | game:GetService( "Players" ).LocalPlayer.CharacterAdded:Connect( function (character) |
04 | died = character:WaitForChild( "Humanoid" ).Died:Connect( function () |
Whenever a character is added, I listen for the "Died" event from their given humanoid. Whenever a humanoid dies, the character is removed from the Workspace and replaced with a new one, which means I listen for the "Died" event on a new humanoid.
So my question is: Should I bother including line 6 in my code? Is it good practice? Is there any sort of lag caused by listening for events that won't trigger? Or is there some sort of garbage collection going on that automatically disconnects any events whenever the object whose event that's being listened for is destroyed?