Useful Snippets
#61

Heres a simple drug script for player health i made for my server. It allows you you purchase drugs on the fly for health.
Heres wut it scrolls when you type /drugs:

-------------= Drug's To Buy =------------
Weed: costs $1000 - Adds 20% Health
Cocaine: costs $2000 - Adds 40% Health
Speed: costs $3000 - Adds 60% Health
Extacy: costs $4000 - Adds 80% Health
---------Usage: /get(drugname)------------

The script checks for 2 things: Player Money and Player Health:
If you dont have enough money, it wont let you buy the drug, also if the Percent of Health the drug gives you exceeds 100% of the players health it will tell you to buy a cheaper drug, so your health does not exceed 100%

Heres the pasteable code link from pastebin:


http://sykozscripts.pastebin.com/920965
Reply
#62

http://sa-mp.pastebin.co.uk/16304 - Debugger that I'm using for my gamemode.

pawn Код:
public OnPlayerRequestClass(playerid, classid)
{
    DEBUGF("OnPlayerRequestClass(%d, %d)",playerid, classid);
   
    return 1;
}
In this code if both C_DEBUG and G_DEBUG are true it will print the debug message to console and also to the admins in-game .
Reply
#63

Servers with looping animations and /stopanim command to stop animations have it just instantly click the character into the default position. with the following animation code you can have them move into the idle position more smoothly without it looking crappy, watch the video if you dont know what i mean, the first example in the video is taken from an existing server, the second is using the following code.

http://www.youtube.com/watch?v=khO6F1gx4cU

