function ToggleFlash(actionName, inputState, inputObject)-- Play Flash toggle animation
    local FlashAnim = Char.Humanoid.Animator:LoadAnimation(AnimFolder.ToggleFlash)
    if inputState == Enum.UserInputState.End and not isPlaying  then
        isPlaying = true
        FlashAnim:Play()
    end
    FlashAnim:GetMarkerReachedSignal("ToggleFlash"):Connect(function() -- Turn Flashlight on and off when flash animation signal reached
        --stuff here
                isPlaying = false
    end)

end

Does creating a connection inside a function needs to be disconnected to prevent resource being used more than necessary,

or will it get automatically disconnected once the function ended, because "FlashAnim" is a local variable and "Connect" function get disconnected if the related object disappear, and since i assume local variables gets cleaned up when its parent function is finished, i guess it's logical to assume it got auto disconnected? but i just wanna make sure, just in case.

3 comments

0

They only get disconnected if you :Destroy() them, it's better to disconnect it.

0

@imKirda I don't think that's true since connection to Player's Character or related object like Humanoid, disconnected when the player dies and respawned

0

Humanoid is destroyed on death so it disconnects the connections. You are actually right, it gets disconnected on garbage collection so it's not needed to be disconnected.