Can I be sure that the player is not removed in this scenario?

Hello! I’m currently in the process of writing a round system for my PVP game.
It’s going great so far but I do have one concern.
There’s no way I can allow my rounds script to error because that would effectively cause the whole game to break.

My rounds script handles a lot of the players by adding instances, moving characters CFRame and so on. Since any of these players could choose to leave the game at any moment I need to confirm that they are still in the server. I was thinking of just pcalling a lot of my operations but I figured that’s just a rubberband solution to this issue.

So, what I need to know is whether I can trust the player instance to always exist in a scenario like this:

	for _, player in pairs(players) do
		if not player then return end
		
		-- Player leaves here as an example
		
		-- Can I be sure that this instance under player remains?
		-- Or would I need to always check for it?
		player.test.Value = 2
	end
  1. In this loop is there any way that a player could leave and the code therefore errors?
  2. Can I be sure that all instances I have under the player is there until it’s fully removed?

Thanks in advance for any responses.

1 Like

Where the comments are, it should be safe. Unless you add a yield like task.wait() somewhere.

If you are worried about the program breaking from an error, you should look into using pcall functions.

No, you cannot be sure the player instance and its descendants will remain until fully removed. Players can leave at any moment, which can cause errors when trying to access their properties.

To handle this, you should check if the player and the instance still exist before performing operations. Here’s an example:

for _, player in pairs(players) do
	if player and player:FindFirstChild("test") then
		player.test.Value = 2
	end
end

Using pcall is also a good practice for critical sections to prevent the script from breaking.

Are you sure, it seems weird to me that Roblox would put all a descendasnts to nil before the player itself. It’s not very cost effective always having to confirm if an instance always exists.