Weapon ammo duplicates everytime player spawns
#1

Hello im trying to make my weapon system to save when character exits the server. and then load the ammo as it was when he left it. but when player spawns the ammo duplicates.. for example i gave myself deagle with 5 ammo, relogged and had 10 ammo.

Here is some code that might lead to something..
pawn Код:
forward SaveWeapons(playerid);
public SaveWeapons(playerid)
{
    new file[100];
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, sizeof(name));
    format(file, sizeof(file), "weapons/%s.ini", name);
    GetPlayerWeaponData(playerid, 1, WeaponInfo[playerid][Weapon1], WeaponInfo[playerid][Weapon1a]);
    GetPlayerWeaponData(playerid, 2, WeaponInfo[playerid][Weapon2], WeaponInfo[playerid][Weapon2a]);
    GetPlayerWeaponData(playerid, 3, WeaponInfo[playerid][Weapon3], WeaponInfo[playerid][Weapon3a]);
    GetPlayerWeaponData(playerid, 4, WeaponInfo[playerid][Weapon4], WeaponInfo[playerid][Weapon4a]);
    GetPlayerWeaponData(playerid, 5, WeaponInfo[playerid][Weapon5], WeaponInfo[playerid][Weapon5a]);
    GetPlayerWeaponData(playerid, 6, WeaponInfo[playerid][Weapon6], WeaponInfo[playerid][Weapon6a]);
    GetPlayerWeaponData(playerid, 7, WeaponInfo[playerid][Weapon7], WeaponInfo[playerid][Weapon7a]);
    GetPlayerWeaponData(playerid, 8, WeaponInfo[playerid][Weapon8], WeaponInfo[playerid][Weapon8a]);
    GetPlayerWeaponData(playerid, 9, WeaponInfo[playerid][Weapon9], WeaponInfo[playerid][Weapon9a]);
    GetPlayerWeaponData(playerid, 10, WeaponInfo[playerid][Weapon10], WeaponInfo[playerid][Weapon10a]);
    GetPlayerWeaponData(playerid, 11, WeaponInfo[playerid][Weapon11], WeaponInfo[playerid][Weapon11a]);
    GetPlayerWeaponData(playerid, 12, WeaponInfo[playerid][Weapon12], WeaponInfo[playerid][Weapon12a]);
    dini_IntSet(file, "Weapon1",WeaponInfo[playerid][Weapon1]);
    dini_IntSet(file, "Weapon1a",WeaponInfo[playerid][Weapon1a]);
    dini_IntSet(file, "Weapon2",WeaponInfo[playerid][Weapon2]);
    dini_IntSet(file, "Weapon2a",WeaponInfo[playerid][Weapon2a]);
    dini_IntSet(file, "Weapon3",WeaponInfo[playerid][Weapon3]);
    dini_IntSet(file, "Weapon3a",WeaponInfo[playerid][Weapon3a]);
    dini_IntSet(file, "Weapon4",WeaponInfo[playerid][Weapon4]);
    dini_IntSet(file, "Weapon4a",WeaponInfo[playerid][Weapon4a]);
    dini_IntSet(file, "Weapon5",WeaponInfo[playerid][Weapon5]);
    dini_IntSet(file, "Weapon5a",WeaponInfo[playerid][Weapon5a]);
    dini_IntSet(file, "Weapon6",WeaponInfo[playerid][Weapon6]);
    dini_IntSet(file, "Weapon6a",WeaponInfo[playerid][Weapon6a]);
    dini_IntSet(file, "Weapon7",WeaponInfo[playerid][Weapon7]);
    dini_IntSet(file, "Weapon7a",WeaponInfo[playerid][Weapon7a]);
    dini_IntSet(file, "Weapon8",WeaponInfo[playerid][Weapon8]);
    dini_IntSet(file, "Weapon8a",WeaponInfo[playerid][Weapon8a]);
    dini_IntSet(file, "Weapon9",WeaponInfo[playerid][Weapon9]);
    dini_IntSet(file, "Weapon9a",WeaponInfo[playerid][Weapon9a]);
    dini_IntSet(file, "Weapon10",WeaponInfo[playerid][Weapon10]);
    dini_IntSet(file, "Weapon10a",WeaponInfo[playerid][Weapon10a]);
    dini_IntSet(file, "Weapon11",WeaponInfo[playerid][Weapon11]);
    dini_IntSet(file, "Weapon11a",WeaponInfo[playerid][Weapon11a]);
    dini_IntSet(file, "Weapon12",WeaponInfo[playerid][Weapon12]);
    dini_IntSet(file, "Weapon12a",WeaponInfo[playerid][Weapon12a]);
    return 1;
}
pawn Код:
forward LoadWeapons(playerid);
public LoadWeapons(playerid)
{
    new file[100];
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, sizeof(name));
    format(file, sizeof(file), "weapons/%s.ini", name);
    if(!dini_Exists(file))
    {
        dini_Create(file);
        dini_IntSet(file, "Weapon1",WeaponInfo[playerid][Weapon1] = 0);
        dini_IntSet(file, "Weapon1a",WeaponInfo[playerid][Weapon1a] = 0);
        dini_IntSet(file, "Weapon2",WeaponInfo[playerid][Weapon2] = 0);
        dini_IntSet(file, "Weapon2a",WeaponInfo[playerid][Weapon2a] = 0);
        dini_IntSet(file, "Weapon3",WeaponInfo[playerid][Weapon3] = 0);
        dini_IntSet(file, "Weapon3a",WeaponInfo[playerid][Weapon3a] = 0);
        dini_IntSet(file, "Weapon4",WeaponInfo[playerid][Weapon4] = 0);
        dini_IntSet(file, "Weapon4a",WeaponInfo[playerid][Weapon4a] = 0);
        dini_IntSet(file, "Weapon5",WeaponInfo[playerid][Weapon5] = 0);
        dini_IntSet(file, "Weapon5a",WeaponInfo[playerid][Weapon5a] = 0);
        dini_IntSet(file, "Weapon6",WeaponInfo[playerid][Weapon6] = 0);
        dini_IntSet(file, "Weapon6a",WeaponInfo[playerid][Weapon6a] = 0);
        dini_IntSet(file, "Weapon7",WeaponInfo[playerid][Weapon7] = 0);
        dini_IntSet(file, "Weapon7a",WeaponInfo[playerid][Weapon7a] = 0);
        dini_IntSet(file, "Weapon8",WeaponInfo[playerid][Weapon8] = 0);
        dini_IntSet(file, "Weapon8a",WeaponInfo[playerid][Weapon8a] = 0);
        dini_IntSet(file, "Weapon9",WeaponInfo[playerid][Weapon9] = 0);
        dini_IntSet(file, "Weapon9a",WeaponInfo[playerid][Weapon9a] = 0);
        dini_IntSet(file, "Weapon10",WeaponInfo[playerid][Weapon10] = 0);
        dini_IntSet(file, "Weapon10a",WeaponInfo[playerid][Weapon10a] = 0);
        dini_IntSet(file, "Weapon11",WeaponInfo[playerid][Weapon11] = 0);
        dini_IntSet(file, "Weapon11a",WeaponInfo[playerid][Weapon11a] = 0);
        dini_IntSet(file, "Weapon12",WeaponInfo[playerid][Weapon12] = 0);
        dini_IntSet(file, "Weapon12a",WeaponInfo[playerid][Weapon12a] = 0);
    }
    else
    {
        WeaponInfo[playerid][Weapon1] = dini_Int(file, "Weapon1");
        WeaponInfo[playerid][Weapon1a] = dini_Int(file, "Weapon1a");
        WeaponInfo[playerid][Weapon2] = dini_Int(file, "Weapon2");
        WeaponInfo[playerid][Weapon2a] = dini_Int(file, "Weapon2a");
        WeaponInfo[playerid][Weapon3] = dini_Int(file, "Weapon3");
        WeaponInfo[playerid][Weapon3a] = dini_Int(file, "Weapon3a");
        WeaponInfo[playerid][Weapon4] = dini_Int(file, "Weapon4");
        WeaponInfo[playerid][Weapon4a] = dini_Int(file, "Weapon4a");
        WeaponInfo[playerid][Weapon5] = dini_Int(file, "Weapon5");
        WeaponInfo[playerid][Weapon5a] = dini_Int(file, "Weapon5a");
        WeaponInfo[playerid][Weapon6] = dini_Int(file, "Weapon6");
        WeaponInfo[playerid][Weapon6a] = dini_Int(file, "Weapon6a");
        WeaponInfo[playerid][Weapon7] = dini_Int(file, "Weapon7");
        WeaponInfo[playerid][Weapon7a] = dini_Int(file, "Weapon7a");
        WeaponInfo[playerid][Weapon8] = dini_Int(file, "Weapon8");
        WeaponInfo[playerid][Weapon8a] = dini_Int(file, "Weapon8a");
        WeaponInfo[playerid][Weapon9] = dini_Int(file, "Weapon9");
        WeaponInfo[playerid][Weapon9a] = dini_Int(file, "Weapon9a");
        WeaponInfo[playerid][Weapon10] = dini_Int(file, "Weapon10");
        WeaponInfo[playerid][Weapon10a] = dini_Int(file, "Weapon10a");
        WeaponInfo[playerid][Weapon11] = dini_Int(file, "Weapon11");
        WeaponInfo[playerid][Weapon11a] = dini_Int(file, "Weapon11a");
        WeaponInfo[playerid][Weapon12] = dini_Int(file, "Weapon12");
        WeaponInfo[playerid][Weapon12a] = dini_Int(file, "Weapon12a");
    }
}
pawn Код:
forward SetPlayerWeapons(playerid);
public SetPlayerWeapons(playerid)
{
    //for(new i=0;i<47;i++) Weapon[playerid][i] = true;
    //ResetPlayerWeaponsEx(playerid);
    if(WeaponInfo[playerid][Weapon1]) GivePlayerWeaponEx(playerid, WeaponInfo[playerid][Weapon1], WeaponInfo[playerid][Weapon1a]);
    if(WeaponInfo[playerid][Weapon2]) GivePlayerWeaponEx(playerid, WeaponInfo[playerid][Weapon2], WeaponInfo[playerid][Weapon2a]);
    if(WeaponInfo[playerid][Weapon3]) GivePlayerWeaponEx(playerid, WeaponInfo[playerid][Weapon3], WeaponInfo[playerid][Weapon3a]);
    if(WeaponInfo[playerid][Weapon4]) GivePlayerWeaponEx(playerid, WeaponInfo[playerid][Weapon4], WeaponInfo[playerid][Weapon4a]);
    if(WeaponInfo[playerid][Weapon5]) GivePlayerWeaponEx(playerid, WeaponInfo[playerid][Weapon5], WeaponInfo[playerid][Weapon5a]);
    if(WeaponInfo[playerid][Weapon6]) GivePlayerWeaponEx(playerid, WeaponInfo[playerid][Weapon6], WeaponInfo[playerid][Weapon6a]);
    if(WeaponInfo[playerid][Weapon7]) GivePlayerWeaponEx(playerid, WeaponInfo[playerid][Weapon7], WeaponInfo[playerid][Weapon7a]);
    if(WeaponInfo[playerid][Weapon8]) GivePlayerWeaponEx(playerid, WeaponInfo[playerid][Weapon8], WeaponInfo[playerid][Weapon8a]);
    if(WeaponInfo[playerid][Weapon9]) GivePlayerWeaponEx(playerid, WeaponInfo[playerid][Weapon9], WeaponInfo[playerid][Weapon9a]);
    if(WeaponInfo[playerid][Weapon10]) GivePlayerWeaponEx(playerid, WeaponInfo[playerid][Weapon10], WeaponInfo[playerid][Weapon10a]);
    if(WeaponInfo[playerid][Weapon11]) GivePlayerWeaponEx(playerid, WeaponInfo[playerid][Weapon11], WeaponInfo[playerid][Weapon11a]);
    if(WeaponInfo[playerid][Weapon12]) GivePlayerWeaponEx(playerid, WeaponInfo[playerid][Weapon12], WeaponInfo[playerid][Weapon12a]);
    //SetTimerEx("SaveWeapons", 2000, false, "i", playerid);
    return 1;
}
I'm guessing the problem is with SetPlayerWeapons(playerid), without this function the player won't get his weapons.

