How could I stop this function getting called twice

Ok so, this is quite hard to explain but I have two teams, each with 2 players on each team. When 1 player dies it sets a bool for that player to true (to show they are dead). It then calls a function which checks if all players on one of the teams are dead. If not, nothing happens, if they are all dead then it runs some code. My issue is that if both players on one of the teams die at very similar times, the function gets ran twice. Any Ideas how I could fix this?

local function checkDeaths()
		-- Checks that one whole team is dead
		if T1Player1Dead == true and T1Player2Dead == true  then
        -- Normally some code here that runs
-- Functions that check when a player dies
	if T1Player1 then
		table.insert(connections, T1Player1.CharacterRemoving:Connect(function(hit)
			T1Player1Dead = true
			checkDeaths()
		end))
	end
	if T1Player2 then
		table.insert(connections, T1Player2.CharacterRemoving:Connect(function(hit)
			T1Player2Dead = true
			checkDeaths()
		end))
	end

Iā€™d really appreciate any help, cheers :slight_smile:

1 Like

You could use a cooldown for the function by checking if a bool is false then proceed with the code. As you proceed you set that bool to true to not allow any other code block to run the function, then you can later on set that bool to false by using task.wait().

1 Like

Since there are two potential connections firing the same function, there could be race conditions that happen when both players die simultaneously. In order to counter this, use a function that is the only thread for updating the game.