Force player into spectate mode on death?
Good day,
I can not speak English very well. I'm learning to make plugins but I don't know how to edit that plugin. So that if someone dies from team A or B so that it throws him into the spectate mode. I will be happy for advice or adjustment and some explanation. Thank you very much.
#region Hooks
void OnServerInitialized() {
	LoadVariables();
	if (!CheckDependencies()) return;
	if (!CheckSpawnfiles()) return;
	UseTB = true;
	TeamA_Score = 0;
	TeamB_Score = 0;
	foreach(var player in BasePlayer.activePlayerList)
	OnPlayerInit(player);
}
private void OnEntityTakeDamage(BasePlayer victim, HitInfo hitInfo) {
	if (victim == null || hitInfo == null) return;

	if (UseTB) {
		if (hitInfo.InitiatorPlayer) {
			var attacker = hitInfo.InitiatorPlayer;
			if (victim != attacker) {
				if (victim.GetComponent < TBPlayer > () && attacker.GetComponent < TBPlayer > ()) {
					if (victim.GetComponent < TBPlayer > ().team == attacker.GetComponent < TBPlayer > ().team) {
						hitInfo.damageTypes.ScaleAll(configData.Options.FF_DamageScale);
						SendReply(hitInfo.Initiator as BasePlayer, "Friendly Fire!");
					}
				}
			}
		}
	}
}

