# How do I set random team selection based on player count? 

**URL:** <https://forums.unrealengine.com/t/how-do-i-set-random-team-selection-based-on-player-count/2376431>\
**Category:** Programming & Scripting\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, question, fortnite\
**Created:** [March 5, 2025, 3:56pm UTC](https://forums.unrealengine.com/t/how-do-i-set-random-team-selection-based-on-player-count/2376431 "2025-03-05T15:56:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ExoTesting26](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ExoTesting26](https://forums.unrealengine.com/u/ExoTesting26)\
**Post date:** [March 5, 2025, 3:56pm UTC](https://forums.unrealengine.com/t/how-do-i-set-random-team-selection-based-on-player-count/2376431/1 "2025-03-05T15:56:41Z")

</div>

So I’m running an infection style game mode where there are humans and zombies. Currently, my code only selects 1 random zombie (On Begin) no matter how many players there are. I want to make it where it selects 1 random zombie depending on my set conditions:  
1 random zombie if \<= 6  
2 random zombies if \>6 and \<= 10… etc.

 ![a8BFuS79](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/5/c/95ce5c89d868881b569fb8356775542ffbbfdfbf.png)  
My code before this screen shot only chooses 1. I’ve added in the “ZombiesToChoose” and the conditionals below it.

I think changing this helps but don’t know what to do after. I also don’t want duplicates of zombies, if it hits a dupiclate, reroll.

if (Players.Length \> 0):  
for (X := 0…ZombiesToChoose):  
RandomIndex := GetRandomInt(0, Players.Length -1)

Don’t know if thats correct or what to do next

---

<div class="post-metadata">

**Author:** ![im\_a\_lama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/im_a_lama/32/995063_2.png) [@im\_a\_lama](https://forums.unrealengine.com/u/im_a_lama)\
**Post date:** [March 5, 2025, 4:10pm UTC](https://forums.unrealengine.com/t/how-do-i-set-random-team-selection-based-on-player-count/2376431/2 "2025-03-05T16:10:42Z")

</div>

After you set ZombiesToChoose

```auto
RandomPlayers := Shuffle(Players)
for(I := 0..ZombiesToChoose-1, RandomPlayer := RandomPlayers[I]):
    # MakeZombie(RandomPlayer)

```

This should ensure there’s no duplicates

---

<div class="post-metadata">

**Author:** ![btwixer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@btwixer](https://forums.unrealengine.com/u/btwixer)\
**Post date:** [March 5, 2025, 4:16pm UTC](https://forums.unrealengine.com/t/how-do-i-set-random-team-selection-based-on-player-count/2376431/3 "2025-03-05T16:16:36Z")

</div>

The looks good to me, a few comments.

Instead of this - where it loops through all players looking for the matching index.

```auto
for (...):
   if (Player Index = RandomIndex):
     # Rest of code...

```

It’s more efficient and a little cleaner to do:

```auto
if:
  aPlayer := Players[RandomIndex]
then:
  # Rest of code...

```

My second comment is a bit trickier, about the following llne.

```auto
RandomIndex := GetRandomInt(0, Players.Length - 1)

```

This will correctly pick a random player, but it may pick the same player more than once.

You may want to pick three player numbers, but ensure they are all different numbers (unless picking same player more than once is valid for what you want to do).

There may be better ways, but what I’ve done in the past, is something like this.

```auto
# This picks the first player, just like the original code.
Index1 := GetRandomInt(0, Players.Length - 1)

# This picks from a range one smaller than the original range (may still be same index after this next line).
Index2 := GetRandomInt(0, Players.Length - 2)
# This ensures it's not the same index.
if (Index2 = Index1):
    set Index2 += 1

# Now we have two different random players.

# A third would look like this.
Index3:= GetRandomInt(0, Players.Length - 3)
if (Index3 = Index1):
    set Index3 += 1
if (Index3 = Index2):
    set Index3 += 1

```

I think this approach works.

Hope it helps.

---

<div class="post-metadata">

**Author:** ![btwixer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@btwixer](https://forums.unrealengine.com/u/btwixer)\
**Post date:** [March 5, 2025, 4:23pm UTC](https://forums.unrealengine.com/t/how-do-i-set-random-team-selection-based-on-player-count/2376431/4 "2025-03-05T16:23:30Z")

</div>

Replying to my own post 🙂

Another approach would be to make a duplicate Players array and remove each player as you randomly pick them. In some ways that would be cleaner, but it results in multiple times the code has to copy the array (a copy is made each time a remove is done), so I’ve usually gone with the approach I outlined above.

---

<div class="post-metadata">

**Author:** ![ExoTesting26](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ExoTesting26](https://forums.unrealengine.com/u/ExoTesting26)\
**Post date:** [March 6, 2025, 12:20am UTC](https://forums.unrealengine.com/t/how-do-i-set-random-team-selection-based-on-player-count/2376431/5 "2025-03-06T00:20:57Z")

</div>

Trying this out first, so I applied my code and whats happening is its applying the human traits and zombie traits but its not actually doing the teamclass swap for humans like it did before. Im not sure if its doing the swap for zombies since my UI counter is showing 1 zombie and 0 humans

 ![PXf92_gB](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/f/b/4fb1d253646bc456da2b2eab67e7e6d313237825.png)

---

<div class="post-metadata">

**Author:** ![btwixer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@btwixer](https://forums.unrealengine.com/u/btwixer)\
**Post date:** [March 6, 2025, 12:41am UTC](https://forums.unrealengine.com/t/how-do-i-set-random-team-selection-based-on-player-count/2376431/6 "2025-03-06T00:41:30Z")

</div>

A couple thoughts.

Minor typo on the first two “if” statements - for exactly 8 players ZombiesToChoose stays at 0. You probably want a \<= 8 on the first “if”.

Is PlayerMap initialized with 0’s for every entry before this code runs? If not, I don’t know if Verse defaults to 0, if so the code should be ok, if not they the following would never be true.

```auto
if (PlayerMap[Player] = 0):

```

On second thought, if that’s an actual “map” (not array) then it would need to be initialized I think.

I don’t see anything else.

---

<div class="post-metadata">

**Author:** ![ExoTesting26](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ExoTesting26](https://forums.unrealengine.com/u/ExoTesting26)\
**Post date:** [March 6, 2025, 12:44am UTC](https://forums.unrealengine.com/t/how-do-i-set-random-team-selection-based-on-player-count/2376431/7 "2025-03-06T00:44:26Z")

</div>

Awesome going to try this, also if you don’t mind, would you be able to write out your implementation using my code ? You don’t have to do the whole thing, just the part where its supposed to happen. Thanks in advance for the help!!!

Edit:

Yes, Playermap is initialized as a map. humans are set to playermap[player] = 0 and zombies are playermap[player] = 1

---

<div class="post-metadata">

**Author:** ![btwixer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@btwixer](https://forums.unrealengine.com/u/btwixer)\
**Post date:** [March 6, 2025, 12:49am UTC](https://forums.unrealengine.com/t/how-do-i-set-random-team-selection-based-on-player-count/2376431/8 "2025-03-06T00:49:06Z")

</div>

Do you mean this part?

```auto
for (Player : Players):
  if (PlayerMap[Player] = 0):
    # Rest of code...

```

If so, I think you can do it this way - have your code run if the map entry doesn’t exist. Might be able to do this with a “not” or something in the if itself, instead of the else.

```auto
for (Player : Players):
    if (PlayerMap[Player] = 0):
        # Probably do nothing
    else:
        # Put your code here

```

Something like that.

---

<div class="post-metadata">

**Author:** ![btwixer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@btwixer](https://forums.unrealengine.com/u/btwixer)\
**Post date:** [March 6, 2025, 12:51am UTC](https://forums.unrealengine.com/t/how-do-i-set-random-team-selection-based-on-player-count/2376431/9 "2025-03-06T00:51:09Z")

</div>

Seeing your edit above - if PlayerMap[Player] is initialized to 0 for humans prior to the code you show, then I don’t see an issue with your code.

When I’m uncertain what path code is taking, I add Print(“Something”) statements and look at what the code is doing.

---

<div class="post-metadata">

**Author:** ![ExoTesting26](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ExoTesting26](https://forums.unrealengine.com/u/ExoTesting26)\
**Post date:** [March 6, 2025, 5:36am UTC](https://forums.unrealengine.com/t/how-do-i-set-random-team-selection-based-on-player-count/2376431/10 "2025-03-06T05:36:45Z")

</div>

So this fixed it:

 ![ySa2ELbu](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/3/e/23ee80860795d079df1936b0236c524503a5105b.png)

---

<div class="post-metadata">

**Author:** ![btwixer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@btwixer](https://forums.unrealengine.com/u/btwixer)\
**Post date:** [March 6, 2025, 5:57am UTC](https://forums.unrealengine.com/t/how-do-i-set-random-team-selection-based-on-player-count/2376431/11 "2025-03-06T05:57:20Z")

</div>

Great!

---

<div class="post-metadata">

**Author:** ![im\_a\_lama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/im_a_lama/32/995063_2.png) [@im\_a\_lama](https://forums.unrealengine.com/u/im_a_lama)\
**Post date:** [March 6, 2025, 9:46am UTC](https://forums.unrealengine.com/t/how-do-i-set-random-team-selection-based-on-player-count/2376431/12 "2025-03-06T09:46:36Z")

</div>

You can debug by printing player names if you want [[Feature request] Get player name in Verse - #3 by bananna\_manuk](https://forums.unrealengine.com/t/feature-request-get-player-name-in-verse/1378109/3)