The SaveWeapons(playerid); is happening inside the stock where character info saved.
The LoadWeapons(playerid); is happening at the same time as SetPlayerWeapons(playerid); happening. They both happening in the stock where character info loaded.
Character loaded only after login, Character saved before disconnect.


Why does the ammo duplicates, any clues?
Reply
#2

Use ResetPlayerWeapons before providing weapons to them because GivePlayerWeapon would just double the amount of ammo - the ammo they get by spawning plus the saved ammo.

I'd also like to mention that it's better to delay the loading of weapons at least for 1000ms if you're using this under OnPlayerSpawn.
Reply
#3

@maximthepain

https://sampwiki.blast.hk/wiki/ResetPlayerWeapons
Reply
#4

Quote:
Originally Posted by Lordzy
Посмотреть сообщение
Use ResetPlayerWeapons before providing weapons to them because GivePlayerWeapon would just double the amount of ammo - the ammo they get by spawning plus the saved ammo.

I'd also like to mention that it's better to delay the loading of weapons at least for 1000ms if you're using this under OnPlayerSpawn.
Oh. you are right, thanks. The delay isn't needed as the weapons given after login, which is after everything loaded.
Reply
#5

You will still have issues if you are using GetPlayerWeaponData() rule of thumb never trust the client with this data do it yourself.

