There is a code
public void SetTeam(Player player) { foreach (Team team in Teams.ToArray()) { if (team.IsFull()) { continue; } else { if (!team.InCommand(player)) { team.AddPlayer(player); } break; } } } With this code, I fill in the teams with players, there are only two of them, 1 or 2 players, depending on the mode. So I distribute:
foreach (Player player in GetPlayers().ToArray()) { SetTeam(player); Prespawn(player); TeleportToTeamSpawn(player); } But it happens that the players distribute incorrectly, it happens that one player throws in different teams. Distribution is carried out in a timer.
Team.cs
public class Team { public string Name { get; set; } = "0"; public int MaxPlayers { get; set; } = 0; public bool IsEliminated { get; set; } = false; public Vector3 Spawn { get; set; } = Vector3.Zero; public Vector3 Portal { get; set; } = Vector3.Zero; public int Points { get; set; } = 0; public List<Player> Players = new List<Player>(); public Team(int maxplayers, string name) { MaxPlayers = maxplayers; Name = name; } public void AddPlayer(Player player) { if (IsFull()) return; if (!Players.Contains(player)) { Players.Add(player); } } public void RemovePlayer(Player player) { if (Players.Contains(player)) { Players.Remove(player); } } public bool IsFull() { if (Players.Count == MaxPlayers) { return true; } return false; } public void Reset() { IsEliminated = false; Points = 0; Players.Clear(); Players = null; Players = new List<Player>(); } public bool InCommand(Player player) { if (Players.Find((x) => x.Username.ToLower() == player.Username.ToLower()) is Player) { return true; } return false; } } There is a guess that this is due to different threads.