Give Score to Whole Team
#1

Hello,

I am using SetPlayerTeam and GetPlayerTeam.
I want to give 5 Score to the whole Team.

I got two teams defined :-
Код:
#define TEAM_SOLDIER        1
#define TEAM_TERRORIST    2
Anyone can help me?
Reply
#2

pawn Код:
for (new i = 0; i != MAX_PLAYERS; i++)
{
    if (GetPlayerTeam(i) == [Team id])
    {
        SetPlayerScore(i, 5);
    }
}
Reply
#3

pawn Код:
stock GiveTeamScore(teamid, score)
{
    for(new i = 0; i < MAX_PLAYERS; i++) {
        if(GetPlayerTeam(i) == teamid) {
            SetPlayerScore(i, GetPlayerScore(i) + score);
        }
    }
    return 1;
}

//Usage:
GiveTeamScore(TEAM_SOLDIER, 5);
Reply
#4

Even better code,

For Soilder
pawn Код:
foreach(Player, i) {
if (GetPlayerTeam(i) == TEAM_SOILDER))
{
    SetPlayerScore(i, 5);
}
}
For terrorists just change TEAM_SOILDER to TEAM_TERRORIST. And I'd like to point out that your code is very inefficent because you don't even check if the player is connected. What if we only have ten people connected on a 500 slot server? You're looping through 490 more slots than you need to.
Reply
#5

Код:
foreach(Player, i) {
if (GetPlayerTeam(i) == TEAM_SOILDER))
{
SetPlayerScore(i, 5);
}
}
It's isn't a good one, if one of the terrorists had 50 score, his score will be reset to 5.
Reply
#6

Quote:
Originally Posted by Abagail
Посмотреть сообщение
Even better code,

For Soilder
pawn Код:
foreach(Player, i) {
if (GetPlayerTeam(i) == TEAM_SOILDER))
{
    SetPlayerScore(i, 5);
}
}
For terrorists just change TEAM_SOILDER to TEAM_TERRORIST. And I'd like to point out that your code is very inefficent because you don't even check if the player is connected. What if we only have ten people connected on a 500 slot server? You're looping through 490 more slots than you need to.
Your code won't work as it exactly won't give "score", it will reset the score of all the members of the team.
You should do:

PHP код:
SetPlayerScore(iGetPlayerScore(i)+5); 
Here's fully working code:
PHP код:
for (new 0MAX_PLAYERSi++)
{
    if(
IsPlayerConnected(i))//Always check if player is connected
    
{
        if (
GetPlayerTeam(i) == TheDesiredTeam)//Change it to the desired one.
        
{
            
SetPlayerScore(iGetPlayerScore(i)+5);
        }
    }

Reply
#7

I know, Stinged's code confused me, just changed it before I saw your posts. I am shocked that TWO people didnt even check if the player's connected. Furthermore, I would recommend using foreach.
Reply
#8

Quote:
Originally Posted by Abagail
Посмотреть сообщение
I know, Stinged's code confused me, just changed it before I saw your posts. I am shocked that TWO people didnt even check if the player's connected. Furthermore, I would recommend using foreach.
I usually use foreach, which automatically checks if the player is connected, so that's why I sometimes forget to do that with the for loop.
Reply
#9

Use foreach loops (requires: https://sampforum.blast.hk/showthread.php?tid=92679).

pawn Код:
foreach (new i : Player) //Already checks if the player is connected, no need to check it by typing another line of code after the curly braces
{
    if(GetPlayerTeam(i) == TEAM_ID)
    {
        SetPlayerScore(i, GetPlayerScore(i) + 5);
    }
}
Reply
#10

Quote:
Originally Posted by [XST]O_x
Посмотреть сообщение
pawn Код:
stock GiveTeamScore(teamid, score)
{
    for(new i = 0; i < MAX_PLAYERS; i++) {
        if(GetPlayerTeam(i) == teamid) {
            SetPlayerScore(i, GetPlayerScore(i) + score);
        }
    }
    return 1;
}

//Usage:
GiveTeamScore(TEAM_SOLDIER, 5);
Worked!
Thanks alot man.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)