Is a Player already removed when PlayerRemoving fires?

If I were to use game.Players:GetPlayers() as soon as PlayerRemoving is triggered, would the returned array include the player who left or are they already completely removed from the game? I guess I’m just a little confused by the present tense “Removing”.

1 Like

This is a complicated question.

Yes, the player is already gone. You cannot get input or show any sortof thing on screen to them in this event.

However, the player object will exist until it is garbage collected. So you can use that object to do anything you need like save data.

Edit: I’m unsure on :GetPlayers(), but I believe they would not be in that array immediately after PlayerRemoving fires. Might be worth testing.

5 Likes

I’m only concerned about the Player object being present in a GetPlayers() result, I suppose I’ll try to test this when I get home.

My game currently fires GetPlayers() a second after a player leaves and it does seem to work, but I want to remove the time delay and I’m not sure when Roblox updates the yield function.

Yes, as soon as Players.PlayerRemoving fires, the Player object is parented to nil. This is true on the server, I’m not sure if this is the same on the client. ( It should because of order of replication, but I’ve never tested it )

If you keep a reference to the Player object in Lua, you can still access its properties even while it’s parent is nil. However, you shouldn’t keep a reference to it for too long ( usually until datastore is saved ), otherwise the Player object will never get garbage collected and you have a memory leak.

7 Likes

Just to be sure that every game I’ve ever made doesn’t have a memory leak in it:

If I have a serverside table that indexes players on join like so:

sessionData[plr] = defaultSessionData

Since the entry in the table is garbage collected when I do this on player leave:

sessionData[plr] = nil

The player gets garbage collected, right?

1 Like

Yep, that’s correct! But personally I would use Player.UserId as a key for storing session data.

3 Likes

If you keep a reference to the Player object in Lua, you can still access its properties even while it’s parent is nil.

Note that as far as I’m aware, a lot of the properties of Player will be set to nil. The ones I know are Character and Team.

Is playergui still accessible after the player is nil?

You shouldn’t access PlayerGui from the server period. I wouldn’t know.

1 Like