Critical bug - GivePlayerWeapon
#1

I noticed a few days ago that the function "GivePlayerWeapon" is bugged. Not the whole function, but it's last parameter, "ammo". I have this code in my script:

pawn Код:
GivePlayerWeapon(playerid, 32, 20);
It would give me a Tec9 with 20 bullets, but instead of 20 it gives me 520 bullets (420-100)! I think it's value is mixing with other weapons value, because when I bought the Tec9 on ammunation (I have weapon buy system), I was holding an Desert Eagle with 500 bullets (I wanted to be 500, not bug).

When I have no weapon it gives me the 20 bullets, withouth the additional 500.

I hope this can be fixed as soon as possible, because for a RP servers it's very bad having more bullets than you deserve to, as you only paid for 20 bullets.
Reply
#2

I don't think i understand your problem fully, it sounds like you're saying you get 500 extra ammo when you have a deagle with 500 ammo.
If you have an smg with 500 bullets and buy a tec 9 with 20 then yes you'll get 520 ammo for the tec 9, there are simple work arounds for this and is a GTA SA thing
Reply
#3

If you have a weapon in that slot already, you ammo will be added.
For example if you have an MP5 with 500 ammo, and you give the player a UZI with 500 ammo,
he will have a UZI of 1000 ammo.
The solution is to store their current weapons, reset their weapons, and give them back with the new ammo.

So to conclude, this is not a bug.
Reply
#4

OR you can play as the ammo were compatible for the other weapon
Reply
#5

Hum.. Ok, thanks.

@Leong: how would I do what you recommended? Because if I do GivePlayerWeapon the ammo wouldn't be added again?
Reply
#6

Firstly use GetPlayerWeaponData to get all the weapons of a player.
Then You can use ResetPlayerWeapons to clear his weapons.
After that you give them back all the weapons they have, except the weapon you wanted to give them.
Finally you can give the weapon you want to give to him.
Reply
#7

Thanks for your answer Leong. I did what you recommended but didn't work. Take a look at my code:

pawn Код:
stock GivePlayerWeaponEx(playerid, gun, ammo)
{
    new weapons[13], ammovar[13];
   
    for(new i = 0; i < 13; i++)
    {
        GetPlayerWeaponData(playerid, i, weapons[i], ammovar[i]);
    }
   
    ResetPlayerWeapons(playerid);
   
    for(new i = 0; i < 13; i++)
    {
        GivePlayerWeapon(playerid, weapons[i], ammovar[i]);
    }
   
    GivePlayerWeapon(playerid, gun, ammo);
}
OBS for admins: I think this topic should be renamed to "How to fix GivePlayerWeapon bug" and should be moved to "Scripting Discussion".
Reply
#8

Quote:
Originally Posted by [BEP]AcerPilot
Посмотреть сообщение
Thanks for your answer Leong. I did what you recommended but didn't work. Take a look at my code:

pawn Код:
stock GivePlayerWeaponEx(playerid, gun, ammo)
{
    new weapons[13], ammovar[13];
   
    for(new i = 0; i < 13; i++)
    {
        GetPlayerWeaponData(playerid, i, weapons[i], ammovar[i]);
    }
   
    ResetPlayerWeapons(playerid);
   
    for(new i = 0; i < 13; i++)
    {
        GivePlayerWeapon(playerid, weapons[i], ammovar[i]);
    }
   
    GivePlayerWeapon(playerid, gun, ammo);
}
OBS for admins: I think this topic should be renamed to "How to fix GivePlayerWeapon bug" and should be moved to "Scripting Discussion".
You'll need to find the slot of the weapon.
For example if a player have an MP5 and you want to give him a UZI, which both are in slot 4, you'll have to do this:
pawn Код:
stock GivePlayerWeaponEx(playerid, gun, ammo)
{
    new weapons[13], ammovar[13];
   
    for(new i = 0; i < 13; i++)
    {
        GetPlayerWeaponData(playerid, i, weapons[i], ammovar[i]);
    }
   
    ResetPlayerWeapons(playerid);
   
    for(new i = 0; i < 13; i++)
    {
        if(i != 4) GivePlayerWeapon(playerid, weapons[i], ammovar[i]);// 4 is the slot for MP5 and UZI. You'll need to make a function to find the slots of the weapon you want to give, and replace with the number 4.
    }
   
    GivePlayerWeapon(playerid, gun, ammo);
}
Check it out:https://sampwiki.blast.hk/wiki/Weapons
Reply
#9

That could help you:
pawn Код:
#define INVALID_WEAPON_SLOT_ID      -1

stock SetPlayerWeapon(playerid, weapon, ammo)
{
    new tweapon[13], tammo[13];
    for(new i = 0; i < 13; i++) if(GetWeaponSlot(weapon) != i) GetPlayerWeaponData(playerid, i, tweapon[i], tammo[i]);
    tweapon[GetWeaponSlot(weapon)] = weapon;
    tammo[GetWeaponSlot(weapon)] = ammo;
    ResetPlayerWeapons(playerid);
    for(new i = 0; i < 13; i++) GivePlayerWeapon(playerid, tweapon[i], tammo[i]);
    SetPlayerArmedWeapon(playerid, weapon);
}

stock 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;
}
Usage:
pawn Код:
SetPlayerWeapon(playerid, weapon, ammo);
Example:
pawn Код:
SetPlayerWeapon(playerid, 32, 20);
This stock will just set the players weapon ID and ammo instead of increasing the ammo.
Reply
#10

OK, thanks guys. I'll test the codes.

#EDIT: Worked. Thanks for all that answered in here. Admins can lock this topic if they want to.

#EDIT2: I just noticed when I put GivePlayerWeaponEx in the stock that logs in the player when he types /login, the server stop working. All commands are 'Server: Unknown Command' and all the functions that should be executed after GivePlayerWeaponEx aren't executed. I think it's because it's the first time he will receive the weapons. Suggestions?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)