Consider doing things like this to prevent cheating or other problems that might occur with trusting the client. This method also doubles as an effective way to catch cheaters for example by comparing how much ammo the player has to GetPlayerWeaponData() all you would have to do is SyncAmmo(playerid) if it's not correct and report the player to the admins.

pawn Код:
enum PLAYERWEAPONINFO
{
    WeapDeagle,
    WeapShotGun,
    WeapMP5,
    WeapM4,
    ...
    ...
    ...
}

static p_WeaponData[MAX_PLAYERS][PLAYERWEAPONINFO];

public OnPlayerWeaponShot(playerid, weaponid, hittype, hitid, Float:fX, Float:fY, Float:fZ)
{
    switch(weaponid)
    {
        case 24: { p_WeaponData[playerid][WeapDeagle]--; }
        case 25: { p_WeaponData[playerid][WeapShotGun]--; }
        case 29: { p_WeaponData[playerid][WeapMP5]--; }
        case 31: { p_WeaponData[playerid][WeapM4]--; }
        ...
        ...
        ...
    }
    return 1;
}

GivePlayerWeaponAmmo(playerid, weaponid, ammo)
{
    GivePlayerWeapon(playerid, weaponid, ammo);
    switch(weaponid)
    {
        case 24: { p_WeaponData[playerid][WeapDeagle] = ammo; }
        case 25: { p_WeaponData[playerid][WeapShotGun] = ammo; }
        case 29: { p_WeaponData[playerid][WeapMP5] = ammo; }
        case 31: { p_WeaponData[playerid][WeapM4] = ammo; }
        ...
        ...
        ...
    }
    return 1;
}

SyncAmmo(playerid)
{
    ResetPlayerWeapons(playerid);
    if(p_WeaponData[playerid][WeapDeagle]) GivePlayerWeapon(playerid, p_WeaponData[playerid][WeapDeagle]);
    if(p_WeaponData[playerid][WeapShotGun]) GivePlayerWeapon(playerid, p_WeaponData[playerid][WeapShotGun]);
    if(p_WeaponData[playerid][WeapMP5]) GivePlayerWeapon(playerid, p_WeaponData[playerid][WeapMP5]);
    if(p_WeaponData[playerid][WeapM4]) GivePlayerWeapon(playerid, p_WeaponData[playerid][WeapM4]);
    ...
    ...
    ...
   
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)