AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Limit of buy item but he take ammo (https://forums.alliedmods.net/showthread.php?t=333931)

zollymaN 08-18-2021 12:33

Limit of buy item but he take ammo
 
I put a limit on an item so I can't abuse it but when I want to buy the same item I keep deducting from ammo packs why?


Code:

#include <amxmodx>
#include <fun>
#include <zp_apocalypse>

new const item_name[] = "Buy 1000 HP!"
new g_itemid_buyhp
new hpamount
new g_bComprado[ 33 ];
new cvar_limit

public plugin_init()
{
    register_plugin("[ZP] Buy Health Points", "1.0", "T[h]E Dis[as]teR")
   
    hpamount = register_cvar("zp_buyhp_amount", "1000")
    cvar_limit = register_cvar("zp_buyhp_limit", "1")
   
    g_itemid_buyhp = zp_register_extra_item(item_name, 5, ZP_TEAM_ZOMBIE & ZP_TEAM_HUMAN)
   
    register_event("HLTV", "event_round_start", "a", "1=0", "2=0")
}
public event_round_start()
{
    arrayset( g_bComprado, 33, 0 );
}
public zp_extra_item_selected(id,itemid)
{
    if(!is_user_alive(id))
       
    return PLUGIN_HANDLED;
   
    if(itemid==g_itemid_buyhp)
    {
        if(zp_get_user_ammo_packs(id) < 20)
        {
            client_print(id, print_chat,"[ZP] Not enough Ammopacks!");
            return PLUGIN_HANDLED;
        }
        else
        {
            if( g_bComprado[ id ] < get_pcvar_num(cvar_limit))
            {
                g_bComprado[ id ] ++;
                set_user_health(id,get_user_health(id)+get_pcvar_num(hpamount));
                zp_set_user_ammo_packs(id, zp_get_user_ammo_packs(id) - 5);
                client_print(id, print_chat,"[ZP] You Bought HP!");
            }
            else
            {
                client_print(id, print_chat,"[ZP] poti cumpara acest item daar odata pe runda");
            }
        }
    }
    return PLUGIN_CONTINUE;
}


lexzor 08-18-2021 13:55

Re: Limit of buy item but he take ammo
 
Because you don't register cvar correct.

you have to do something like this

PHP Code:

new g_Cvar[2];

public 
plugin_init() 
{
    
register_plugin("[ZP] Buy Health Points""1.0""T[h]E Dis[as]teR")
    
    
g_Cvar[0] = register_cvar("zp_buyhp_amount""1000")
    
g_Cvar[1] = register_cvar("zp_buyhp_limit""1")
    
    
cvar_limit get_pcvar_num(g_Cvar[1]);
    
hpamount get_pcvar_num(g_Cvar[0]);

    
g_itemid_buyhp zp_register_extra_item(item_name5ZP_TEAM_ZOMBIE ZP_TEAM_HUMAN)
    
    
register_event("HLTV""event_round_start""a""1=0""2=0")


and next time, do some debugging using server_print.

You could resolve this if you easily print on server console cvar_limit.

Code:

native register_cvar(const name[], const string[], flags = FCVAR_NONE, Float:fvalue = 0.0);

Return
Unique cvar pointer

https://www.amxmodx.org/api/cvars/register_cvar

Napoleon_be 08-18-2021 19:11

Re: Limit of buy item but he take ammo
 
Quote:

Originally Posted by lexzor (Post 2755546)
Because you don't register cvar correct.

you have to do something like this

PHP Code:

new g_Cvar[2];

public 
plugin_init() 
{
    
register_plugin("[ZP] Buy Health Points""1.0""T[h]E Dis[as]teR")
    
    
g_Cvar[0] = register_cvar("zp_buyhp_amount""1000")
    
g_Cvar[1] = register_cvar("zp_buyhp_limit""1")
    
    
cvar_limit get_pcvar_num(g_Cvar[1]);
    
hpamount get_pcvar_num(g_Cvar[0]);

    
g_itemid_buyhp zp_register_extra_item(item_name5ZP_TEAM_ZOMBIE ZP_TEAM_HUMAN)
    
    
register_event("HLTV""event_round_start""a""1=0""2=0")


and next time, do some debugging using server_print.

You could resolve this if you easily print on server console cvar_limit.

Code:

native register_cvar(const name[], const string[], flags = FCVAR_NONE, Float:fvalue = 0.0);

Return
Unique cvar pointer

https://www.amxmodx.org/api/cvars/register_cvar

There's nothing wrong with the way he's registering his cvars. You're making things complicated for no reason.

OP: Are you gaining any health when this happens, or is it just deducting packs?

Bugsy 08-18-2021 21:16

Re: Limit of buy item but he take ammo
 
Do some debugging. Log or server_print() the value of g_bComprado[ id ] at each stage in this function (when increased, when value checked); do the same for the cvar value. Somehow you are getting into code that deducts ammo packs. You can figure out all issues on your own once you learn the general principle of debugging.

Code:
    if(itemid==g_itemid_buyhp)     {         if(zp_get_user_ammo_packs(id) < 20)         {             client_print(id, print_chat,"[ZP] Not enough Ammopacks!");             return PLUGIN_HANDLED;         }         else         {             server_print( "g_bComprado[ %d ] = %d , cvar=%d" , id , g_bComprado[ id ] , get_pcvar_num(cvar_limit) );             if( g_bComprado[ id ] < get_pcvar_num(cvar_limit))             {                 server_print( "reached code where ammo packs bought" );                 g_bComprado[ id ] ++;                 set_user_health(id,get_user_health(id)+get_pcvar_num(hpamount));                 zp_set_user_ammo_packs(id, zp_get_user_ammo_packs(id) - 5);                 client_print(id, print_chat,"[ZP] You Bought HP!");             }             else             {                 client_print(id, print_chat,"[ZP] poti cumpara acest item daar odata pe runda");             }         }     }

zollymaN 08-19-2021 05:09

Re: Limit of buy item but he take ammo
 
none of these work, and I also discovered that in fact the limit is once on the map, not on the round...... What's wrong with this plugin?

Bugsy 08-19-2021 08:50

Re: Limit of buy item but he take ammo
 
Are you referring to my code? If so, then the function is not even getting called that is causing your issue.

Napoleon_be 08-19-2021 12:08

Re: Limit of buy item but he take ammo
 
To me it looks like this is your issue, as you're mentioning that u can only use it once a map.

PHP Code:

public event_round_start()
{
    
arraysetg_bComprado33);


Try this

PHP Code:

public event_round_start()
{
    new 
iPlayers[MAX_PLAYERS], iNum;
    
get_players(iPlayersiNum)

    for(new 
iiNumi++) {
        
g_bComprado[iPlayers[i]] = 0;
    }



Bugsy 08-19-2021 13:10

Re: Limit of buy item but he take ammo
 
arrayset() would cover the entire array, I'm not sure how only resetting certain indexes would matter.

CrazY. 08-19-2021 13:14

Re: Limit of buy item but he take ammo
 
I'm not sure what's your zp version, usually you have to return PLUGIN_HANDLED in order to block the user from buying, but you're returning PLUGIN_CONTINUE

Code:

#include <amxmodx>
#include <fun>
#include <zp_apocalypse>

new const item_name[] = "Buy 1000 HP!"
new g_itemid_buyhp
new hpamount
new g_bComprado[ 33 ];
new cvar_limit

public plugin_init()
{
    register_plugin("[ZP] Buy Health Points", "1.0", "T[h]E Dis[as]teR")
   
    hpamount = register_cvar("zp_buyhp_amount", "1000")
    cvar_limit = register_cvar("zp_buyhp_limit", "1")
   
    g_itemid_buyhp = zp_register_extra_item(item_name, 5, ZP_TEAM_ZOMBIE & ZP_TEAM_HUMAN)
   
    register_event("HLTV", "event_round_start", "a", "1=0", "2=0")
}
public event_round_start()
{
    arrayset( g_bComprado, 33, 0 );
}
public zp_extra_item_selected(id,itemid)
{
    if(!is_user_alive(id))
       
    return PLUGIN_HANDLED;
   
    if(itemid==g_itemid_buyhp)
    {
        if(zp_get_user_ammo_packs(id) < 20)
        {
            client_print(id, print_chat,"[ZP] Not enough Ammopacks!");
            return PLUGIN_HANDLED;
        }
        else
        {
            if( g_bComprado[ id ] < get_pcvar_num(cvar_limit))
            {
                g_bComprado[ id ] ++;
                set_user_health(id,get_user_health(id)+get_pcvar_num(hpamount));
                zp_set_user_ammo_packs(id, zp_get_user_ammo_packs(id) - 5);
                client_print(id, print_chat,"[ZP] You Bought HP!");
                return PLUGIN_CONTINUE
            }
            else
            {
                client_print(id, print_chat,"[ZP] poti cumpara acest item daar odata pe runda");
                return PLUGIN_HANDLED
            }
        }
    }
    return PLUGIN_CONTINUE;
}


zollymaN 08-19-2021 16:48

Re: Limit of buy item but he take ammo
 
Quote:

Originally Posted by CrazY. (Post 2755630)
I'm not sure what's your zp version, usually you have to return PLUGIN_HANDLED in order to block the user from buying, but you're returning PLUGIN_CONTINUE

Code:

#include <amxmodx>
#include <fun>
#include <zp_apocalypse>

new const item_name[] = "Buy 1000 HP!"
new g_itemid_buyhp
new hpamount
new g_bComprado[ 33 ];
new cvar_limit

public plugin_init()
{
    register_plugin("[ZP] Buy Health Points", "1.0", "T[h]E Dis[as]teR")
   
    hpamount = register_cvar("zp_buyhp_amount", "1000")
    cvar_limit = register_cvar("zp_buyhp_limit", "1")
   
    g_itemid_buyhp = zp_register_extra_item(item_name, 5, ZP_TEAM_ZOMBIE & ZP_TEAM_HUMAN)
   
    register_event("HLTV", "event_round_start", "a", "1=0", "2=0")
}
public event_round_start()
{
    arrayset( g_bComprado, 33, 0 );
}
public zp_extra_item_selected(id,itemid)
{
    if(!is_user_alive(id))
       
    return PLUGIN_HANDLED;
   
    if(itemid==g_itemid_buyhp)
    {
        if(zp_get_user_ammo_packs(id) < 20)
        {
            client_print(id, print_chat,"[ZP] Not enough Ammopacks!");
            return PLUGIN_HANDLED;
        }
        else
        {
            if( g_bComprado[ id ] < get_pcvar_num(cvar_limit))
            {
                g_bComprado[ id ] ++;
                set_user_health(id,get_user_health(id)+get_pcvar_num(hpamount));
                zp_set_user_ammo_packs(id, zp_get_user_ammo_packs(id) - 5);
                client_print(id, print_chat,"[ZP] You Bought HP!");
                return PLUGIN_CONTINUE
            }
            else
            {
                client_print(id, print_chat,"[ZP] poti cumpara acest item daar odata pe runda");
                return PLUGIN_HANDLED
            }
        }
    }
    return PLUGIN_CONTINUE;
}


I use the version where you updated it mr crazy, that is 4.4, my opinion is that it is the most stable and well developed source of zm :D

I tried the code written by you but it still drops me from ammo packs when I want to buy hp again


All times are GMT -4. The time now is 02:33.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.