Useful Snippets
#1

As i see that the Post your interesting/weird... commands is flooded with not just commands,
but other snippets that go in different areas of the SA:MP Scripts, i decided to make this
topic with ******' approval for people to post there useful, weird, or interesting snippets that not just
go into the OnPlayerCommandText Area but other areas of the SA:MP scripts. Ill start it out.

Connect icon in DeathStats from ******
pawn Code:
public OnPlayerConnect(playerid)
{
   SendDeathMessage(playerid, INVALID_PLAYER_ID, 200);
   return 1;
}

Disconnect icon in DeathStats from ******
pawn Code:
public OnPlayerDisconnect(playerid)
{
  SendDeathMessage(INVALID_PLAYER_ID, playerid, 201);
}
Reply
#2

Admin Lock

pawn Code:
new aVehicle; //Above Main
pawn Code:
//gamemodeinit
aVehicle = AddStaticVehicle(598,2256.0903,2477.0115,10.5668,178.5022,0,1); //gamemodeinit
pawn Code:
public AdminLock(playerid) //Anywhere in script
{
 if(IsPlayerConnected(playerid))
 {
   if(IsPlayerAdmin(playerid))
   {
    SetVehicleParamsForPlayer(aVehicle, playerid,0,0);
   }
   else
   {
    SetVehicleParamsForPlayer(aVehicle, playerid,0,1);
   }
 }
}
pawn Code:
public OnPlayerSpawn(playerid)
{
   AdminLock(playerid);
   return 1;
}
pawn Code:
AddStaticVehicleEx(598,2256.0903,2477.0115,10.5668,178.5022,0,1,never);
Reply
#3

Well this is a small thing but i find it a bit more efficient,