private void OnEntityDeath(BasePlayer victim, HitInfo hitInfo) {
	if (victim == null || hitInfo == null) return;

	if (UseTB) {
		if (hitInfo.InitiatorPlayer != null) {
			BasePlayer attacker = hitInfo.InitiatorPlayer;
			if (victim != attacker) {
				if (victim.GetComponent < TBPlayer > () && attacker.GetComponent < TBPlayer > ()) {
					if (victim.GetComponent < TBPlayer > ().team != attacker.GetComponent < TBPlayer > ().team) {
						attacker.GetComponent < TBPlayer > ().kills++;
						AddPoints(attacker, victim);
					}
				}
			}
		}
	}
}
private void RefreshScoreboard() {
	foreach(var player in BasePlayer.activePlayerList) {
		Scoreboard(player);
	}
}
private void OnPlayerInit(BasePlayer player) {
	if (UseTB) {
		if (player.IsSleeping() || player.HasPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot)) {
			timer.Once(1, () = >{
				player.EndSleeping();
				OnPlayerInit(player);
			});
		}
		else InitPlayer(player);
	}
}
private void InitPlayer(BasePlayer player) {
	if (!player.GetComponent < TBPlayer > ()) {
		TBPlayers.Add(player.gameObject.AddComponent < TBPlayer > ());
		Scoreboard(player);
		if (DCPlayers.ContainsKey(player.userID)) {
			player.GetComponent < TBPlayer > ().kills = DCPlayers[player.userID].kills;
			player.GetComponent < TBPlayer > ().team = DCPlayers[player.userID].team;
			DCPlayers.Remove(player.userID);
			DCTimers[player.userID].Destroy();
			DCTimers.Remove(player.userID);
			player.DieInstantly();
			player.Respawn();
		}
		else OpenTeamSelection(player);
	}
}
private void OnPlayerDisconnected(BasePlayer player) {
	if (UseTB) {
		CuiHelper.DestroyUi(player, UIMain);
		CuiHelper.DestroyUi(player, UIScoreboard);

		TBPlayer tbPlayer = player.GetComponent < TBPlayer > ();
		if (tbPlayer != null) {
			DCPlayers.Add(player.userID, new PlayerData {
				kills = tbPlayer.kills,
				team = tbPlayer.team
			});
			DCTimers.Add(player.userID, timer.Once(configData.Options.RemoveSleeper_Timer * 60, () = >{
				DCPlayers.Remove(player.userID);
				DCTimers[player.userID].Destroy();
				DCTimers.Remove(player.userID);
			}));

			if (TBPlayers.Contains(tbPlayer)) TBPlayers.Remove(tbPlayer);
			UnityEngine.Object.Destroy(tbPlayer);
		}
	}
}
private void OnPlayerRespawned(BasePlayer player) {
	if (UseTB) {
		if (player.GetComponent < TBPlayer > ()) {
			Team team = player.GetComponent < TBPlayer > ().team;
			player.inventory.Strip();
			if (team != Team.SPECTATOR) {
				GivePlayerWeapons(player);
				GivePlayerGear(player, team);

				object newpos = null;

				if (team == Team.A) newpos = Spawns.Call("GetRandomSpawn", new object[] {
					configData.TeamA.Spawnfile
				});
				else if (team == Team.B) newpos = Spawns.Call("GetRandomSpawn", new object[] {
					configData.TeamB.Spawnfile
				});
				else if (team == Team.ADMIN && !string.IsNullOrEmpty(configData.Admin.Spawnfile)) newpos = Spawns.Call("GetRandomSpawn", new object[] {
					configData.Admin.Spawnfile
				});

				if (newpos is Vector3) MovePlayerPosition(player, (Vector3) newpos);
			}
		}
		else OnPlayerInit(player);
	}
}
private object OnPlayerChat(ConsoleSystem.Arg arg) {
	if (UseTB) {
		if (configData.Options.UsePluginChatControl) {
			BasePlayer player = (BasePlayer) arg.Connection.player;
			string message = arg.GetString(0, "text");
			string color = configData.Spectators.Chat_Color + configData.Spectators.Chat_Prefix;
			if (player.GetComponent < TBPlayer > ()) {
				switch (player.GetComponent < TBPlayer > ().team) {
				case Team.A:
					color = configData.TeamA.Chat_Color + configData.TeamA.Chat_Prefix;
					break;
				case Team.B:
					color = configData.TeamB.Chat_Color + configData.TeamB.Chat_Prefix;
					break;
				case Team.ADMIN:
					color = configData.Admin.Chat_Color + configData.Admin.Chat_Prefix;
					break;
				}
			}
			string formatMsg = $ "{color} {player.displayName}</color> : {message}";
			PrintToChat(formatMsg);
			return false;
		}
	}
	return null;
}
void Unload() {
	foreach(var p in BasePlayer.activePlayerList)
	OnPlayerDisconnected(p);

	var objects = UnityEngine.Object.FindObjectsOfType < TBPlayer > ();
	if (objects != null) foreach(var gameObj in objects) UnityEngine.Object.Destroy(gameObj);

	TBPlayers.Clear();
	DCPlayers.Clear();
	DCTimers.Clear();
}#endregion

#region Functions
private bool CheckDependencies() {
	if (Spawns == null) {
		PrintWarning($ "Spawns Database could not be found!");
		return false;
	}
	return true;
}
private bool CheckSpawnfiles() {
	object successA = Spawns.Call("GetSpawnsCount", configData.TeamA.Spawnfile);
	object successB = Spawns.Call("GetSpawnsCount", configData.TeamB.Spawnfile);
	object successAdmin = Spawns.Call("GetSpawnsCount", configData.Admin.Spawnfile);
	if (successA is string) {
		configData.TeamA.Spawnfile = null;
		Puts("Error finding the Team A spawn file");
		return false;
	}
	if (successB is string) {
		configData.TeamB.Spawnfile = null;
		Puts("Error finding the Team B spawn file");
		return false;
	}
	if (successAdmin is string) {
		configData.Admin.Spawnfile = null;
		SaveConfig(configData);
		Puts("Error finding the Admin spawn file, removing admin spawn points");
	}
	return true;
}
static void MovePlayerPosition(BasePlayer player, Vector3 destination) {
	try {
		if (player.isMounted) player.GetMounted().DismountPlayer(player, true);

		player.SetParent(null, true, true);

		player.StartSleeping();
		player.SetPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot, true);

		player.EnablePlayerCollider();
		player.RemovePlayerRigidbody();
		//player.UpdatePlayerCollider(true);
		//player.UpdatePlayerRigidbody(false);
		player.EnableServerFall(true);
		player.MovePosition(destination);
		player.ClientRPCPlayer(null, player, "ForcePositionTo", destination);

		player.UpdateNetworkGroup();
		player.SendNetworkUpdateImmediate(false);
		player.ClearEntityQueue(null);
		player.SendFullSnapshot();
	}
	finally {
		//player.UpdatePlayerCollider(true);
		//player.UpdatePlayerRigidbody(true);
		player.EnablePlayerCollider();
		player.AddPlayerRigidbody();
		player.EnableServerFall(false);
	}
}

