Are client player references kept upon leaving?

I’m making a little system for myself and I know that if, for example, references to player objects haven’t been removed after the player is gone (i.e. cache for player data), the player object persists until the server ceases to exist. Is this the same case with the client?

Yes, if you have an object referenced in your code, it cannot be garbage collected. So if an object leaves the game hierarchy (such as someone leaving which causes their Player object to be unparented from Players), you need to set any references to that object to nil and only then it can be garbage collected. This is especially important for Player objects and other kinds of objects that are repetitively created and destroyed over the course of the game.

1 Like
local Connection
Connection = LocalPlayer:GetPropertyChangedSignal("Parent"):Connect(function ()
	if not LocalPlayer.Parent == game:GetService("Players") then
		LocalPlayer = nil
		PlayerGui = nil
		Connection:Disconnect()
	end
end)

So I assume this would be sufficient for my purposes then?

To break it down. If you store the Player object anywhere in your code, then that object will forever be kept in memory and never “garbage collected” until the object is no longer in your code.

For example:

local AllOfThePlayers = {};

function OnPlayerJoin( PlayerObject )

   AllOfThePlayers[ PlayerObject.UserId ] = PlayerObject

end
game.Players.PlayerAdded:Connect( OnPlayerJoin )

^^^ That will store every player that ever has existed into a table (with the UserId being the key, and the Player object as the Value). The problem here is if the player leaves, the player object will never be garbage collected until the object is no longer stored in a variable.

To fix that:

function OnPlayerLeave( PlayerObject )

    AllOfThePlayers[ PlayerObject.UserId ] = nil

end
game.Players.PlayerRemoving:Connect( OnPlayerLeave )

^ This will set the key/value we were using for this player to nil, and thus the object is cleaned.

Just check to make sure any references to the Player object is cleaned up in your code. This is all the same for any object in your game, not just Player objects.

1 Like

If your scripts don’t hold any references to Players, they’ll get GCed on the server (soon) after the players leaves, but never on the client. For it sticking around on the client, not sure if it’s a memory leak in a CoreScript (which would be weird cuz different VM), chat script (although I deleted them when testing) or just something internal. Still, even if you’re in the same server for 3 hours with players constantly joining and leaving, I doubt it’d have any noticeable effect.

When writing code, it’s nice to know that events get disconnected when :Destroy() is called (which should happen when the player leaves). If the only code with access to local variables are event connections that get disconnected, there shouldn’t a memory leak.

you could play around with weak tables, or, like alexnewtron said, clean up data whenever you have to

1 Like

Be careful with weak tables instead of explicitly cleaning things up. There’s an edge case where a player teleports from Place1 to Place2, makes some money, then back to Place1, and data is cached so they lose progress. Very specific edge case, but something similar has happened before.

2 Likes