Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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:

game:GetService("Players").LocalPlayer.CharacterAdded:Connect(function(character)

    local died
    died = character:WaitForChild("Humanoid").Died:Connect(function()

        died:Disconnect() --Should I include this line?
        print("You Died :(")

    end)
end)

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?

1 answer

Log in to vote
0
Answered by
Uluomis 32
5 years ago

From personal experience, I've never used the :Disconnect() function and everything I've scripted with a function in it runs perfectly fine. So if I were you I wouldn't include that line.

Ad

Answer this question