GetPlayerWeaponData reports 0 ammo
#1

I'm working on a sort of weapon anti-cheat (weapons and ammo are stored server-side).

When players connect to the server, their saved weapons are loaded and given to them. The weapon and ammo in each slot is stored in an array. Most of the time this works fine. Some times however, their weapons are given etc., but GetPlayerWeaponData is reporting they have 0 ammo. This is incorrect, as you can see in the image below.



The text in chat shows what GetPlayerWeaponData reports. Slot 5 is reporting that I have an M4 (correct) with 0 ammo (not correct). You can clearly see I have 100 ammo in the HUD in the top-right corner.

So as you can see, GetPlayerWeaponData is reporting 0 ammo, when players actually have ammo. What could possibly cause this?

If I shoot a single bullet off, my ammo is then corrected and GetPlayerWeaponData reports 99.

One solution I am going to try is using SetPlayerAmmo after giving them their weapons. I assume that will update it and fix the problem. Would like to hear any other suggestions though. Or perhaps a way to properly fix it and not make a workaround? Perhaps this is just another unfixable bug.
Reply
#2

Workaround untill you find a fix using https://sampwiki.blast.hk/wiki/GetPlayerAmmo

But you need to call that with weapon ids not slots.

Not a clue why GetPlayerWeaponData aint working though.

EDIT: forgot it only takes playerid param, maybe a combination of https://sampwiki.blast.hk/wiki/SetPlayerArmedWeapon and https://sampwiki.blast.hk/wiki/GetPlayerAmmo. Possibly wont look nice for the player.

Just ignore what i said it obviously would ruin it.
Reply
#3

It's your script itself causing this issue as mine works totally fine (with no prior shooting whatsoever).





pawn Code:
// DEVELOPMENT SCRIPT

// ** INCLUDES

#include <a_samp>
#include <a_mysql>
#include <zcmd>

// ** DEFINES

// *** FUNCTIONS

#define function%0(%1) forward%0(%1); public%0(%1)

// *** DATABASE

#define MYSQL_HOST "localhost"
#define MYSQL_USER "root"
#define MYSQL_PASSWORD ""
#define MYSQL_DATABASE "mysql_weapons"

// *** WEAPONS DATABASE

#define TABLE_WEAPONS "weapons"

#define WEAPONS_USERNAME "username"
#define WEAPONS_WEAPON_ID "weapon_id"
#define WEAPONS_WEAPON_AMMO "weapon_ammo"

// ** VARIABLES

// *** DATABASE

static weapons_database;

// ** MAIN

main()
{
    print("Development Mode: mysql_weapons.amx");
}

// ** CALLBACKS

public OnGameModeInit()
{
    LoadDatabase();
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

public OnPlayerSpawn(playerid)
{
    LoadPlayerWeapons(playerid);
    return 1;
}

// ** COMMANDS

CMD:weapondata(playerid, params[])
{
    new string[144], weapons[13][2];
    for(new i = 0; i <= 12; i ++)
    {
        GetPlayerWeaponData(playerid, i, weapons[i][0], weapons[i][1]);

        format(string, sizeof(string), "Weapon ID: %d - Ammo: %d.", weapons[i][0], weapons[i][1]);
        SendClientMessage(playerid, -1, string);
    }
    return 1;
}

// ** FUNCTIONS

stock LoadDatabase()
{
    mysql_log(LOG_ERROR | LOG_WARNING | LOG_DEBUG);
    weapons_database = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_DATABASE, MYSQL_PASSWORD);

    if(mysql_errno(weapons_database) != 0)
    {
        printf("[MySQL] Couldn't connect to %s.", MYSQL_DATABASE);
    }
    else
    {
        printf("[MySQL] Connected to %s.", MYSQL_DATABASE);

        new query[500], string[144];
        format(string, sizeof(string), "CREATE TABLE IF NOT EXISTS %s(", TABLE_WEAPONS);
        strcat(query, string);

        format(string, sizeof(string), "%s VARCHAR(%d),", WEAPONS_USERNAME, MAX_PLAYER_NAME);
        strcat(query, string);

        format(string, sizeof(string), "%s INT(20),", WEAPONS_WEAPON_ID);
        strcat(query, string);

        format(string, sizeof(string), "%s INT(20))", WEAPONS_WEAPON_AMMO);
        strcat(query, string);

        mysql_query(weapons_database, query);
    }  
    return 1;
}

