# AI Join Player Squad

> _This script will add a action to AI that will make it possible to add them to your just by looking at them and clicking the action._

First off create a folder in your mission folder called **`scripts`** now create a folder in there called **`AI_JoinSquad.sqf`** in there copy and paste this code:

{% code overflow="wrap" %}
```sqf
if (!isDedicated) then {

	[] spawn {

		while {true} do {
		
		_allAIUnits = allUnits select {_x distanceSqr getpos player < 900};
		
			if (count _allAiUnits > 0) then {{
				if( side player == side _x && alive _x && group _x != group player && leader player == player && _x isKindOf "Man" && vehicle _x == _x && (leader _x != _x || count (units group _x) == 1) ) then {
					if(isNil {_x getVariable "sa_join_squad_action_id"}) then {
						_actionId = _x addAction ["<t color='#FFFFFF' size='1.4'><img image='A3\ui_f\data\map\markers\military\start_CA.paa'></img><t color='#ffd700' size='1.4' font='puristaBold'>Leave My Squad</t>", { 
							if(isNil {(_this select 0) getVariable "sa_original_group"}) then {
								(_this select 0) setVariable ["sa_original_group",group (_this select 0)];
							};
							[_this select 0] join player; 
							(_this select 0) removeAction (_this select 2);
							(_this select 0) setVariable ["sa_join_squad_action_id",nil];
						}, nil, 0, false];
						_x setVariable ["sa_join_squad_action_id",_actionId];
					};
				} else {
					if(!isNil {_x getVariable "sa_join_squad_action_id"}) then {
						_x removeAction (_x getVariable "sa_join_squad_action_id");
						_x setVariable ["sa_join_squad_action_id",nil];
					};
				};
				
				if( side player == side _x && alive _x && group _x == group player && leader player == player && _x isKindOf "Man" && vehicle _x == _x && _x != player ) then {
					if(isNil {_x getVariable "sa_leave_squad_action_id"}) then {
						_actionId = _x addAction ["<t color='#FFFFFF' size='1.4'><img image='A3\ui_f\data\map\markers\military\objective_CA.paa'></img><t color='#ffd700' size='1.4' font='puristaBold'>Leave My Squad</t>", { 
							if(!isNil {(_this select 0) getVariable "sa_original_group"}) then {
								[_this select 0] join ((_this select 0) getVariable "sa_original_group"); 
								(_this select 0) setVariable ["sa_original_group",nil];
							} else {
								[_this select 0] join grpNull; 
							};
							(_this select 0) removeAction (_this select 2);
							(_this select 0) setVariable ["sa_leave_squad_action_id",nil];
						}, nil, 0, false];
						_x setVariable ["sa_leave_squad_action_id",_actionId];
					};
				} else {
					if(!isNil {_x getVariable "sa_leave_squad_action_id"}) then {
						_x removeAction (_x getVariable "sa_leave_squad_action_id");
						_x setVariable ["sa_leave_squad_action_id",nil];
					};
				};
				
			} forEach _allAiUnits;
			sleep 10;
			};

		};

	};

};
```
{% endcode %}

Now go to your **init.sqf** file and copy and paste this code in there:

```sqf
// AI JOIN PLAYER SQUAD
[] execVM "scripts\AI_JoinSquad.sqf";
```
