# Randomize array role for each player

**URL:** <https://forums.unrealengine.com/t/randomize-array-role-for-each-player/2139839>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, question\
**Created:** [November 21, 2024, 4:30am UTC](https://forums.unrealengine.com/t/randomize-array-role-for-each-player/2139839 "2024-11-21T04:30:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![h2oph7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@h2oph7](https://forums.unrealengine.com/u/h2oph7)\
**Post date:** [November 21, 2024, 4:30am UTC](https://forums.unrealengine.com/t/randomize-array-role-for-each-player/2139839/1 "2024-11-21T04:30:01Z")

</div>

Hello everyone, I am trying to make mini game with roles for example every game have 2 civ 1 doctor 1 police but i want to randomize the enum for 2 player have role(police , doctor) except 2 players have civ role but i don’t know how to do that i trying a lot but i got different result if anyone can help me I really appreciate it

I got players array like this

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

---

<div class="post-metadata">

**Author:** ![Arodi007](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arodi007/32/1022275_2.png) [@Arodi007](https://forums.unrealengine.com/u/Arodi007)\
**Post date:** [November 21, 2024, 5:16am UTC](https://forums.unrealengine.com/t/randomize-array-role-for-each-player/2139839/2 "2024-11-21T05:16:25Z")

</div>

You can use my example below, mine is assigning a profile pic to a player and am storing them in their player state.

**Player State BP**  
You can replace my player icon texture variable with your Enum Role.  
If you need that role to be persistent across switching map you will need to set it to their game instance as well.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/a/8/ea890e0e241c4fd0c5fad65d62bb299506133ee0.png)

**Game Mode BP**  
For the array, depending on your game style if a role can be assigned to only 1 player, you will want to make a remove element from array or something similar to prevent assigning duplicate role.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/a/e/6ae7db1e6f4118a65c7490dd675720fed558a957.jpeg)

---

<div class="post-metadata">

**Author:** ![h2oph7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@h2oph7](https://forums.unrealengine.com/u/h2oph7)\
**Post date:** [November 21, 2024, 6:36am UTC](https://forums.unrealengine.com/t/randomize-array-role-for-each-player/2139839/3 "2024-11-21T06:36:15Z")

</div>

I don’t know why i can’t cast

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/d/4/bd458451d22b51ba289c470e56464d4374c7c392.png)

---

<div class="post-metadata">

**Author:** ![Arodi007](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arodi007/32/1022275_2.png) [@Arodi007](https://forums.unrealengine.com/u/Arodi007)\
**Post date:** [November 21, 2024, 9:17am UTC](https://forums.unrealengine.com/t/randomize-array-role-for-each-player/2139839/4 "2024-11-21T09:17:08Z")

</div>

You will need to create a player state bp, these work similar to a controller bp but for multiplayer, since they exist in client and server where you will want to share the player data.

Create bp player state, open it and add your variable enum ERole from it.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/a/6/3a63161012a9438e3e42ec20334304278708fc3c.png)

Don’t forget to set that player state from your world setting game mode too:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/c/0/dc02ee1159edfb82ae27d164811c2a74d3c414e2.png)

You can then get the player state from the NewPlayer and cast to that player state bp.

---

<div class="post-metadata">

**Author:** ![h2oph7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@h2oph7](https://forums.unrealengine.com/u/h2oph7)\
**Post date:** [November 22, 2024, 5:20pm UTC](https://forums.unrealengine.com/t/randomize-array-role-for-each-player/2139839/5 "2024-11-22T17:20:55Z")

</div>

Thanks ! roles randomize works but i can’t understand how to avoid duplicate role assignment, because i want to set 1 role for each player

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/b/1/2b11bf4ad51788f85fd41234017f5058bd0462f8.png)

---

<div class="post-metadata">

**Author:** ![h2oph7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@h2oph7](https://forums.unrealengine.com/u/h2oph7)\
**Post date:** [November 25, 2024, 7:50pm UTC](https://forums.unrealengine.com/t/randomize-array-role-for-each-player/2139839/6 "2024-11-25T19:50:55Z")

</div>

I got the solution after I understand how array works  
First, I make player state blueprint. Then, I add player role variable for player state (BP\_Test. Next, I add Roles array into game and roles inside array. For example, mafia, civ, etc. Finally, In game mode i use on post login then cast to bp\_test and shuffle the array to randomize for each loop to set result of array to assign player role variable after assign variable it will be removed from array to avoid duplicate role assignment.

Player state :

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/e/c/1ec8d9ff1d2c67dc6fd64628fc4cf4c4e477f794.png)

Gamemode:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/a/6/0a68dc073a48cb5eab2bf3f962ecb898713cd088.png)

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [December 25, 2024, 7:51pm UTC](https://forums.unrealengine.com/t/randomize-array-role-for-each-player/2139839/7 "2024-12-25T19:51:34Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
