AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved [CS:GO] trying to give health per kill (https://forums.alliedmods.net/showthread.php?t=317628)

JoaoRodrigoGamer 07-21-2019 20:40

[CS:GO] trying to give health per kill
 
I was searching the web for a base to write my plugin on and I found this piece of code:
Code:

public Action OnPlayerDeath(Handle event, char[] name, bool dontBroadcast)
{
        int client = GetClientOfUserId(GetEventInt(event, "attacker"));
        int dead = GetClientOfUserId(GetEventInt(event, "userid"));
        if (GetConVarBool(g_Cvar_HealthRegenEnabled))
        {
                if (HasClientFlag(client, ADMFLAG_RESERVATION))
                {
                        int OldHealth = GetEntProp(client, Prop_Send, "m_iHealth", 4, 0);
                        if (dead != client)
                        {
                                if (GetClientTeam(client) > 1)
                                {                               
                                        g_iHealthRegenedQuantity = GetConVarInt(g_Cvar_HealthRegenedQuantity);
                                        g_iMaxHealth = GetConVarInt(g_Cvar_MaxHealthQuantity);
                                        if (g_iHealthRegenedQuantity + OldHealth > g_iMaxHealth)
                                        {       
                                                SetEntProp(client, Prop_Send, "m_iHealth", g_iMaxHealth, 4, 0);
                                        }
                                        else
                                        {
                                                SetEntProp(client, Prop_Send, "m_iHealth", OldHealth + g_iHealthRegenedQuantity, 4, 0);
                                        }
                                }
                        }
                }
        }
}

After editing it to my needs, the code looked like this:

Code:

public Action OnPlayerDeath(Handle event, char[] name, bool dontBroadcast)
{
        int client = GetClientOfUserId(GetEventInt(event, "attacker"));
        int dead = GetClientOfUserId(GetEventInt(event, "userid"));
        if (CheckCommandAccess(client, "", ADMFLAG_NEEDED, true))
        {
                int OldHealth = GetEntProp(client, Prop_Send, "m_iHealth", 4, 0);
                if (dead != client)
                {
                        if (GetClientTeam(client) > 1)
                        {                               
                                int g_iHealthRegenedQuantity = 10;
                                int g_iMaxHealth = 150;
                                if (g_iHealthRegenedQuantity + OldHealth > g_iMaxHealth)
                                {       
                                        SetEntProp(client, Prop_Send, "m_iHealth", g_iMaxHealth, 4, 0);
                                }
                                else
                                {
                                        SetEntProp(client, Prop_Send, "m_iHealth", (OldHealth + g_iHealthRegenedQuantity), 4, 0);
                                }
                        }
                }
        }
}

My problem is that when someone is killed, the attacker does not get more 10 HP as intended.
Basically, this is the scheme:
- When someone is killed, the attacker gets +10 HP;
- The max health should be 150;

Can anyone explain what am I doing wrong?

iskenderkebab33 07-22-2019 01:47

Re: [CS:GO] trying to give heath per kill
 
i think this can help you: https://forums.alliedmods.net/showpo...16&postcount=7

JoaoRodrigoGamer 07-22-2019 04:51

Re: [CS:GO] trying to give heath per kill
 
Quote:

Originally Posted by iskenderkebab33 (Post 2660129)

thx, I had forgotten to hook the player death.
Now I can detect when the victim dies, but instead of giving +10HP it is giving +30, +40...

Code:

public OnClientPutInServer(client)
{
        HookEvent("player_death", OnPlayerDeath);
}
public Action OnPlayerDeath(Handle event, char[] name, bool dontBroadcast)
{
        int attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
        if (CheckCommandAccess(attacker, "", ADMFLAG_NEEDED, true))
        {
                int health = GetEntProp(attacker, Prop_Send, "m_iHealth");
                PrintToConsole(attacker, "Health: %i", health);
                health = health + 10;
                SetEntityHealth(attacker, health);
                if(health > 150)
                {
                        SetEntityHealth(attacker, 150);
                }
        }
        return Plugin_Continue;
}

When I print the health int to console I get
Code:

Health: 100
Health: 110
Health: 120

I think it is giving +10HP per player in the server, but I'm not sure why...

8guawong 07-22-2019 07:10

Re: [CS:GO] trying to give heath per kill
 
Quote:

Originally Posted by JoaoRodrigoGamer (Post 2660137)
thx, I had forgotten to hook the player death.
Now I can detect when the victim dies, but instead of giving +10HP it is giving +30, +40...

Code:

public OnClientPutInServer(client)
{
        HookEvent("player_death", OnPlayerDeath);
}
public Action OnPlayerDeath(Handle event, char[] name, bool dontBroadcast)
{
        int attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
        if (CheckCommandAccess(attacker, "", ADMFLAG_NEEDED, true))
        {
                int health = GetEntProp(attacker, Prop_Send, "m_iHealth");
                PrintToConsole(attacker, "Health: %i", health);
                health = health + 10;
                SetEntityHealth(attacker, health);
                if(health > 150)
                {
                        SetEntityHealth(attacker, 150);
                }
        }
        return Plugin_Continue;
}

When I print the health int to console I get
Code:

Health: 100
Health: 110
Health: 120

I think it is giving +10HP per player in the server, but I'm not sure why...

Console output looks fine to me
Where do you see +30 +40?

JoaoRodrigoGamer 07-22-2019 07:29

Re: [CS:GO] trying to give heath per kill
 
Quote:

Originally Posted by 8guawong (Post 2660156)
Console output looks fine to me
Where do you see +30 +40?

Probably I didn't explain myself to well.

When I was testing the code, I killed one single enemy but instead of getting +10HP it looks like it was regenerating. That console output was for killing one enemy only.

8guawong 07-22-2019 08:07

Re: [CS:GO] trying to give heath per kill
 
Maybe change
PHP Code:

public OnClientPutInServer(client

To
PHP Code:

public OnPluginStart() 


JoaoRodrigoGamer 07-22-2019 08:30

Re: [CS:GO] trying to give heath per kill
 
Quote:

Originally Posted by 8guawong (Post 2660167)
Maybe change
PHP Code:

public OnClientPutInServer(client

To
PHP Code:

public OnPluginStart() 


That worked!
Can you explain or link to anything that explains the difference of behavior of the HookEvent() in the OnPluginStart vs. OnClientPutInServer?

Cruze 07-22-2019 08:40

Re: [CS:GO] trying to give heath per kill
 
Quote:

Originally Posted by JoaoRodrigoGamer (Post 2660169)
That worked!
Can you explain or link to anything that explains the difference of behavior of the HookEvent() in the OnPluginStart vs. OnClientPutInServer?

OnClientPutInServer is called everytime any player is connected to your server so PlayerDeath was getting hooked everytime anyone joined which will most probably cause problem whereas OnPluginStart is called when plugin runs for the first time.

JoaoRodrigoGamer 07-22-2019 08:56

Re: [CS:GO] trying to give heath per kill
 
Quote:

Originally Posted by Cruze (Post 2660172)
OnClientPutInServer is called everytime any player is connected to your server so PlayerDeath was getting hooked everytime anyone joined which will most probably cause problem whereas OnPluginStart is called when plugin runs for the first time.

That makes sense. Thanks for the help everyone!


All times are GMT -4. The time now is 11:23.

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