local function I()if#PlayerManager.ActivePlayers==0 then RaceManager.OnRaceFinishedEvent:Fire()end end local function b(I,b)if b then PlayerManager.Selections[I]=b end for b,V in pairs(PlayerManager.ActivePlayers)do if V==I then return end end table.insert(PlayerManager.ActivePlayers,I)CheckpointEvent:FireClient(I,{{"\115\101\116\065\099\116\105\118\101";true}})if#PlayerManager.ActivePlayers>=GameSettings.minimumPlayers then onMiminumActivePlayersEvent:Fire()end end local function V(b)CheckpointEvent:FireClient(b,{{"\115\101\116\065\099\116\105\118\101";false}})for V,p in pairs(PlayerManager.ActivePlayers)do if p==b then table.remove(PlayerManager.ActivePlayers,V)I()return end end end
-- Runs when player dies or leaves. If no player is left, end round
local function checkPlayerCount()
	if #PlayerManager.ActivePlayers == 0 then
		RaceManager.OnRaceFinishedEvent:Fire()
	end
end

local function addActivePlayer(player, speeder)
	if speeder then
		PlayerManager.Selections[player] = speeder
	end

	for playerKey, whichPlayer in pairs(PlayerManager.ActivePlayers) do
		if whichPlayer == player then
			-- Player is already in table
			return
		end
	end
	table.insert(PlayerManager.ActivePlayers, player)
	CheckpointEvent:FireClient(player, { { "setActive", true } })

	if #PlayerManager.ActivePlayers >= GameSettings.minimumPlayers then
		onMiminumActivePlayersEvent:Fire()
	end
end

local function removeActivePlayer(player)
	CheckpointEvent:FireClient(player, { { "setActive", false } })

	for playerKey, whichPlayer in pairs(PlayerManager.ActivePlayers) do
		if whichPlayer == player then
			table.remove(PlayerManager.ActivePlayers, playerKey)
			checkPlayerCount()
			return
		end
	end
end
