How can I assign every connecting player to one of two teams, so that both teams are balanced?

Hi, I am working on a multiplayer shooter, and I have now started working on a team deathmatch game mode. I have created a script that should assign each connecting player to one of 2 teams. The problem is, that the first 2 players gets assigned to team A, but the second player should be assigned to team B. However, the third player that connects is getting assigned to team B, but then the next player again gets assigned to team A. So there will always be 1 or 2 players more in team A, and I don’t want that, but I can’t figure out why it does that. Here is my script, thanks in advance for your help.

    [Command]
    void CmdGetTeams(string _connectedPlayerNetID)
    {
        int teamA = 0;
        int teamB = 0;
        int playersOnline = 0;
        Player[] players = GameManager.GetAllPlayers();

        foreach (Player player in players)
        {
            playersOnline += 1;
            if(player.team == 1)
            {
                teamA += 1;
            }
            if(player.team == 2)
            {
                teamB += 1;
            }
        }
        if (teamA <= teamB)
        {
            RpcAssignTeam(_connectedPlayerNetID, 1);
        }

        if (teamA < teamB)
        {
            //gameObject.GetComponent<Player>().team = 1;
            RpcAssignTeam(_connectedPlayerNetID, 1);
            Debug.Log("TeamA=" + teamA + " teamB= " + teamB);
            
        }
        if(teamB < teamA)
        {
            //gameObject.GetComponent<Player>().team = 2;
            RpcAssignTeam(_connectedPlayerNetID, 2);
            Debug.Log("TeamA=" + teamA + " teamB= " + teamB);
        }
        if(playersOnline == 2)
        {
            CmdReAssignTeams();
        }
            
    }

    [ClientRpc]
    void RpcAssignTeam(string _connectedPlayerNetID, int _team)
    {
        
        Player _player = GameManager.GetPlayer(_connectedPlayerNetID);
        
        _player.team = _team;
    }

the Player script contains the information about the player like, the team and how many points hi has.
I actually used the same logic to find which team got the most win points at the end but it always ended up saying undecided,even when it was not. here is the part of the script where it checks which team got the most win points.

 private void CmdCheckForWinnerTeam()
    {
      
            int teamA_WP = 0;
            int teamB_WP = 0;
            Player[] players = GameManager.GetAllPlayers();

            foreach (Player player in players)
            {
                if (player.team == 1)
                {
                    player.winPoints += teamA_WP;
                }
                if (player.team == 2)
                {
                    player.winPoints += teamB_WP;
                }
            }

            if (teamA_WP < teamB_WP)
            {
                //teamB Won!
                teamWon = 2;
            Debug.Log("team B Won!");
        }
            else if (teamA_WP > teamB_WP)
            {
                //teamA Won
                teamWon = 1;
            Debug.Log("team A Won!");
            }
            else if (teamA_WP == teamB_WP)
            {
                teamWon = 0;
            Debug.Log("Undecided");
            }
         
    }

What does the “CmdReAssignTeams()” do in the first part?

For the second part, you are not adding value to teamA_wp, instead you’re adding points to player.winPoints.

I figured it out! so the problem was that the CmdGetTeams() got executed as many times has there where clients. because i did not disable the script on the remote clients. So what i did, was simply disable the script if it was not the local player, like the character controller scripts.