MapVoteManager.cs

A component that runs a post-match map vote. Holds three map candidates, syncs vote counts to clients, takes votes through an RPC and loads the winning scene on the host.

Rough Code
🐞 FinishVoting loads the next map with Game.ActiveScene.LoadFromFile, which only changes the host's scene; connected clients stay on the old map.
using Sandbox;
using System;
using System.Collections.Generic;
using System.Linq;

public sealed class MapVoteManager : Component
{
	public static MapVoteManager Instance { get; private set; }

	public struct MapCandidate
	{
		public string Title;
		public string Subtitle;
		public string ScenePath;
		public string Icon;
		public string GameMode;
	}

	public static readonly List<MapCandidate> Candidates = new()
	{
		new MapCandidate
		{
			Title = "NEXTBOT EVADE",
			Subtitle = "Urban survival horde with downed & revive mechanics",
			ScenePath = "scenes/OneMoreS2.scene",
			Icon = "🏃",
			GameMode = "Survival Evade"
		},
		new MapCandidate
		{
			Title = "SUMO ARENA",
			Subtitle = "Physics pushing on shrinking dynamic platform tiles",
			ScenePath = "scenes/onemore.scene",
			Icon = "💥",
			GameMode = "Sumo Push"
		},
		new MapCandidate
		{
			Title = "DARK SURVIVAL",
			Subtitle = "Horror darkness survival requiring flashlights and parkour",
			ScenePath = "scenes/ScenceNextbot3.scene",
			Icon = "🔦",
			GameMode = "Dark Evade"
		}
	};

	[Sync] public bool IsVotingActive { get; private set; } = false;
	[Sync] public float TimeRemaining { get; private set; } = 12f;
	[Sync] public int Votes0 { get; private set; } = 0;
	[Sync] public int Votes1 { get; private set; } = 0;
	[Sync] public int Votes2 { get; private set; } = 0;

	public int LocalVotedIndex { get; private set; } = -1;
	private readonly Dictionary<Guid, int> _playerVotes = new();

	protected override void OnAwake()
	{
		Instance = this;
	}

	protected override void OnStart()
	{
		Instance = this;
	}

	public int GetVoteCount( int index ) => index switch
	{
		0 => Votes0,
		1 => Votes1,
		2 => Votes2,
		_ => 0
	};

	public void StartVoting( float duration = 12f )
	{
		if ( IsProxy ) return;

		IsVotingActive = true;
		TimeRemaining = duration;
		Votes0 = 0;
		Votes1 = 0;
		Votes2 = 0;
		_playerVotes.Clear();
		LocalVotedIndex = -1;

		if ( Killfeed.Instance != null )
		{
			Killfeed.Instance.AddEntry( "SYSTEM", "ALL PLAYERS", "NEXT MAP VOTE STARTED (12s)!", "🗳️" );
		}

		Log.Info( "[MapVoteManager] Post-match map voting started!" );
	}

	protected override void OnUpdate()
	{
		if ( !IsVotingActive ) return;

		if ( !IsProxy )
		{
			TimeRemaining -= Time.Delta;
			if ( TimeRemaining <= 0f )
			{
				FinishVoting();
			}
		}
	}

	public void CastVote( int candidateIndex )
	{
		if ( !IsVotingActive || candidateIndex < 0 || candidateIndex >= Candidates.Count ) return;
		LocalVotedIndex = candidateIndex;
		SubmitVoteRpc( Connection.Local?.Id ?? Guid.NewGuid(), candidateIndex );
	}

	[Rpc.Host]
	private void SubmitVoteRpc( Guid playerId, int candidateIndex )
	{
		if ( !IsVotingActive ) return;

		_playerVotes[playerId] = candidateIndex;
		TallyVotes();
	}

	private void TallyVotes()
	{
		int v0 = 0, v1 = 0, v2 = 0;
		foreach ( var kvp in _playerVotes )
		{
			if ( kvp.Value == 0 ) v0++;
			else if ( kvp.Value == 1 ) v1++;
			else if ( kvp.Value == 2 ) v2++;
		}
		Votes0 = v0;
		Votes1 = v1;
		Votes2 = v2;
	}

	private void FinishVoting()
	{
		IsVotingActive = false;

		// Select winner: highest votes, fallback random
		int bestIndex = 0;
		int maxVotes = Votes0;

		if ( Votes1 > maxVotes )
		{
			maxVotes = Votes1;
			bestIndex = 1;
		}
		if ( Votes2 > maxVotes )
		{
			maxVotes = Votes2;
			bestIndex = 2;
		}

		if ( maxVotes == 0 )
		{
			bestIndex = Game.Random.Int( 0, Candidates.Count - 1 );
		}

		var winner = Candidates[bestIndex];
		Log.Info( $"[MapVoteManager] Voting ended! Winner: {winner.Title} with {maxVotes} votes. Loading {winner.ScenePath}..." );

		if ( Killfeed.Instance != null )
		{
			Killfeed.Instance.AddEntry( "WINNER", winner.Title, $"NEXT MAP: {winner.Title}!", "🏆" );
		}

		BroadcastMapWon( winner.Title, winner.ScenePath );

		Game.ActiveScene.LoadFromFile( winner.ScenePath );
	}

	[Rpc.Broadcast]
	private void BroadcastMapWon( string title, string path )
	{
		Sound.Play( "music/buttonhover.sound" );
	}
}