GameMode/Rules/SwapTeams.cs

Game rule component that runs when the game leaves a state, iterates all player state objects and swaps each assigned player's team to the opposing team.

Networking
using Sandbox.Diagnostics;
using Sandbox.Events;

namespace KOTH;

public sealed class SwapTeams : Component,
	IGameEventHandler<LeaveStateEvent>
{
	void IGameEventHandler<LeaveStateEvent>.OnGameEvent(LeaveStateEvent eventArgs)
	{
		Assert.True(Networking.IsHost);

		foreach (var PlayerState in GameNetworkManager.PlayerStates)
		{
			if (!PlayerState.IsValid() || PlayerState.Team == Team.Unassigned)
			{
				continue;
			}

			PlayerState.Team = PlayerState.Team.GetOpponents();
		}
	}
}