Jump to content
Sign in to follow this  
Chris_37

Get All players with certain variable in to a array?

Recommended Posts

Hey everyone, 

I am trying to get all the players in a mission with a certain variable into an array and when their is already 5 players in the array it will stop the player from having a gun.

So far I have found this and attempted to use it.

player setVariable ["hasweapon",true, true];

 

   playerArray = [];
 _players = player GetVariable "hasweapon";
  {
   if (!isNil "_players") then
   {
     playerArray PushBack _x;
   };
  } forEach allPlayers;
 hint format ["%1 players have the variable 'hasweapon'.",(count playerArray)];

 

 _result = count playerArray;
 if (_result >= 2) then {
  removeAllWeapons player;
 };

 

however this didn't completely work I was testing earlier and it wasn't working as expected, am I doing something wrong, or even going about it wrong?

Share this post


Link to post
Share on other sites
45 minutes ago, Chris_37 said:

Hey everyone, 

I am trying to get all the players in a mission with a certain variable into an array and when their is already 5 players in the array it will stop the player from having a gun.

So far I have found this and attempted to use it.

player setVariable ["hasweapon",true, true];

 

   playerArray = [];
 _players = player GetVariable "hasweapon";
  {
   if (!isNil "_players") then
   {
     playerArray PushBack _x;
   };
  } forEach allPlayers;
 hint format ["%1 players have the variable 'hasweapon'.",(count playerArray)];

 

 _result = count playerArray;
 if (_result >= 2) then {
  removeAllWeapons player;
 };

 

however this didn't completely work I was testing earlier and it wasn't working as expected, am I doing something wrong, or even going about it wrong?

 

The command select can really simplify stuff here:

_playersWithWeapons = allPlayers select {_x getVariable ["hasweapon",false]};

If you only want the first 5 players of the array to be armed use this:

_playersWithWeapons = allPlayers select {_x getVariable ["hasweapon",false]};

reverse _playersWithWeapons;
_playersWithWeapons resize (count _playersWithWeapons - 5);//removes the first 5 players from the array
reverse _playersWithWeapons;

_playersWithWeapons apply {removeAllWeapons _x};//everyone else gets his weapons removed

Cheers

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×