The destroy wiki explains that destroy “Sets the
Instance.Parent
property to nil, locks theInstance.Parent
property, disconnects all connections and calls Destroy on all children”
As a Roblox developer, it is currently too hard to manually unlink the player signal connections (such as CharacterAdded) to prevent memory leaks and other bugs that may rely on the player connections being disconnected (because you expect certain items to be garbage collected, but they persist indefinitely)
If Roblox is able to address my issue, it would make developing a lot less painful because I would not have to track all player connections to disconnect them when the player leaves
Proof that the player is improperly destroyed (connections are not disconnected)
The following code shows that the table x persists indefinitely because the CharacterAdded connection is not disconnected
local t=setmetatable({},{__mode='k',})
game:GetService'Players'.PlayerAdded:Connect(function(plr)
local x={}
t[x]=true
plr.CharacterAdded:connect(function(char)
print(x)
end)
end)
while wait()do
local n=0
for k,v in next,t do n=n+1 end
print(n)
end
If you comment out the print(x) inside of CharacterAdded, lua properly garbage collects the table x
Additionally, if you manually disconnect the player connections (what we unfortunately have to do now), then lua also properly garbage collects the table x:
local t=setmetatable({},{__mode='k',})
local cons={}
game:GetService'Players'.PlayerAdded:Connect(function(plr)
local x={}
t[x]=true
cons[plr]=plr.CharacterAdded:connect(function(char)
print(x)
end)
end)
game:GetService'Players'.PlayerRemoving:Connect(function(plr)
cons[plr]:Disconnect()
cons[plr]=nil
end)
while wait()do
local n=0
for k,v in next,t do n=n+1 end
print(n)
end
I want to clarify that this doesn’t just happen for the CharacterAdded connection, I believe it happens for ALL RBXScriptSignals in the player object (I tested CharacterAdded, Chatted, and DescendantRemoving)
Hopefully this thread clarifies the issue better