How to count players using a particular cmd? - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: How to count players using a particular cmd? (
/showthread.php?tid=564074)
How to count players using a particular cmd? -
Nabster - 19.02.2015
Like there are players using /spec,i want to see how many of them are speccing with names
Re: How to count players using a particular cmd? -
CalvinC - 19.02.2015
You can start by creating a variable:
pawn Код:
new Spectating[MAX_PLAYERS]; // Creates a variable for MAX_PLAYERS(500)
Then in your /spec command, set it to 1:
pawn Код:
Spectating[playerid] = 1; // Sets "Specating" to 1 for the player
And set it to 0 when they stop spectating.
Then where-ever you want to show their names, loop through all players, and check if it's set to 1, if so, get their name and show it with strcat.
Etc. in a message:
pawn Код:
new string[128], name[MAX_PLAYER_NAME], spectators; // Creates 2 strings, "string" and "name", and a "spectators" variable
format(string, sizeof(string), "Players spectating:"); // Formats "string", making it contain: "Players spectating:"
foreach(Player, i) // Loops through all players, and defines them as "i"
{
if(Spectating[playerid] == 1) // Checks if "Spectating" is set to 1
{
GetPlayerName(i, name, sizeof(name)); // Get's the name of "i"
strcat(string, " %s", name); // Add's on the name to the string
spectators ++; // Increases "spectators" by 1
}
}
switch(spectators) // Creates a switch, checking the value of "spectators"
{
case 0: SendClientMessage(playerid, -1, "No players are currently spectating anyone.");
// If "spectators" hasn't been increased (therefore it's value is 0) it will send this message instead
default: SendClientMessage(playerid, -1, string);
// If it's set to something else than 0 (players are spectating), it will send the original string
}
Re: How to count players using a particular cmd? -
Vince - 19.02.2015
Don't even need the variable. Use GetPlayerState with PLAYER_STATE_SPECTATING. However, if you're using foreach you might as well create a custom iterator which you can put all the spectators into.