OnPlayerWeaponShot
#1

It doesn't sync well when a player is firing from an MP5, i've tested every single weapon and this one turned out to have flaws.
Reply
#2

Can you explain what you mean by syncing well? You mean sometimes the shots don't trigger the callback?
Reply
#3

Here's my entire callback:
I made a server-side weapon system and made this callback to update the player's ammo every time he fires a bullet, however the only weapon that i've come across that doesn't function well is the MP5 and it does not set the ammo accurately.

pawn Code:
public OnPlayerWeaponShot(playerid, weaponid, hittype, hitid, Float:fX, Float:fY, Float:fZ)
{
    if( hittype != BULLET_HIT_TYPE_NONE ) // Bullet Crashing uses just this hittype
    {
        if( !( -1000.0 <= fX <= 1000.0 ) || !( -1000.0 <= fY <= 1000.0 ) || !( -1000.0 <= fZ <= 1000.0 ) ) // a valid offset, it's impossible that a offset bigger than 1000 is legit (also less than -1000.0 is impossible, not used by this hack, but still, let's check for it, just for the future, who knows what hacks will appear). The object with biggest offset is having ~700-800 radius.
        {
            Kick(playerid);
            return 0; // let's desynchronize that bullet, so players won't crash
        }
    }
    new beforeAmmo = PVar:playerid[WeaponAmmo][weaponid];
    new leftAmmo = GetWeaponAmmo(playerid, weaponid) - 1;
   
    if(PlayerHasWeapon(playerid, weaponid))
    {
        if(beforeAmmo - leftAmmo == 1)
        {
            if(!PVar:playerid[SpawnedWeapon][weaponid])
            {
                if(IsPrimaryWeapon(weaponid))
                {
                    if(weaponid == PVar:playerid[Weapons][PRIME_WEP])
                    {
                        if(leftAmmo == 0)
                        {
                            PVar:playerid[Weapons][PRIME_WEP] = 0;
                        }
                        PVar:playerid[Weapons][PRIME_AMMO] = leftAmmo;
                    }
                }
                else if(IsSecondaryWeapon(weaponid))
                {
                    if(weaponid == PVar:playerid[Weapons][SECOND_WEP])
                    {
                        if(leftAmmo == 0)
                        {
                            PVar:playerid[Weapons][SECOND_WEP] = 0;
                        }
                        PVar:playerid[Weapons][SECOND_AMMO] = leftAmmo;
                    }
                }      
            }
            if(leftAmmo == 0)
            {
                if(PVar:playerid[SpawnedWeapon][weaponid])
                {
                    PVar:playerid[SpawnedWeapon][weaponid] = false;
                    RemovePlayerWeapon(playerid, weaponid);
                }
            }
            PVar:playerid[WeaponAmmo][weaponid] = leftAmmo;
        }
    }
    return 1;
}
Reply
#4

GetWeaponAmmo is not a SA:MP function, and I'm gonna guess that may very well be causing an issue. Would you mind showing us that function?
Reply
#5

pawn Code:
GetWeaponAmmo(playerid, weaponid)
{
    new weapon, ammo;
    GetPlayerWeaponData(playerid, GetWeaponSlot(weaponid), weapon, ammo);
    return ammo;
}
pawn Code:
GetWeaponSlot(weaponid)
{
    new slot;
    switch(weaponid)
    {
        case 0,1: slot = 0;
        case 2..9: slot = 1;
        case 22..24: slot = 2;
        case 25..27: slot = 3;
        case 28,29,32: slot = 4;
        case 30,31: slot = 5;
        case 33,34: slot = 6;
        case 35..38: slot = 7;
        case 16..18,39: slot = 8;
        case 41..43: slot = 9;
        case 10..15: slot = 10;
        case 44..46: slot = 11;
        case 40: slot = 12;
        default: slot = INVALID_WEAPON_SLOT_ID;
    }
    return slot;
}
Reply
#6

Try seeing if the ammo is properly returned using GetPlayerAmmo. For example:

pawn Code:
GetWeaponAmmo(playerid, weaponid)
{
    new weapon, ammo;
    if(GetPlayerWeapon(playerid) == WEAPON_MP5)
    {
         return GetPlayerAmmo(playerid);
    }
    GetPlayerWeaponData(playerid, GetWeaponSlot(weaponid), weapon, ammo);
    return ammo;
}
Try seeing if this works.
Reply
#7

It returns the right amount and still doesn't work properly.
I'm pretty sure that the only reason that can explain this is because the MP5 is firing rather fast and OnPlayerWeaponShot gets called slower than expected.

I first had the problem with the UZI and TEC 9 because the bullets are getting fired really fast, I simply fixed it by setting the player's weapon skill to 1 so the fire rate will slow down. But doing that for the MP5 doesn't work.

Reply
#8

You can try using OnPlayerKeyStateChange to try to account for lost bullets. You can also try freezing the player for 250ms after firing an MP5.
Reply
#9

I think you facing c-bug xd
Reply
#10

Are you a driver when your firing your gun? It only works for passenger's and not drivers.

https://sampwiki.blast.hk/wiki/OnPlayerWeaponShot - "Only passenger drive-by is supported (not driver drive-by, and not sea sparrow / hunter shots)."

Would be nice if driver drive-by was synced.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)