private void StartSpectating(BasePlayer player, BasePlayer target) {
	if (!player.IsSpectating()) {
		player.SetPlayerFlag(BasePlayer.PlayerFlags.Spectating, true);
		player.gameObject.SetLayerRecursive(10);
		player.CancelInvoke("MetabolismUpdate");
		player.CancelInvoke("InventoryUpdate");
		player.ClearEntityQueue();
		player.SendEntitySnapshot(target);
		player.gameObject.Identity();
		player.SetParent(target, 0);
	}
}
private void EndSpectating(BasePlayer player) {
	if (player.IsSpectating()) {
		player.SetParent(null, 0);
		player.SetPlayerFlag(BasePlayer.PlayerFlags.Spectating, false);
		player.gameObject.SetLayerRecursive(17);
		player.metabolism.Reset();
		player.InvokeRepeating("InventoryUpdate", 1f, 0.1f * UnityEngine.Random.Range(0.99f, 1.01f));
	}
}
private void AddPoints(BasePlayer player, BasePlayer victim) {
	string colorAttacker = "";
	string colorVictim = "";
	string prefixAttacker = "";
	string prefixVictim = "";
	switch (player.GetComponent < TBPlayer > ().team) {
	case Team.NONE:
		return;
	case Team.A:
		TeamA_Score++;
		colorAttacker = configData.TeamA.Chat_Color;
		prefixAttacker = configData.TeamA.Chat_Prefix;
		break;
	case Team.B:
		TeamB_Score++;
		colorAttacker = configData.TeamB.Chat_Color;
		prefixAttacker = configData.TeamB.Chat_Prefix;
		break;
	case Team.ADMIN:
		colorAttacker = configData.Admin.Chat_Color;
		prefixAttacker = configData.Admin.Chat_Prefix;
		return;
	case Team.SPECTATOR:
		return;
	}
	switch (victim.GetComponent < TBPlayer > ().team) {
	case Team.NONE:
		return;
	case Team.A:
		colorVictim = configData.TeamA.Chat_Color;
		prefixVictim = configData.TeamA.Chat_Prefix;
		break;
	case Team.B:
		colorVictim = configData.TeamB.Chat_Color;
		prefixVictim = configData.TeamB.Chat_Prefix;
		break;
	case Team.SPECTATOR:
		return;
	case Team.ADMIN:
		colorVictim = configData.Admin.Chat_Color;
		prefixVictim = configData.Admin.Chat_Prefix;
		break;
	}
	RefreshScoreboard();
	if (configData.Options.BroadcastDeath) {
		string formatMsg = colorAttacker + player.displayName + "</color> has killed " + colorVictim + victim.displayName + "</color>";
		PrintToChat(formatMsg);
	}
}#endregion​

If you could put this into a source code block that would make it much easier to read so much easier to help you

void Test()
{
    Puts("test");
}

