Hook for healing?

I’ve been looking through the hooks, but I can’t seem to find any for when a player is healed. I’m trying to make players slow down when they are shot in the legs, but speed back up when they are healed. Any help?

Just speed them back up when they’re healed? It doesn’t happen automatically, only a script would slow them down and heal them, so just edit the script.

I’m not sure if you know what I’m asking. I’m wondering if there’s some sort of hook that is called when a player is healed. Or, if anyone knows of a way to easily detect when a player has been healed.

Use think and compare health values?

Probably a better way though.

Typing this from my phone, so there may be syntax or caps errors:
local _R.Player:oldSetHealth=_R.Player:SetHealth
function _R:Player:SetHealth(var)
hook.Call(“PlayerChangeHealth”, GM, self, var)

That creates a hook called GM:PlayerChangeHealth(player, new health). Or you could just replace the hook.Call line with whatever code you’re going to use.

That’s the thing, I don’t just want to use a think hook…

[editline]13th November 2011[/editline]

That looks like something useful, but I’m really new to Lua, and I like to understand the code. Any chance you can explain what you did there?

pretty sure the first line should be

[lua]local _R.Player.oSetHealth = _R.Player.SetHealth[/lua]

[editline]13th November 2011[/editline]

and a player’s health doesn’t have to change via the SetHealth function, so if I understand correctly, this isn’t what you want

local oldSetHealth = _R.Player:SetHealth();
function _R.Player:SetHealth( var )
hook.Call(“PlayerHealthChanged”, self, var);

return oldSetHealth( var );


Is how he should be doing it, however, the best way of doing this would be comparing health values in a think hook.

Yeah, fail on me for the syntax (spell check was freaking it out so I was more focused on that than anything). But if the player is healed, chances are its by a sent and most sents will use sethealth. Using a think hook would catch all cases, but would also take more of a performance toll. It wouldnt be overly noticable, but it could be problematic with older hardware. You could also add the hook.Call line to PlayerTakeDamage and other health-changing hooks so that it does catch it every time.

As for what the code is doing, its basically overriding the SetHealth function to make it call GM:PlayerHealthChanged whenever it is run.

[editline]13th November 2011[/editline]
Why exactly should it return oldSetHealth instead of just running it? I’m pretty sure SetHealth doesn’t return anything.

lots of more complicated shit is done in a Think hook, checking a player’s health won’t do shit

A think hook checking 2 values would not take a performance toll, not one that would ever cause issues. Also, it’s overwriting the function, calling the hook before doing anything else and then returning how the old function would.

[editline]13th November 2011[/editline]

Uhh, what? The only thing you would even do in a Think hook (in this case), would be comparing 2 values.

[lua]hook.Add(“Think”, “PlayerHealth”, function()
for _,v in pairs(player.GetAll()) do
local Health = v:GetHealth()
v.LastHealth = v.LastHealth or Health
if( Health - v.LastHealth > 0 ) then
hook.Call(“PlayerGainedHealth”, GAMEMODE, v, (Health - v.LastHealth))
hook.Call(“PlayerLostHealth”, GAMEMODE, v, (v.LastHealth - Health))
v.LastHealth = Health

that’s what I’m saying, tons of more complicated and performance affecting shit is done in a think hook, comparing values isn’t something that would hurt

This game isn’t like Call of Duty. You don’t have regeneration. This is like Half Life 2, you have a number that (usually) ranges between 0 and 100. If your health gets regenerated anyhow, it’s a script.

If you have a script that slows a player’s speed when shot in the legs, and their health is renewed, just add the speed update in the same code that regens their health.

Here, try this one.

hook.Add("PlayerScaleDamage","LegSlowdown",function( ply, hitgroup, dmginfo )
if timer.IsTimer("Reset_PlayerSpeed") then return end
ply:SetWalkSpeed( Walkspeed you want)
ply:SetRunSpeed(Runspeed you want)
-- Sets the player speed back on old speed.
timer.Create( "Reset_PlayerSpeed", 1, 0, function()
         if !ply:Health() == ply:GetMaxHealth() then return end
	 ply:SetWalkSpeed( Normal walkspeed you want)
         ply:SetRunSpeed(Normal runspeed you want)
         ply:ChatPrint("You have normal speed again !")

Hope it works for you.