Loops are used when you want to do the same thing over and over again, for example showing a message to all players in one virtual world. This would look like this:
pawn Код:
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i) && GetPlayerVirtualWorld(i) == 1337)
{
SendClientMessage(i, 0xFF00FFFF, "Message shown to all players in virtual world 1337.");
}
}
For sure you can add more things there.
i represents playerid, but as we prefer using i in loops, we also use it here.
Another example could be, that you want to check if a player is at a specific place, and if that's the case and the player is not RCON admin, then kill the player:
pawn Код:
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i))
{
if(IsPlayerInRangeOfPoint(i, 15.0, 1234.0, 1234.0, 10.0) && !IsPlayerAdmin(i))
{
SendClientMessage(i, 0xFF00FFFF, "Only Admins are allowed to come here.");
SetPlayerHealth(i, 0.0);
}
}
}
For sure you can do a lot more with loops, those are just two easy examples.