How do I detect if PlayerRemoving is due to server restart?

I’m trying to detect whether a player left the game due to a server restart. My game is centered around dueling, and players who leave mid-match are punished with losing their win streak, however server restarts run PlayerRemoving before game:BindToClose(), and I’m not sure how to prevent punishing players for server restarts.

I’ve already tried game:BindToClose(), storing a variable stating server shut down is true, and then preventing punishing players who are mid-match, however I soon figured out that PlayerRemoving happens before game:BindToClose(), therefore I can’t capture the reason PlayerRemoving happened in the first place.

I’ve read more than 5 dev forum posts specifically about this issue, and I can’t find a solution, even with posts that were marked with a solution, and ones specifically about logging and win streaks. I also spent 4 hours attempting solutions and using Roblox’s documentation, without any successful attempts detecting if PlayerRemoving was caused by a server restart.

I appreciate any help I can get.

What about using a DataStore key that sets a flag whether the server had shutdown? It would be in the BindToClose scope, and upon the server starting up it would check the flag.

I was also thinking something similar to this when I read this yesterday. Specifically, BindToClose provides a Enum.CloseReason argument that precisely tells you what happened to the server.

When the server starts, save a key inside a data store that contains the servers the player has played in, as an array (they’re all just tables but). You can use game.JobId, but I don’t know if that might cause some ID collision but it’s super unlikely.

When the server ends, save a key inside a different data store that contains who should be punished, the list of players (through UserIds), and who has read this key, as a dictionary. If the server shut down, save it as nil inside the dictionary.

When the player rejoins back into the game, check their player ID inside the first data store and check all their server IDs in the other data store. If there is no value associated with the key, the server has not ended yet, so you wait for another time to check it. If there is, the server has ended, and you can check the punish list and handle the punishment if necessary, and finally update the list of who has read this key. If you’re the final player to check the list (#whoHasRead == #listOfPlayers), you can delete the key.

[spoiler]please optimize this method as you wish bc as the method I just described seems so convoluted[/spoiler]

My game also has duels, and whenever I want to restart servers, I use ConfigService to disable some flags, just to be extra safe, I also publish a message using MessagingService, I actually have a small wrapper, I can share it with you if you want