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

Solved [CS:GO] give player X health every X seconds


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
iskenderkebab33
Senior Member
Join Date: Jun 2018
Old 07-31-2018 , 12:17   [CS:GO] give player X health every X seconds
Reply With Quote #1

Hey! i have a problem with my code which reason i don't know...

code must do: give every player +10 HP every 30 seconds

my try:

PHP Code:
#include <sourcemod>
#include <sdkhooks>
#include <sdktools>

#pragma semicolon 1
#pragma newdecls required

public void OnPluginStart()
{
    
HookEvent("round_start"Event_RoundStart);
}

public 
Action Event_RoundStart(Event hEvent, const char[] sNamebool bDontBroadcast)
{
    
CreateTimer(30.0Timer_HealthINVALID_HANDLETIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
    
    return 
Plugin_Continue;
}

public 
Action Timer_Health(Handle hTimer)
{
    
int client GetClientUserId(client);

    
int health GetEntProp(clientProp_Send"m_iHealth");
        
health += 10;
    
    return 
Plugin_Continue;

any help is welcome <3

Last edited by iskenderkebab33; 11-03-2018 at 17:28.
iskenderkebab33 is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 07-31-2018 , 12:54   Re: [CS:GO] give player X health every X seconds
Reply With Quote #2

First, something that annoys me. You include sdkhooks, but don't even use it.

Line 15, replace "INVALID_HANDLE" with "_".

Line 22, that basically tells me: get userid from nothing. You could get the userid and pass it as third param on line 15, but that means you have to GetClientOfUserid aswell in callback.

Line 23, there's nothing really wrong here, just want to point out that the int "health" is EXACTLY the same as "GetEntProp(client, Prop_Send, "m_iHealth");"

Line 24, here comes a problem: we first have to get the client's health with GetEntProp, then we have to set it with SetEntProp, or SetEntityHealth (both of which works as good as eachother).

If you're too lazy to read plain english, here's what it should look like.

PHP Code:
#include <sourcemod>
#include <sdktools>

#pragma semicolon 1

public void OnPluginStart()
{
    
HookEvent("round_start"Event_RoundStart);
}

public 
Action Event_RoundStart(Event hEvent, const char[] sNamebool bDontBroadcast)
{
    
CreateTimer(30.0Timer_Health_TIMER_REPEAT TIMER_FLAG_NO_MAPCHANGE);
    
    return 
Plugin_Continue;
}

public 
Action Timer_Health(Handle hTimer)
{
    for (
int i 1<= MaxClientsi++)
    {
        
int iHealth GetClientHealth(i) + 10;
        
        
SetEntityHealth(iiHealth);
    }
    
    return 
Plugin_Continue;

mug1wara is offline
iskenderkebab33
Senior Member
Join Date: Jun 2018
Old 07-31-2018 , 13:00   Re: [CS:GO] give player X health every X seconds
Reply With Quote #3

Quote:
Originally Posted by mug1wara View Post
First, something that annoys me. You include sdkhooks, but don't even use it.

Line 15, replace "INVALID_HANDLE" with "_".

Line 22, that basically tells me: get userid from nothing. You could get the userid and pass it as third param on line 15, but that means you have to GetClientOfUserid aswell in callback.

Line 23, there's nothing really wrong here, just want to point out that the int "health" is EXACTLY the same as "GetEntProp(client, Prop_Send, "m_iHealth");"

Line 24, here comes a problem: we first have to get the client's health with GetEntProp, then we have to set it with SetEntProp, or SetEntityHealth (both of which works as good as eachother).

If you're too lazy to read plain english, here's what it should look like.

PHP Code:
#include <sourcemod>
#include <sdktools>

#pragma semicolon 1

public void OnPluginStart()
{
    
HookEvent("round_start"Event_RoundStart);
}

public 
Action Event_RoundStart(Event hEvent, const char[] sNamebool bDontBroadcast)
{
    
CreateTimer(30.0Timer_Health_TIMER_REPEAT TIMER_FLAG_NO_MAPCHANGE);
    
    return 
Plugin_Continue;
}

public 
Action Timer_Health(Handle hTimer)
{
    for (
int i 1<= MaxClientsi++)
    {
        
int iHealth GetClientHealth(i) + 10;
        
        
SetEntityHealth(iiHealth);
    }
    
    return 
Plugin_Continue;

thank you, working.
iskenderkebab33 is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 07-31-2018 , 13:11   Re: [CS:GO] give player X health every X seconds
Reply With Quote #4

Quote:
Originally Posted by mug1wara View Post
First, something that annoys me. You include sdkhooks, but don't even use it.

Line 15, replace "INVALID_HANDLE" with "_".

Line 22, that basically tells me: get userid from nothing. You could get the userid and pass it as third param on line 15, but that means you have to GetClientOfUserid aswell in callback.

Line 23, there's nothing really wrong here, just want to point out that the int "health" is EXACTLY the same as "GetEntProp(client, Prop_Send, "m_iHealth");"

Line 24, here comes a problem: we first have to get the client's health with GetEntProp, then we have to set it with SetEntProp, or SetEntityHealth (both of which works as good as eachother).

If you're too lazy to read plain english, here's what it should look like.

PHP Code:
#include <sourcemod>
#include <sdktools>

#pragma semicolon 1

public void OnPluginStart()
{
    
HookEvent("round_start"Event_RoundStart);
}

public 
Action Event_RoundStart(Event hEvent, const char[] sNamebool bDontBroadcast)
{
    
CreateTimer(30.0Timer_Health_TIMER_REPEAT TIMER_FLAG_NO_MAPCHANGE);
    
    return 
Plugin_Continue;
}

public 
Action Timer_Health(Handle hTimer)
{
    for (
int i 1<= MaxClientsi++)
    {
        
int iHealth GetClientHealth(i) + 10;
        
        
SetEntityHealth(iiHealth);
    }
    
    return 
Plugin_Continue;

I feel bad for the server for having to store infinite size errors because you don't check IsClientInGame when looping all POSSIBLE players.

Also, the amount of health you will earn will increase by 10 every round. This means that when 30 rounds pass, each 30 seconds you will get 300 HP.

And I have no clue what happens when you heal a dead player nor want to.

The code I attached here solves the issues.
Code:
#include <sourcemod>
#include <sdktools>

#pragma semicolon 1

new Handle:hTimerHealth = INVALID_HANDLE;

public void OnPluginStart()
{
    HookEvent("round_start", Event_RoundStart);
}

public Action Event_RoundStart(Event hEvent, const char[] sName, bool bDontBroadcast)
{
    if(hTimerHealth != INVALID_HANDLE)
    {
        CloseHandle(hTimerHealth);
        hTimerHealth = INVALID_HANDLE;
    }
    hTimerHealth = CreateTimer(30.0, Timer_Health, _, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE);
    
    return Plugin_Continue;
}

public Action Timer_Health(Handle hTimer)
{
    for (int i = 1; i <= MaxClients; i++)
    {
        if(!IsClientInGame(i))
            continue;
            
        else if(!IsPlayerAlive(i))
            continue;

        int iHealth = GetClientHealth(i) + 10;
        
        SetEntityHealth(i, iHealth);
    }
    
    return Plugin_Continue;
}
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334

Last edited by eyal282; 07-31-2018 at 13:25.
eyal282 is offline
micapat
Veteran Member
Join Date: Feb 2010
Location: Nyuu, nyuu (France).
Old 07-31-2018 , 13:19   Re: [CS:GO] give player X health every X seconds
Reply With Quote #5

It would certainly be nice to check if the player is alive too
__________________
micapat is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 07-31-2018 , 13:24   Re: [CS:GO] give player X health every X seconds
Reply With Quote #6

Quote:
Originally Posted by micapat View Post
It would certainly be nice to check if the player is alive too
Face palm thanks.
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 07-31-2018 , 13:31   Re: [CS:GO] give player X health every X seconds
Reply With Quote #7

He never told me to check if the client was valid :p
mug1wara is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 07-31-2018 , 14:24   Re: [CS:GO] give player X health every X seconds
Reply With Quote #8

SDKTools is not needed.

PHP Code:
#include <sourcemod>
#pragma semicolon 1
#pragma newdecls required

public void OnPluginStart()
{
    
HookEvent("round_start"eEventRoundStart);
}

public 
void eEventRoundStart(Event event, const char[] namebool dontBroadcast)
{
    
CreateTimer(30.0tTimerHeal_TIMER_FLAG_NO_MAPCHANGE|TIMER_REPEAT);
}

public 
Action tTimerHeal(Handle timer)
{
    for (
int iPlayer 1iPlayer <= MaxClientsiPlayer++)
    {
        if (
bIsValidClient(iPlayer))
        {
            
int iHealth GetClientHealth(iPlayer) + 10;
            
SetEntityHealth(iPlayeriHealth);
        }
    }
}

stock bool bIsValidClient(int client)
{
    return 
client && client <= MaxClients && IsClientInGame(client) && IsPlayerAlive(client) && !IsClientInKickQueue(client);

Attached Files
File Type: sp Get Plugin or Get Source (hp_regen.sp - 215 views - 734 Bytes)
__________________

Last edited by Psyk0tik; 07-31-2018 at 14:25.
Psyk0tik is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 07-31-2018 , 14:28   Re: [CS:GO] give player X health every X seconds
Reply With Quote #9

Yep, noticed. Thought GetClientHealth / SetEntityHealth was a part of sdktools.
mug1wara is offline
LenHard
Senior Member
Join Date: Jan 2016
Old 08-01-2018 , 06:14   Re: [CS:GO] give player X health every X seconds
Reply With Quote #10

Quote:
Originally Posted by mug1wara View Post
He never told me to check if the client was valid :p
You should always check no matter what's the reason, it's really a bad practice to not check. It'll prevent you from making that mistake in the future and also prevent errors to those whom you're helping.

Quote:
Originally Posted by Crasher_3637 View Post
SDKTools is not needed.

PHP Code:
#include <sourcemod>
#pragma semicolon 1
#pragma newdecls required

public void OnPluginStart()
{
    
HookEvent("round_start"eEventRoundStart);
}

public 
void eEventRoundStart(Event event, const char[] namebool dontBroadcast)
{
    
CreateTimer(30.0tTimerHeal_TIMER_FLAG_NO_MAPCHANGE|TIMER_REPEAT);
}

public 
Action tTimerHeal(Handle timer)
{
    for (
int iPlayer 1iPlayer <= MaxClientsiPlayer++)
    {
        if (
bIsValidClient(iPlayer))
        {
            
int iHealth GetClientHealth(iPlayer) + 10;
            
SetEntityHealth(iPlayeriHealth);
        }
    }
}

stock bool bIsValidClient(int client)
{
    return 
client && client <= MaxClients && IsClientInGame(client) && IsPlayerAlive(client) && !IsClientInKickQueue(client);

If I'm not wrong, that'd cause a timer to be set every round start causing duplicating timers. Add a integer of the round and stop the timer.

PHP Code:
int gI_Round;

public 
void eEventRoundStart(Event hEvent, const char[] sEventNamebool bDontBroadcast)
{
    ++
gI_Round;
    
CreateTimer(30.0Timer_HealgI_RoundTIMER_FLAG_NO_MAPCHANGE|TIMER_REPEAT);
}

public 
Action Timer_Heal(Handle hTimerany iRound)
{
    if (
gI_Round == iRound)
    {
        for (
int i 1<= MaxClientsi++) 
            if (
IsClientInGame(i) && IsPlayerAlive(i)) 
                
SetEntityHealth(iGetClientHealth(i) + 10); 
        return 
Plugin_Continue;
    }
    else return 
Plugin_Stop;

__________________

Last edited by LenHard; 08-01-2018 at 19:56.
LenHard is offline
Reply



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 02:50.


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