like that

        #region Hooks       
        void OnServerInitialized()
        {
            LoadVariables();
            if (!CheckDependencies()) return;
            if (!CheckSpawnfiles()) return;
            UseTB = true;
            TeamA_Score = 0;
            TeamB_Score = 0;
            foreach (var player in BasePlayer.activePlayerList)
                OnPlayerInit(player);
        }
        private void OnEntityTakeDamage(BasePlayer victim, HitInfo hitInfo)
        {
            if (victim == null || hitInfo == null)
                return;

            if (UseTB)
            {
                if (hitInfo.InitiatorPlayer)
                {
                    var attacker = hitInfo.InitiatorPlayer;
                    if (victim != attacker)
                    {
                        if (victim.GetComponent<TBPlayer>() && attacker.GetComponent<TBPlayer>())
                        {
                            if (victim.GetComponent<TBPlayer>().team == attacker.GetComponent<TBPlayer>().team)
                            {
                                hitInfo.damageTypes.ScaleAll(configData.Options.FF_DamageScale);
                                SendReply(hitInfo.Initiator as BasePlayer, "Friendly Fire!");
                            }
                        }
                    }
                }
            }
        }

        private void OnEntityDeath(BasePlayer victim, HitInfo hitInfo)
        {
            if (victim == null || hitInfo == null)
                return;

            if (UseTB)
            {
                if (hitInfo.InitiatorPlayer != null)
                {
                    BasePlayer attacker = hitInfo.InitiatorPlayer;
                    if (victim != attacker)
                    {
                        if (victim.GetComponent<TBPlayer>() && attacker.GetComponent<TBPlayer>())
                        {
                            if (victim.GetComponent<TBPlayer>().team != attacker.GetComponent<TBPlayer>().team)
                            {
                                attacker.GetComponent<TBPlayer>().kills++;
                                AddPoints(attacker, victim);
                            }
                        }
                    }
                }
            }
        }       
        private void RefreshScoreboard()
        {
            foreach(var player in BasePlayer.activePlayerList)
            {
                Scoreboard(player);
            }
        }
        private void OnPlayerInit(BasePlayer player)
        {
            if (UseTB)
            {
                if (player.IsSleeping() || player.HasPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot))
                {
                    timer.Once(1, () =>
                    {
                        player.EndSleeping();
                        OnPlayerInit(player);
                    });
                }
                else InitPlayer(player);               
            }
        }  
        private void InitPlayer(BasePlayer player)
        {
            if (!player.GetComponent<TBPlayer>())
            {
                TBPlayers.Add(player.gameObject.AddComponent<TBPlayer>());
                Scoreboard(player);
                if (DCPlayers.ContainsKey(player.userID))
                {
                    player.GetComponent<TBPlayer>().kills = DCPlayers[player.userID].kills;
                    player.GetComponent<TBPlayer>().team = DCPlayers[player.userID].team;
                    DCPlayers.Remove(player.userID);
                    DCTimers[player.userID].Destroy();
                    DCTimers.Remove(player.userID);
                    player.DieInstantly();
                    player.Respawn();
                }
                else OpenTeamSelection(player);
            }            
        }   
        private void OnPlayerDisconnected(BasePlayer player)
        {
            if (UseTB)
            {
                CuiHelper.DestroyUi(player, UIMain);
                CuiHelper.DestroyUi(player, UIScoreboard);

                TBPlayer tbPlayer = player.GetComponent<TBPlayer>();
                if (tbPlayer != null)
                {
                    DCPlayers.Add(player.userID, new PlayerData { kills = tbPlayer.kills, team = tbPlayer.team });
                    DCTimers.Add(player.userID, timer.Once(configData.Options.RemoveSleeper_Timer * 60, () => { DCPlayers.Remove(player.userID); DCTimers[player.userID].Destroy(); DCTimers.Remove(player.userID); }));
                                
                    if (TBPlayers.Contains(tbPlayer))
                        TBPlayers.Remove(tbPlayer);
                    UnityEngine.Object.Destroy(tbPlayer);
                }                
            }
        }      
        private void OnPlayerRespawned(BasePlayer player) 
        {
            if (UseTB)
            {
                if (player.GetComponent<TBPlayer>())
                {
                    Team team = player.GetComponent<TBPlayer>().team;
                    player.inventory.Strip();
                    if (team != Team.SPECTATOR)
                    {
                        GivePlayerWeapons(player);
                        GivePlayerGear(player, team);

                        object newpos = null;

                        if (team == Team.A) newpos = Spawns.Call("GetRandomSpawn", new object[] { configData.TeamA.Spawnfile });
                        else if (team == Team.B) newpos = Spawns.Call("GetRandomSpawn", new object[] { configData.TeamB.Spawnfile });
                        else if (team == Team.ADMIN && !string.IsNullOrEmpty(configData.Admin.Spawnfile)) newpos = Spawns.Call("GetRandomSpawn", new object[] { configData.Admin.Spawnfile });

                        if (newpos is Vector3)
                            MovePlayerPosition(player, (Vector3)newpos);
                    }
                }
                else OnPlayerInit(player);
            }           
        }
        private object OnPlayerChat(ConsoleSystem.Arg arg)
        {
            if (UseTB)
            {
                if (configData.Options.UsePluginChatControl)
                {
                    BasePlayer player = (BasePlayer)arg.Connection.player;
                    string message = arg.GetString(0, "text");
                    string color = configData.Spectators.Chat_Color + configData.Spectators.Chat_Prefix;
                    if (player.GetComponent<TBPlayer>())
                    {
                        switch (player.GetComponent<TBPlayer>().team)
                        {
                            case Team.A:
                                color = configData.TeamA.Chat_Color + configData.TeamA.Chat_Prefix;
                                break;
                            case Team.B:
                                color = configData.TeamB.Chat_Color + configData.TeamB.Chat_Prefix;
                                break;
                            case Team.ADMIN:
                                color = configData.Admin.Chat_Color + configData.Admin.Chat_Prefix;
                                break;
                        }
                    }
                    string formatMsg = $"{color} {player.displayName}</color> : {message}";
                    PrintToChat(formatMsg);
                    return false;
                }
            }
            return null;
        }
        void Unload()
        {
            foreach (var p in BasePlayer.activePlayerList)
                OnPlayerDisconnected(p);

            var objects = UnityEngine.Object.FindObjectsOfType<TBPlayer>();
            if (objects != null)
                foreach (var gameObj in objects)
                    UnityEngine.Object.Destroy(gameObj);

            TBPlayers.Clear();
            DCPlayers.Clear();
            DCTimers.Clear();
        }
        #endregion

        #region Functions
        private bool CheckDependencies()
        {
            if (Spawns == null)
            {
                PrintWarning($"Spawns Database could not be found!");
                return false;
            }            
            return true;
        }
        private bool CheckSpawnfiles()
        {
            object successA = Spawns.Call("GetSpawnsCount", configData.TeamA.Spawnfile);
            object successB = Spawns.Call("GetSpawnsCount", configData.TeamB.Spawnfile);
            object successAdmin = Spawns.Call("GetSpawnsCount", configData.Admin.Spawnfile);
            if (successA is string)
            {
                configData.TeamA.Spawnfile = null;
                Puts("Error finding the Team A spawn file");
                return false;
            }
            if (successB is string)
            {
                configData.TeamB.Spawnfile = null;
                Puts("Error finding the Team B spawn file");
                return false;
            }
            if (successAdmin is string)
            {
                configData.Admin.Spawnfile = null;
                SaveConfig(configData);
                Puts("Error finding the Admin spawn file, removing admin spawn points");                
            }
            return true;
        }
        static void MovePlayerPosition(BasePlayer player, Vector3 destination)
        {
            try
            {
                if (player.isMounted)
                    player.GetMounted().DismountPlayer(player, true);

                player.SetParent(null, true, true);

                player.StartSleeping();
                player.SetPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot, true);

                player.EnablePlayerCollider();
                player.RemovePlayerRigidbody();
                //player.UpdatePlayerCollider(true);
                //player.UpdatePlayerRigidbody(false);
                player.EnableServerFall(true);
                player.MovePosition(destination);
                player.ClientRPCPlayer(null, player, "ForcePositionTo", destination);

                player.UpdateNetworkGroup();
                player.SendNetworkUpdateImmediate(false);
                player.ClearEntityQueue(null);
                player.SendFullSnapshot();
            }
            finally
            {
                //player.UpdatePlayerCollider(true);
                //player.UpdatePlayerRigidbody(true);
                player.EnablePlayerCollider();
                player.AddPlayerRigidbody();
                player.EnableServerFall(false);
            }
        }
        
        private void StartSpectating(BasePlayer player, BasePlayer target)
        {
            if (!player.IsSpectating())
            {
                player.SetPlayerFlag(BasePlayer.PlayerFlags.Spectating, true);
                player.gameObject.SetLayerRecursive(10);
                player.CancelInvoke("MetabolismUpdate");
                player.CancelInvoke("InventoryUpdate");
                player.ClearEntityQueue();
                player.SendEntitySnapshot(target);
                player.gameObject.Identity();
                player.SetParent(target, 0);
            }
        }
        private void EndSpectating(BasePlayer player)
        {
            if (player.IsSpectating())
            {
                player.SetParent(null, 0);
                player.SetPlayerFlag(BasePlayer.PlayerFlags.Spectating, false);
                player.gameObject.SetLayerRecursive(17);
                player.metabolism.Reset();
                player.InvokeRepeating("InventoryUpdate", 1f, 0.1f * UnityEngine.Random.Range(0.99f, 1.01f));
            }
        }       
        private void AddPoints(BasePlayer player, BasePlayer victim)
        {
            string colorAttacker = "";
            string colorVictim = "";
            string prefixAttacker = "";
            string prefixVictim = "";
            switch (player.GetComponent<TBPlayer>().team)
            {
                case Team.NONE:
                    return;
                case Team.A:
                    TeamA_Score++;
                    colorAttacker = configData.TeamA.Chat_Color; 
                    prefixAttacker = configData.TeamA.Chat_Prefix;                    
                    break;
                case Team.B:
                    TeamB_Score++;
                    colorAttacker = configData.TeamB.Chat_Color;                    
                    prefixAttacker = configData.TeamB.Chat_Prefix;                    
                    break;
                case Team.ADMIN:
                    colorAttacker = configData.Admin.Chat_Color;
                    prefixAttacker = configData.Admin.Chat_Prefix;
                    return;
                case Team.SPECTATOR:
                    return;
            }
            switch (victim.GetComponent<TBPlayer>().team)
            {
                case Team.NONE:
                    return;
                case Team.A:
                    colorVictim = configData.TeamA.Chat_Color;
                    prefixVictim = configData.TeamA.Chat_Prefix;
                    break;
                case Team.B:
                    colorVictim = configData.TeamB.Chat_Color;
                    prefixVictim = configData.TeamB.Chat_Prefix;
                    break;
                case Team.SPECTATOR:
                    return;
                case Team.ADMIN:
                    colorVictim = configData.Admin.Chat_Color;
                    prefixVictim = configData.Admin.Chat_Prefix;
                    break;               
            }
            RefreshScoreboard();
            if (configData.Options.BroadcastDeath)
            {
                string formatMsg = colorAttacker + player.displayName + "</color> has killed " + colorVictim + victim.displayName + "</color>";
                PrintToChat(formatMsg);
            }
        }
        #endregion
OnPlayerInit

was changed to OnPlayerConnected

And will it help me with the fact that when I kill a player in a given A / B team, it will move him to spectate mode?
If its wrong when they connect it will not run
InitPlayer(BasePlayer player)​