Do I have to disconnect these events created under playeradded?

local Players = game:GetService("Players")
local function onCharacterAdded(character)

print(character.Name .. " has spawned")

local function onCharacterRemoving(character)
    print(character.Name .. " is despawning")
end
local function UpdateMoney(newBalance)
    --money updating
end
local function onPlayerAdded(player)
    player.CharacterAdded:Connect(onCharacterAdded)
    player.CharacterRemoving:Connect(onCharacterRemoving)
    player.Money.Changed:Connect(UpdateMoney)
end
Players.PlayerAdded:Connect(onPlayerAdded)

So if the player leaves, technically the money, charadded and charremoving event still exist and were never disconnected. Does it need to be disconnected and what method would be best to disconnect them?

You disconnect events, but not functions. Leaving event connected when no longer used will cause a memory leak.

I believe these are all disconnected automatically by the garbage collector

If the player doesn’t exist anymore then I think it disconnects them automatically, at least it does when you destroy instances so I would think the same applies here

1 Like

No. Not events. You have to disconnect them when no longer in use.

When an instance is destroyed its events all disconnect automatically.

1 Like

When Luau destroys an event’s object, such as the Player object when a user leaves the experience, all of its connections disconnect automatically.

https://create.roblox.com/docs/scripting/events/using-events

1 Like

You still get a memory leak.

They literally reference destroy in the post, players leaving and instances being destroyed automatically remove all connections:

do -- Also also all good, as Destroy() implicitly disconnects all connections
    local p = Instance.new('Part')
    p.Touched:connect(function() print(p) end)
    p:Destroy()
end

(taken from the post you linked)

Then they changed it since the article that I linked was posted. I always disconnect/destroy stuff so I do not get memory leaks.

Roblox has a fantastic section in their documentation showing how to handle events, I would recommend reading it.

https://create.roblox.com/docs/scripting/events/using-events

Yep that’s expected behavior when objects are destroyed.

However for player removing there’s this odd quirk or bug going around where it isn’t being destroyed

Maybe try manually destroying it for @TheCreatorBenn.

3 Likes

I believe that’s the intended behavior, it’s to allow for yielding code to interact with a disconnected user’s player instance, think game:BindToClose et al.

2 Likes

Roblox auto disconnects every thing related to a player when he leaves, no need to worry about it.

I see so the player doesn’t actually get when the player leaves and that’s why we need to destroy the players manually when we’re done using it.