When I connect a function to CharacterAdded, do I need to disconnect it when the player leaves to prevent memory leaks, or does it disconnect on its own, would the following code be fine?
game.Players.PlayerAdded:Connect(functions(player)
player.CharacterAdded:Connect(function(Char)
-- // Do something
end)
end)
Or should I disconnect it when the player leaves like this?
local connections = {}
game.Players.PlayerAdded:Connect(function(player)
connections[player] = player.CharacterAdded:Connect(function(Char)
-- // Do something
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
if connections[player] then
connections[player]:Disconnect()
end
end)
Any help is appreciated!