Can the code give an error?

I need to know if CHARACTER.Container is guaranteed to be accessible every time the player exits the game

function OnPlayerAdded(PLAYER)
	PLAYER.CharacterRemoving:Connect(function(CHARACTER)
		if not PLAYER.Parent then
			
			for i,v in pairs(CHARACTER.Container:GetChildren()) do
				print(v.Name)
			end
			
		end
	end)
end

game.Players.PlayerAdded:Connect(OnPlayerAdded)

for i,v in pairs(game.Players:GetChildren()) do
	OnPlayerAdded(v)
end

CharacterRemoving is firing before it’s actually removed. So you should be able to access it’s instances.

Why don’t you use Players.PlayerRemoving?

This event can be triggered when the player respawns .So The correct thing to do is to use Players.PlayerRemoving . It is called before it is destoryed

Would using PlayerRemoving bring the possibility that the references for the player’s character point to nothing?

1 Like

Sorry for the late reply. This is not possible for wild pointers because the registration callback function in RBXScriptConnection is called before the player leaves. In addition, Roblox uses a server authoritative model: when the player leaves, the PlayerRemoving event occurs, after the player clicks the leave button, the server accepts the leave signal, executes the callback function, and then processes the player data GC . For wild pointers, Luau language doesn’t do this either, but since RBXScriptConnection uses C++ Class, it’s very likely that the vulnerability will be triggered in certain cases. Also server runs the player model and player instance separately on 2 separate events CharacterRemoving and PlayerRemoving . When server is handling player left then fires PlayerRemoving and CharacterRemoving but CharacterRemoving is before PlayerRemoving.

1 Like

Luau’s purpose is memory safe garbage collection
You cannot break the rule. If Luau can make wild pointers and null pointers, then this is a huge mistake

As others have said, CharacterRemoving can fire at points when the player does not leave the game. However, if you use Players.PlayerRemoving, the player’s character has already been destroyed by the time the event is fired, so you can’t access it then.

Is it possible you can store CHARACTER.Container under the player instead? If you can, there’s a small window of time when Players.PlayerRemoving fires you can use to very quickly move it out of the player and to a different parent, maybe ServerStorage, while you process it, then you can destroy it afterwards.

it turns out that one of the most important events is missing in roblox.