Thread: Need help!!
View Single Post
zmd94
Veteran Member
Join Date: Nov 2013
Location: Malaysia (9w2zow).
Old 01-02-2015 , 02:19   Re: Need help!!
Reply With Quote #6

First of all, please use php tag to show your code:
PHP Code:
public cmd_hp(idlevelcid)
{
    if (!
cmd_access(idlevelcid3))
        return 
PLUGIN_HANDLED

    
new Arg1[24]
    new 
Arg2[4]

    
//Get the command arguments from the console
    
read_argv(1Arg123)
    
read_argv(2Arg23)

    
//Convert the health from a string to a number
    
new Health str_to_num(Arg2)

    
//Is the first character the @ symbol?
    
if (Arg1[0] == '@')
    {
        new 
Team 0
        
//Check which team was specified.
        //Note that we start from [1], this is okay
        // it just means the @ isn't included
        
if (equali(Arg1[1], "CT"))
        {
            
Team 2
        

        else if (
equali(Arg1[1], "T")) 
        {
            
Team 1
        
}
        
        new 
players[32], num
        
//This function will fill the players[32] variable
        // with valid player ids. num will contain the number
        // of players that are valid.
        
get_players(playersnum)
        
        new 
i
        
for (i=0i<numi++)
        {
            if (!
Team)
            {
                
//Set this player's health
                
set_user_health(players[i], Health)
            } 
            else 
            {
                if (
get_user_team(players[i]) == Team)
                {
                    
set_user_health(players[i], Health)
                }
            }
        }
    } 
    else 
    {
        
//finds a player id that matches the partial name given
        //the 1 means that it will not target the player if he
        // has immunity access
        
new player cmd_target(idArg11)
        if (!
player)
        {
            
//this will print a message to the user who tried the command
            //The format for this command is called "format()" style,
            // where the first string formats the message according
            // to any number of following parameters.
            // %s means a string
            // %d or %i means an integer
            // %f means a float
            // so "Hello %s, I am %d years old" will
            // require a string and integer to follow
            
console_print(id"Sorry, player %s could not be found or targetted!"Arg1)
            return 
PLUGIN_HANDLED
        

        else 
        {
            
set_user_health(playerHealth)
        }
    }

    return 
PLUGIN_HANDLED

native read_argv(id, output[], len) is used to retrieves argument of client command. So, for example:

amx_freeHP zmd94 90:

1. zmd94 = is the first argument.
2. 90 = is the second argument

For second question: why we need to convert the health from a string to a number?
PHP Code:
new Health str_to_num(Arg2
This is because we want to set player health by using native set_user_health(index, health). The second param which is "health" can only read a number. So, if you don't convert the health from a string to a number, it will give you an error.

Lastly:
PHP Code:
    //Is the first character the @ symbol?
    
if (Arg1[0] == '@')
    {
        new 
Team 0
        
//Check which team was specified.
        //Note that we start from [1], this is okay
        // it just means the @ isn't included
        
if (equali(Arg1[1], "CT"))
        {
            
Team 2
        

        else if (
equali(Arg1[1], "T")) 
        {
            
Team 1
        

This code will working such this.

1. If you write amx_freeHP @ 100 = It will give all players free 100 health.
2. If you write amx_freeHP @CT 100 = It will give all counter-terrorist free 100 health.
3. If you write amx_freeHP @T 100 = It will give all terrorist free 100 health.

If I'm wrong, please correct me. ;)

Last edited by zmd94; 01-02-2015 at 02:25.
zmd94 is offline