Give Score to Whole Team -
Phyzic - 15.08.2014
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?
Re: Give Score to Whole Team -
Stinged - 15.08.2014
pawn Код:
for (new i = 0; i != MAX_PLAYERS; i++)
{
if (GetPlayerTeam(i) == [Team id])
{
SetPlayerScore(i, 5);
}
}
Re: Give Score to Whole Team -
[XST]O_x - 15.08.2014
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);
Re: Give Score to Whole Team -
Abagail - 15.08.2014
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.
Re : Give Score to Whole Team -
Adawg - 15.08.2014
Код:
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.
Re: Give Score to Whole Team -
ThePhenix - 15.08.2014
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(i, GetPlayerScore(i)+5);
Here's fully working code:
PHP код:
for (new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i))//Always check if player is connected
{
if (GetPlayerTeam(i) == TheDesiredTeam)//Change it to the desired one.
{
SetPlayerScore(i, GetPlayerScore(i)+5);
}
}
}
Re: Give Score to Whole Team -
Abagail - 15.08.2014
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.
Re: Give Score to Whole Team -
Stinged - 15.08.2014
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.
Re: Give Score to Whole Team -
Twizted - 15.08.2014
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);
}
}
Re: Give Score to Whole Team -
Phyzic - 16.08.2014
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.