Raised This Month: $12 Target: $400
 3% 

Solved [CS:GO] trying to give health per kill


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
JoaoRodrigoGamer
Junior Member
Join Date: Jul 2019
Location: Portugal
Old 07-21-2019 , 20:40   [CS:GO] trying to give health per kill
Reply With Quote #1

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?

Last edited by JoaoRodrigoGamer; 07-22-2019 at 08:57. Reason: Solved
JoaoRodrigoGamer is offline
iskenderkebab33
Senior Member
Join Date: Jun 2018
Old 07-22-2019 , 01:47   Re: [CS:GO] trying to give heath per kill
Reply With Quote #2

i think this can help you: https://forums.alliedmods.net/showpo...16&postcount=7
iskenderkebab33 is offline
JoaoRodrigoGamer
Junior Member
Join Date: Jul 2019
Location: Portugal
Old 07-22-2019 , 04:51   Re: [CS:GO] trying to give heath per kill
Reply With Quote #3

Quote:
Originally Posted by iskenderkebab33 View Post
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...
JoaoRodrigoGamer is offline
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 07-22-2019 , 07:10   Re: [CS:GO] trying to give heath per kill
Reply With Quote #4

Quote:
Originally Posted by JoaoRodrigoGamer View Post
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?
__________________
8guawong is offline
JoaoRodrigoGamer
Junior Member
Join Date: Jul 2019
Location: Portugal
Old 07-22-2019 , 07:29   Re: [CS:GO] trying to give heath per kill
Reply With Quote #5

Quote:
Originally Posted by 8guawong View Post
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.
JoaoRodrigoGamer is offline
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 07-22-2019 , 08:07   Re: [CS:GO] trying to give heath per kill
Reply With Quote #6

Maybe change
PHP Code:
public OnClientPutInServer(client
To
PHP Code:
public OnPluginStart() 
__________________
8guawong is offline
JoaoRodrigoGamer
Junior Member
Join Date: Jul 2019
Location: Portugal
Old 07-22-2019 , 08:30   Re: [CS:GO] trying to give heath per kill
Reply With Quote #7

Quote:
Originally Posted by 8guawong View Post
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?
JoaoRodrigoGamer is offline
Cruze
Veteran Member
Join Date: May 2017
Old 07-22-2019 , 08:40   Re: [CS:GO] trying to give heath per kill
Reply With Quote #8

Quote:
Originally Posted by JoaoRodrigoGamer View Post
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.
__________________
Taking paid private requests! Contact me
Cruze is offline
JoaoRodrigoGamer
Junior Member
Join Date: Jul 2019
Location: Portugal
Old 07-22-2019 , 08:56   Re: [CS:GO] trying to give heath per kill
Reply With Quote #9

Quote:
Originally Posted by Cruze View Post
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!
JoaoRodrigoGamer is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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