Код:
}
	// Leave animation
  if (strcmp("/stop", cmdtext, true) == 0) {
	   ApplyAnimation(playerid, "CARRY", "crry_prtial", 4.0, 0, 0, 0, 0, 0); // Leave animation
     SendClientMessage(playerid, 0xFF0000FF, "");
		 return 1;
Reply
#64

Globals:
Код:
new gObjects[ MAX_OBJECTS ];
new TotalObjects = 0;
IsValidObjectID:
Код:
public IsValidObjectID( objectid )
{
    if(
    //weapons
    ( objectid >= 321 && objectid <= 326 )
    || ( objectid >= 330 && objectid <= 331 )
    || ( objectid >= 333 && objectid <= 339 )
    || ( objectid >= 341 && objectid <= 344 )
    || ( objectid >= 346 && objectid <= 363 )
    || ( objectid >= 365 && objectid <= 372 )

    //fun stuff
    || ( objectid >= 1433 && objectid <= 13594 )

    //roads
    || ( objectid >= 5482 && objectid <= 5512 )

    //barriers
    || ( objectid >= 966 && objectid <= 998 )

    //misc 1210-1325
    || ( objectid >= 1210 && objectid <= 1325 )

    //misc 1420-1620
    || ( objectid >= 1420 && objectid <= 1620 )

    //misc 1971-4522
    || ( objectid >= 1971 && objectid <= 4522 ) )
    {
     return 1;
    }
    return 0;
}
SpawnObject
Код:
public SpawnObject( playerid, modelid )
{
    if( IsPlayerAdmin( playerid ) == 0 )
    {
      SendClientMessage( playerid, COLOR_RED, "ADMIN ONLY" );
      return 0;
    }

    if( TotalObjects >= MAX_OBJECTS )
    {
      SendClientMessage( playerid, COLOR_RED, "Too many objects on the map!" );
      return 0;
    }

    if( IsValidObjectID( modelid ) == 0 )
    {
      SendClientMessage( playerid, COLOR_RED, "Invalid or unsafe model id!" );
      return 0;
    }

    new Float:SpawnRotZ;

    if( IsPlayerDriver( playerid ) == 0 )
    {
      new Float:PlayerX, Float:PlayerY, Float:PlayerZ;
      GetPlayerPos( playerid, PlayerX, PlayerY, PlayerZ );
      GetPlayerFacingAngle( playerid, SpawnRotZ );
      gObjects[ TotalObjects ] = CreateObject( modelid, PlayerX, PlayerY, PlayerZ, 0.0, 0.0, SpawnRotZ );
      SetPlayerPos( playerid, PlayerX, PlayerY, PlayerZ + 10.0 );
    }
    else if( IsPlayerDriver( playerid ) == 1 )
    {
      new Float:CarX, Float:CarY, Float:CarZ;
      GetVehiclePos( GetPlayerVehicleID( playerid ), CarX, CarY, CarZ );
      GetVehicleZAngle( GetPlayerVehicleID( playerid ), SpawnRotZ );
      gObjects[ TotalObjects ] = CreateObject( modelid, CarX, CarY, CarZ, 0.0, 0.0, SpawnRotZ );
      SetVehiclePos( GetPlayerVehicleID( playerid ), CarX, CarY, CarZ + 10.0 );
    }

    TotalObjects++;

    new szMsg[ 256 ];
    format( szMsg, sizeof( szMsg ), "Model %i ( Object %i ) spawned by admin.", modelid, TotalObjects );
    SendClientMessageToAll( COLOR_YELLOW, szMsg );
    return 1;
}
DeleteSingleObject
Код:
public DeleteSingleObject( playerid, objectid )
{
    if( IsPlayerAdmin( playerid ) == 0 )
    {
      SendClientMessage( playerid, COLOR_RED, "ADMIN ONLY" );
      return 0;
    }

    if( IsValidObject( objectid ) == 1 )
    {
      DestroyObject( objectid );

      new szDel[ 256 ];
      format( szDel, sizeof( szDel ), "Object id %i deleted by admin!", objectid );
      SendClientMessageToAll( COLOR_YELLOW, szDel );
      TotalObjects--;
      return 1;
    }
    else
    {
      SendClientMessage( playerid, COLOR_RED, "Invalid object id." );
      return 0;
    }

    return 1;
}
DeleteAllObjects
Код:
public DeleteAllObjects( playerid )
{
    if( IsPlayerAdmin( playerid ) == 0 )
    {
      SendClientMessage( playerid, COLOR_RED, "ADMIN ONLY" );
      return 0;
    }

    for( new i = 0; i < MAX_OBJECTS; i++ )
    {
      if( IsValidObject( i ) == 1 )
      {
        DestroyObject( i );
      }
    }

    SendClientMessageToAll( COLOR_YELLOW, "All objects deleted by admin!" );

    TotalObjects = 0;
    return 1;
}
SpawnRamp
Код:
public SpawnRamp( playerid, rampid )
{
    new nrampid = 1655;

    switch( rampid )
    {
        case 1:
        {
          nrampid = 1655;
        }
        case 2:
        {
          nrampid = 1473;
        }
        case 3:
        {
          nrampid = 1474;
        }
        case 4:
        {
          nrampid = 1475;
        }
        case 5:
        {
          nrampid = 1477;
        }
        default:
        {
          nrampid = 1655;
        }
    }
    return SpawnObject( playerid, nrampid );
}
Reply
#65

Minigun War Zone (Two in One)
This command will teleport the player to one place then another, in only one co-ordinate!
pawn Код:
if (strcmp(cmdtext, "/minimad", true)==0) // second command
  {
    SendClientMessage(playerid, COLOR_GREEN,"GET READY TO BATTLE :D");
    GameTextForPlayer(playerid, "Minigun wars!", 5000, 0);
    SetPlayerPos(playerid,1410.9625, 667.3798, -193.8245);
    ResetPlayerWeapons(playerid);
    GivePlayerWeapon(playerid, 38, 9000);

    return 1;
  }
Reply
#66

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

Disconnect icon in DeathStats from ******
pawn Код:
public OnPlayerDisconnect(playerid)
{
  SendDeathMessage(INVALID_PLAYER_ID, playerid, 201);
}
[/quote]

the disconnect icon shows for me, but the connect icon wont show.
Reply
#67

This has been discussed before, if you can't get the disconnect icon to show, use the connect icon for both.

Cheers,
Ramjet.
Reply
#68

This gets a randomly selected connected ID. If none is connected, then in theory, it'll keep trying until someone does.

pawn Код:
stock GetRandomID()
{
    new randn = random(MAX_PLAYERS);
   
    if(IsPlayerConnected(randn)) return randn;
   
    else
    {
        return GetRandomID();
    }
}
Example of use:
pawn Код:
GivePlayerMoney(GetRandomID(), 5000);
Reply
#69

Sure there are many anti-driveby filterscripts, but this is just a snippet. You may notice you have what looks like negative ammo upon entering a vehicle, but you don't, it's actually 0.

At the top of the script.
pawn Код:
static DBWeapon[MAX_PLAYERS],DBAmmo[MAX_PLAYERS];
Under OnPlayerStateChange.
pawn Код:
if(newstate == PLAYER_STATE_DRIVER)
    {
      GetPlayerWeaponData(playerid,4,DBWeapon[playerid],DBAmmo[playerid]);
      SetPlayerAmmo(playerid,DBWeapon[playerid],0);
    }
else if(oldstate == PLAYER_STATE_DRIVER)
    {
      GivePlayerWeapon(playerid,DBWeapon[playerid],DBAmmo[playerid]);
    }
Reply
#70

improved Anti drive by
wich disallows you to shoot as passenger or driver

Код:
on top
static DBWeapon[MAX_PLAYERS],DBAmmo[MAX_PLAYERS],PlayerDied[MAX_PLAYERS];

at OnPlayerConnect
PlayerDied[playerid] = 0;

at OnPlayerDeath
PlayerDied[playerid] = 1;

at OnPlayerEnterVehicle
PlayerDied[playerid] = 0;

public OnPlayerStateChange(playerid, newstate, oldstate)
{
   if (newstate == 3) // passenger
   {
   GetPlayerWeaponData(playerid,4,DBWeapon[playerid],DBAmmo[playerid]);
   SetPlayerAmmo(playerid,DBWeapon[playerid],0);
   }
   if (newstate == 2) // driver
   {
   GetPlayerWeaponData(playerid,4,DBWeapon[playerid],DBAmmo[playerid]);
   SetPlayerAmmo(playerid,DBWeapon[playerid],0);
   }	 
   if (newstate == 1) // foot
   {
   if(PlayerDied[playerid] == 0){
   GivePlayerWeapon(playerid,DBWeapon[playerid],DBAmmo[playerid]);
   }
   }
   return 1;
}
Reply
#71

Quote:
Originally Posted by tomozj
This gets a randomly selected connected ID. If none is connected, then in theory, it'll keep trying until someone does.

pawn Код:
stock GetRandomID()
{
    new randn = random(MAX_PLAYERS);
   
    if(IsPlayerConnected(randn)) return randn;
   
    else
    {
        return GetRandomID();
    }
}
Example of use:
pawn Код:
GivePlayerMoney(GetRandomID(), 5000);
Isnt that a bit dangerous? If no ones connected it will never end, if a few people are connected it will take a long time to complete and pawn is single threaded. I think this might be better:
pawn Код:
stock GetRandomID()
{
    new bool:connected[MAX_PLAYERS] = false, amount = 0;
    for(new i = 0; i < MAX_PLAYERS; i++)
      if(IsPlayerConnected(i))
      {
        connected[i] = true;
        amount++;
      }
    if(amount = 0) return -1;
    new rand = random(amount), done = 0;
    for(new i = 0; i < MAX_PLAYERS; i++)
      if(connected[i])
      {
        if(done == rand) return i;
        done++;
        }
    return -1;
}
returns -1 if no1s connected.
Reply
#72

Auto Link Vehicle To Interior

Код:
public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
{
  if (IsPlayerInAnyVehicle(playerid))
  {
   LinkVehicleToInterior(GetPlayerVehicleID(playerid), newinteriorid);
  }
  return 1;
}
Credits: munich123
Reply
#73

Quote:
Originally Posted by BlooDsTainD
Servers with looping animations and /stopanim command to stop animations have it just instantly click the character into the default position. with the following animation code you can have them move into the idle position more smoothly without it looking crappy, watch the video if you dont know what i mean, the first example in the video is taken from an existing server, the second is using the following code.

http://www.youtube.com/watch?v=khO6F1gx4cU

Код:
}
	// Leave animation
  if (strcmp("/stop", cmdtext, true) == 0) {
	   ApplyAnimation(playerid, "CARRY", "crry_prtial", 4.0, 0, 0, 0, 0, 0); // Leave animation
     SendClientMessage(playerid, 0xFF0000FF, "");
		 return 1;
Hi.
How can u make an anim to stop just by pressing a key (for example the RIGHT SHIFT) without using the /stopanim cmd?
Reply
#74

Quote:
Originally Posted by Salvatore_Salieri
Quote:
Originally Posted by BlooDsTainD
Servers with looping animations and /stopanim command to stop animations have it just instantly click the character into the default position. with the following animation code you can have them move into the idle position more smoothly without it looking crappy, watch the video if you dont know what i mean, the first example in the video is taken from an existing server, the second is using the following code.

http://www.youtube.com/watch?v=khO6F1gx4cU

Код:
}
	// Leave animation
  if (strcmp("/stop", cmdtext, true) == 0) {
	   ApplyAnimation(playerid, "CARRY", "crry_prtial", 4.0, 0, 0, 0, 0, 0); // Leave animation
     SendClientMessage(playerid, 0xFF0000FF, "");
		 return 1;
Hi.
How can u make an anim to stop just by pressing a key (for example the RIGHT SHIFT) without using the /stopanim cmd?
Use the function that is made to accept keys....Duh

pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
Reply
#75

This is the new clear text, accounting for the 50 lines of text in 0.2.2

pawn Код:
for(new i = 0; i < 50; i++)
  SendClientMessage(playerid,0xFFFFFFFF," ");
Reply
#76

You may find this useful if you use a dynamic object system and want to teleport to a place (object place) without falling through the objects while they load.....(example: YSI objects)

pawn Код:
forward DynUpdateStart(playerid);
forward DynUpdateEnd(playerid);
pawn Код:
public DynUpdateStart(playerid)
{
    TogglePlayerControllable(playerid, false);
    new string[255];
    format(string, sizeof(string), "~g~Objects~n~~r~Loading");
    GameTextForPlayer(playerid, string, 3000, 6);
    PlayerPlaySound(playerid, 1056, 0.0, 0.0, 0.0);
    SetTimerEx("DynUpdateEnd", 3000, 0,"e",playerid);
    return 1;
}
pawn Код:
public DynUpdateEnd(playerid)
{
    TogglePlayerControllable(playerid, true);
    new string[255];
    format(string, sizeof(string), "~g~Objects~n~~r~Loaded!");
    GameTextForPlayer(playerid, string, 3000, 6);
    PlayerPlaySound(playerid, 1056, 0.0, 0.0, 0.0);
    return 1;
}

Use: (put into the teleport script or spawn etc...)
pawn Код:
DynUpdateStart(playerid)
Credits: Shurty40
Reply
#77

simple anti-max ping!

Quote:

public OnPlayerConnect(playerid)
{
SetTimerEx("MaxPing",15000,0,"d",playerid);
return 1;
}

public MaxPing(playerid)
{
new PlayerPing = GetPlayerPing(playerid);
if (PlayerPing > 550)
{
SendClientMessage(playerid,COLOR_RED,"Sorry,but your ping is very hight!");
new PlayerName[30], str[256];
GetPlayerName(playerid, PlayerName, 30);
format(str, 256, "%s has been kicked for max ping!", PlayerName);
SendClientMessageToAll(COLOR_GREEN,str);
Kick(playerid);
}else{
SendClientMessage(playerid,COLOR_GREEN,"Your ping is normal!;");
}
return 1;
}

sorry for my bad english!
i am russian
Reply
#78

Nice thing ******, i'm just wondering if it's really more efficient than normal loops (if there is not much thing to do in the loop).


To add something useful in this post, probably a lot of people ignore this:

Instead of doing something like this (beginner method):
pawn Код:
new bool:B = true;

Test()
{
    if (B)
        B = false;
    else
        B = true;

    printf( "B is now %s", (B ? ("true") : ("false")) );
}
Or..like that (intermediate method):
pawn Код:
new bool:B = true;

Test()
{
    B = B ? false : true;

    printf( "B is now %s", (B ? ("true") : ("false")) );
}
You can simply do (leet method):
pawn Код:
new bool:B = true;

Test()
{
    B = !B;

    printf( "B is now %s", (B ? ("true") : ("false")) );
}
Run Test() a few time, this will print alternatively "B is now false", "B is now true", "B is now false"...
They all do the same thing, the last one is just much easier to write, i've found this there is a few days.
This obviously works only for booleans and this can be useful for toggleable commands/options, things like that.


PS: Why Useful topics are all un-stickied ?
Reply
#79

u guys only need to set a timer to destroy vehicle after 1 second the player have no time to come back i already tested nowone crash
Reply
#80

Quote:
Originally Posted by [TG
Snipe ]
u guys only need to set a timer to destroy vehicle after 1 second the player have no time to come back i already tested nowone crash
What do you think my script does? Also a little extra security doesn't hurt.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)