instead of using for(new i=0;i<MAX_PLAYERS

I use this:

Code:
new maxplayers; //Global Variable

public OnPlayerConnect(playerid){
	if(playerid > maxplayers-1) maxplayers = playerid+1; //Getting the maximum playerid
}

public OnPlayerDisconnect(playerid){
	if(playerid == maxplayers+1) for(new i=0;i<maxplayers;i++) if(IsPlayerConnected(i)) maxplayers = i+1; //Getting the minimum playerid
}

for(new i=0;i<maxplayers;i++) //the new more efficient loop
Reply
#4

Quote:

That won't work, you're assuming that all players are consecutive, which is rarely the case, you could have 10 players max, but only players 1, 4, 7 and 9 connected, which would make the max thing 9, saving almost nothing, and you would still need to check for connection.

No I'm not assuming there consecutive, check my code again, i find the highest playerid according to OnPlayerConnect and make the max loop that, then when a player disconnects i reevaluate the highest playerid

since basically all MAX_PLAYERS loops try to loop according to playerids you only need the highest one, hence this is more efficient
Reply
#5

ok about the admin vehcile's
i added 13 admin vehicle's and i can open them when im not admin.. now i realized that i had to add this, before it would work:

Code:
AddStaticVehicleEx(425,2137.0598,1729.9246,20.5675,59.3952,0,1,never);
    AddStaticVehicleEx(522,2164.1125,1720.9868,20.1695,161.4324,0,1,never);
	AddStaticVehicleEx(522,2168.9771,1719.4484,19.9998,169.0915,0,1,never);
	AddStaticVehicleEx(522,2174.8657,1718.4332,20.1868,165.7749,0,1,never);
	AddStaticVehicleEx(522,2179.6699,1717.1212,19.9994,166.9654,0,1,never);
	AddStaticVehicleEx(444,2208.9463,1654.0419,20.0004,2.0738,0,1,never);
	AddStaticVehicleEx(556,2184.9636,1654.0623,20.0956,2.9973,0,1,never);
	AddStaticVehicleEx(425,2201.3828,1716.8575,34.2190,88.4603,0,1,never);
	AddStaticVehicleEx(520,2141.1396,1637.6151,20.1024,120.4169,0,1,never);
	AddStaticVehicleEx(541,2175.4431,1613.0690,20.0886,182.6934,0,1,never);
	AddStaticVehicleEx(411,2168.8147,1614.7198,20.0885,181.9470,0,1,never);
	AddStaticVehicleEx(451,2163.6968,1613.2709,20.0878,175.1219,0,1,never);
	AddStaticVehicleEx(447,2141.5942,1591.2502,31.6095,124.6433,0,1,never);
And get this error:

Code:
(19229) : error 021: symbol already defined: "AddStaticVehicleEx"
I searched thru the forums how i could fix this error but found nothing.

Greetz, Sneaky


Reply
#6

only 1 vehicle can be locked with the lock i posted
you need maxvehicles for all of them to be unlocked

Everybody needs sleep Code


pawn Code:
enum pInfo
{
    Sleeping,
    Awake,
    Tired
}
new Info[MAX_PLAYERS][pInfo];
pawn Code:
SetTimer("Motels",5000,1);
SetTimer("SleepTime",300000,1);
pawn Code:
public SleepTime()
{
    for(new i=0; i<MAX_PLAYERS; i++)
    {
      SendClientMessage(i,COLOR_GREY,"* You are starting to feel sleep *");
        SendClientMessage(i,COLOR_GREY,"* Go to the nearest /hotel and get a rest *");
        Info[i] [Tired] = 1;
        Info[i] [Awake] = 0;
        Info[i] [Sleeping] = 0;
    }
}
pawn Code:
public Motels()
{
    for(new i=0; i<MAX_PLAYERS; i++)
    {
      if(IsPlayerInCube(i,2236,-1076,1040,2245,-1066,1100))
      {
        if(Info[i][Tired] == 1)
        {
                GameTextForPlayer(i,"~w~Please wait while your energy is ~b~restored",3000,3);
                Info[i] [Tired] = 0;
                Info[i] [Sleeping] = 1;
                Info[i] [Awake] = 0;
                TogglePlayerControllable(i,0);
        }
        if(Info[i] [Awake] == 1)
        {
                GameTextForPlayer(i,"~w~You ~g~don't ~w~need to rest",2000,3);
        }
        if(Info[i] [Sleeping] == 1)
        {
                Info[i] [Awake] = 1;
                Info[i] [Tired] = 0;
                Info[i] [Sleeping] = 0;
                GameTextForPlayer(i,"~w~Rise and Shine",2000,3);
                TogglePlayerControllable(i,1);
        }
      }
      if(IsPlayerInCube(i,2202,-1070,1000,2211,-1078,1100))
      {
        if(Info[i][Tired] == 1)
        {
                GameTextForPlayer(i,"~w~Please wait while your energy is ~b~restored",3000,3);
                Info[i] [Tired] = 0;
                Info[i] [Sleeping] = 1;
                Info[i] [Awake] = 0;
                TogglePlayerControllable(i,0);
        }
        if(Info[i] [Awake] == 1)
        {
                GameTextForPlayer(i,"~w~You ~g~don't ~w~need to rest",2000,3);
        }
        if(Info[i] [Sleeping] == 1)
        {
                Info[i] [Awake] = 1;
                Info[i] [Tired] = 0;
                Info[i] [Sleeping] = 0;
                GameTextForPlayer(i,"~w~Rise and Shine",2000,3);
                TogglePlayerControllable(i,1);
        }
      }
    }
  return 1;
}
Reply
#7

Why not simply:

pawn Code:
if ( IsPlayerInCube(i,2236,-1076,1040,2245,-1066,1100) || IsPlayerInCube(i,2202,-1070,1000,2211,-1078,1100) )
So you can delete half of the timer func. Also check if they are connected before doing anything.
Reply
#8

I made it a long time ago so i didn't know about that then.
Reply
#9

ok pixels, how can i make all vehicles unlocked for admin and locked for normal players then?
i really dont know how
Reply
#10

Something like this

pawn Code:
#define MAX_VEHICLES 255 //amount of cars in script
new aVehicle[MAX_VEHICLES] //define of aVehicle
pawn Code:
new never = 1000000;
    aVehicle[1] = AddStaticVehicleEx(522,2164.1125,1720.9868,20.1695,161.4324,0,1,never);
    aVehicle[2] = AddStaticVehicleEx(522,2168.9771,1719.4484,19.9998,169.0915,0,1,never);
pawn Code:
public AdminLock(playerid) //Anywhere in script
{
  if(IsPlayerConnected(playerid))
  {
   if(IsPlayerAdmin(playerid))
   {
     SetVehicleParamsForPlayer(aVehicle[MAX_VEHICLES], playerid,0,0);
   }
   else
   {
     SetVehicleParamsForPlayer(aVehicle[MAX_VEHICLES], playerid,0,1);
   }
  }
}
Reply
#11

Red Alert!
Type: Admin Command.
Description: Kicks all connected players off the server, due to an emergency etc.
_________________________________________________
Global Variable (Top Of Your Script):
pawn Code:
new RedAlertTimer;
_________________________________________________
Function
pawn Code:
public KickAll()
{
    for (new i = 0; i < MAX_PLAYERS; i++)
    {
        if (IsPlayerConnected(i))
        {
            Kick(i);
        }
    }
}
_________________________________________________
The Command: (Under The OnPlayerCommandText Callback)
pawn Code:
if (strcmp(cmdtext, "/redalert", true)==0)
    {
      if(IsPlayerAdmin(playerid))
      {
        new string[256];
        new aname[256];
        GetPlayerName(playerid,aname,sizeof(aname));
        GameTextForAll("~r~Red Alert",7000,4);
        format(string,sizeof(string), "Admin: %s has called Red Alert",aname);
        SendClientMessageToAll(0xAA3333AA,string);
        SendClientMessageToAll(0xAA3333AA,"You will all be kicked in ten seconds.");
        RedAlertTimer = SetTimer("KickAll",10000,0);
        SendClientMessage(playerid, 0xFFFF00AA, "Red Alert Successful. To Abort Type /abort");
        printf("%s has initialized the red alert countdown.",aname);
        return 1;
      }
      else
      {
        return 0;
      }
      return 1;
    }
    if (strcmp(cmdtext, "/abort", true)==0)
    {
      if(IsPlayerAdmin(playerid))
      {
        new string[256];
        new aname[256];
        GetPlayerName(playerid,aname,sizeof(aname));
        GameTextForAll("~y~Red Alert Aborted",7000,4);
        format(string,sizeof(string), "Admin: %s has canceled the Red Alert",aname);
        SendClientMessageToAll(0x33AA33AA,string);
        KillTimer(RedAlertTimer);
        SendClientMessage(playerid, 0xFFFF00AA, "Red Alert Abort Successful.");
        printf("%s has cancelled the red alert countdown.",aname);
        return 1;
        }
        else
        {
          return 0;
        }
        return 1;
    }
_________________________________________________
Reply
#12

Admin Area
pawn Code:
// Set the timer triggering the function which checks if players are in the 'admin' area

SetTimer("AreaCheck", 1000, 1);

// The function

AreaCheck()
{
    for(new i=0; i<MAX_PLAYERS; i++ ) {
        if(IsPlayerConnected(i) && IsPlayerInArea(i, minx, maxx, miny, maxy) && !IsPlayerAdmin(i)) // CHECK: If the player is a admin, if the player is in the area
        { // He's not.
            SendClientMessage(i, YOURCOLOR, "You are not allowed to enter the ADMIN area.");
            /* Do with him whatever
                you want */

        }
    }
}

// ISPIA Function

IsPlayerInArea(playerid, Float:minx, Float:maxx, Float:miny, Float:maxy)
{
new Float:x, Float:y, Float:z;
GetPlayerPos(playerid, x, y, z);
if (x > minx && x < maxx && y > miny && y < maxy) return 1;
return 0;
}
Since it's something most newb scripters want to have, and "cant" find it, i'll post a very basic one. Hopefully this will stop some of the 'I cant find a working code' bs.
Reply
#13

if(IsPlayerConnected(i) && IsPlayerInArea(i, minx, maxx, miny, maxy) && !IsPlayerAdmin(i))
Reply
#14

I really dont see the use of that in this type of function :/ ..
Reply
#15

pawn Code:
SendClientMessage(i, YOURCOLOR, "You are not allowed to enter the ADMIN area.");
What if they leave while in the admin area?
Reply
#16

Quote:
Originally Posted by Flame^
I really dont see the use of that in this type of function :/ ..
Dunno what the function IsPlayerInArea does when you try to check for a playerid that is not connected.... Since that's what's gonna happen sooner or later: you will not always have a full 100 player load on your server.
Reply
#17

Added OnPlayerConnect for Flame^'s Snippet.

pawn Code:
SetTimer("AreaCheck", 1000, 1);
// The function
pawn Code:
AreaCheck()
{
    for(new i=0; i<MAX_PLAYERS; i++ ) {
        if(IsPlayerConnected(i))
        {
           if(IsPlayerInArea(i, minx, maxx, miny, maxy) && !IsPlayerAdmin(i)) // CHECK: If the player is a admin, if the player is in the area
           { // He's not.
              SendClientMessage(i, YOURCOLOR, "You are not allowed to enter the ADMIN area.");
              /* Do with him whatever you want */      
           }
        }
    }
}
// ISPIA Function
pawn Code:
IsPlayerInArea(playerid, Float:minx, Float:maxx, Float:miny, Float:maxy)
{
new Float:x, Float:y, Float:z;
GetPlayerPos(playerid, x, y, z);
if (x > minx && x < maxx && y > miny && y < maxy) return 1;
return 0;
}
Reply
#18

What do i need to edit?
Reply
#19

Quote:
Originally Posted by Pixels^
What do i need to edit?
Perhaps this:
I quote ******:
Moved and pinned, but could I point out that I posted those...
Reply
#20

Could you give an example of how it could be used? For instance in a str2bin(string[])? I think this could be really useful
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)