[Tutorial] Limiting players per team.
#1

Limiting Players Per Team
Hi,

I'm going to explain how to limit max players per team according to the number of them.

Credits: Y_Less - foreach

1. We need <a_samp> and <foreach> includes.

PHP Code:
#include <a_samp>
#include <foreach> 
2. We need already the teams defined and also the player classes added below OnGameModeInit - OnPlayerRequestClass - The variable to set the player team. in this case gTeam[MAX_PLAYERS];

PHP Code:
#define TEAM_A 0
#define TEAM_B 1
#define TEAM_C 2
new gTeam[MAX_PLAYERS];
public 
OnPlayerRequestClass(playeridclassid)
{
    switch(
classid)
    {
        case 
0:
        {
            
SendClientMessage(playerid, -1"TEAM A");
            
SetPlayerSkin(playerid0);
        }
        case 
1:
        {
            
SendClientMessage(playerid, -1"TEAM B");
            
SetPlayerSkin(playerid1);
        }
        case 
2:
        {
            
SendClientMessage(playerid, -1"TEAM C");
            
SetPlayerSkin(playerid2);
        }
    }
    
SetPlayerPos(playerid1958.37831343.157215.3746);
    
SetPlayerCameraPos(playerid1958.37831343.157215.3746);
    
SetPlayerCameraLookAt(playerid1958.37831343.157215.3746);
    return 
1;
}
public 
OnGameModeInit()
{
    
AddPlayerClass(0,1958.37831343.157215.3746,94.4754,0,0,0,0,0,0);
    
AddPlayerClass(1,1958.37831343.157215.3746,94.4754,0,0,0,0,0,0);
    
AddPlayerClass(2,1958.37831343.157215.3746,94.4754,30,500,29,200,1,1);
    return 
1;

3. We are going to create a stock that is going to return the number of players of the team indicated.
PHP Code:
stock GetTeamCount(teamid)//We are defining the name of the stock and the teamid.
{
   new 
count 0;//Setting the count variable to 0
   
foreach(new i:Player)//Looping through all the players
   
{
        if(
GetPlayerState(i) == PLAYER_STATE_NONE) continue;//If is in class selection continue.
        
if(gTeam[i] != teamid) continue;//Is not in teamid continue.
        
count++;//counting
    
}
    return 
count;//returning the number of players counted in the team.

4.OnPlayerRequestSpawn:

PHP Code:
public OnPlayerRequestSpawn(playerid)//Calling this function which gets called when a player tries to spawn
{
    new 
acount GetTeamCount(TEAM_A);//Placing the count of TEAM_A to acount
    
new bcount GetTeamCount(TEAM_B);//Placing the count of TEAM_B to bcount
    
new ccount GetTeamCount(TEAM_C);//Placing the count of TEAM_C to ccount
    
switch(gTeam[playerid])//disaggregating the cases of gTeam - we have 3 teams therefore 3 cases.
    
{
        case 
TEAM_A://TEAM_A case
        
{
            if(
acount bcount && ccount)//If the count of players in TEAM_A is greater than the count of TEAM_B and TEAM_C
            
{
                
SendClientMessage(playerid, -1"ERROR: Team A is full. Join another team.");//Sending the error message
                
return 0;//Can't spawn. - Must choose another team.
            
}
        }
        case 
TEAM_B://TEAM_B case
        
{
            if(
bcount acount && ccount )//If the count of players in TEAM_B is greater than the count of TEAM_A and TEAM_C
            
{
                
SendClientMessage(playerid, -1,"ERROR: Team B is full. Join another team.");//Sending the error message
                
return 0;//Can't spawn - Must choose another team.
            
}
        }
        case 
TEAM_C://TEAM_C case
        
{
            if(
ccount acount && bcount)//If the count of players in TEAM_C is greater than the count of TEAM_A and TEAM_B
            
{
                
SendClientMessage(playerid, -1"ERROR: Team C is full. Join another team.");//Sending the error message
                
return 0;//Can't spawn - Must choose another team
            
}
        }
    }
       return 
1;//Can spawn..

Thanks.
Reply
#2

Nice Job +REP
Reply
#3

Good tutorial
Reply
#4

This very helping me, Thanks a lot! +Rep
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)