How can I account for a player that leaves mid-round?

Hi,

I am working on a game that works on a round-based system. When the round starts the game gets all the players that are participating and stores them in an array. The game works with a “tagger”, which is randomly picked from the players. The “tagger” catches the other players which passes on the tag to them. When a specified time limit passes, the script eliminates the current “tagger”. It then selects a new player as the “tagger”.

However I don’t know how I can account for players that leave mid-match. As at the moment the script does not remove the from the array. I thought of placing the array in a String value and removing the leaving player from another script with a .PlayerRemoving event, however I realised that in very rare cases, the PlayerRemoving script and the standard game elimination might occur at the same time, leading to an overwrite of the array, thus causing the main game script to break at some point.

I also need my main game script to randomly select another player as the new “tagger” if the player that is leaving is currently the “tagger”.

Could anyone help me out?

Thanks,
happyfloppy6

Have a PlayerRemoving event listener (https://developer.roblox.com/en-us/api-reference/event/Players/PlayerRemoving) in your script. Inside the connected function remove the leaving player from the array and if he is the tagger then change the tagger. The player array and the tagger must be global script variables for this to work but I assume they already are.

Alternatively (and better), I suggest moving the array and the tagger inside a module script. In this way you can have multiple scripts using/modifying the array and the tagger.