stock PlayerName(playerid)
{
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, MAX_PLAYER_NAME);
    return name;
}

stock LoadPlayerWeapons(playerid)
{
    new query[256], player_name[MAX_PLAYER_NAME];
    mysql_escape_string(PlayerName(playerid), player_name);

    format(query, sizeof(query), "SELECT `%s`, `%s` FROM `%s` WHERE `%s` = '%s';", WEAPONS_WEAPON_ID, WEAPONS_WEAPON_AMMO, TABLE_WEAPONS, WEAPONS_USERNAME, player_name);
    mysql_function_query(weapons_database, query, true, "OnWeaponsLoaded", "i", playerid);
    return 1;
}

function OnWeaponsLoaded(playerid)
{
    new weaponid, ammo;
    for(new i = 0, j = cache_get_row_count(weapons_database); i < j; i ++)
    {
        weaponid = cache_get_row_int(i, 0, weapons_database);
        ammo = cache_get_row_int(i, 1, weapons_database);
       
        GivePlayerWeapon(playerid, weaponid, ammo);
    }
    return 1;
}
Reply
#4

Quote:
Originally Posted by Kevln
View Post
It's your script itself causing this issue as mine works totally fine (with no prior shooting whatsoever).
As I said, it happens rarely. It doesn't happen every time. Plus testing it on a blank script isn't really useful. My server isn't a blank script.
Reply
#5

Okay, I tried it few times and it works fine;
How did get it to be like this, You said it happens rarely so you just kept logging in and out till it happened or had the debug all the time ?

Also, iggy1's idea might work, It's surely not as good as GetWeaponData but it worth the try, the client doesn't see the weapon changing or anything, but maybe if he was shooting it'd stop him, didn't try, didn't check speed difference too, surely slower though
pawn Code:
CheckSlots(playerid)
{
    for (new i; i <= 12; i++)
    {
        new string[64], weapon, ammo, current_weapon = GetPlayerWeapon(playerid);
        GetPlayerWeaponData(playerid, i, weapon, ammo);

        SetPlayerArmedWeapon(playerid, weapon);
        format(string, sizeof(string), "[DEBUG] Slot: %d | Ammo(GetAmmo): %d | Ammo(WeaponData): %d", i, GetPlayerAmmo(playerid), ammo), SendClientMessage(playerid, -1, string);
        SetPlayerArmedWeapon(playerid, current_weapon);
    }
    return 1;
}
Reply
#6

Quote:
Originally Posted by MP2
View Post
As I said, it happens rarely. It doesn't happen every time. Plus testing it on a blank script isn't really useful. My server isn't a blank script.
Whether on a just-the-system script or an already established script it should work the same (in general terms). I tried several times and it showed correct values overall, how rare is this issue?
Reply
#7

Quote:
Originally Posted by Kevln
View Post
Whether on a just-the-system script or an already established script it should work the same (in general terms). I tried several times and it showed correct values overall, how rare is this issue?
Probably 10% of the time I spawn.
Reply
#8

Tried using SetPlayerArmedWeapon and SetPlayerAmmo to fix it, but no luck. It seems to happen when I recieve the weapons while paused (if I pause mid-connection).

I'm thinking this has something to do with the weapons specified if AddPlayerClass, which I've set to all 0. I'll try giving them their weapons a second or two after spawning and see if that helps.

EDIT: Oops, sorry about the double post. Thought someone replied.

EDIT #2: Tried it with a delay, but it's still messing up. Sigh. This time I lost some weapons (not just ammo) also. I don't understand why this only happens sometimes. I don't know what's causing it.
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)