How to set the health when you die.

Hello.

I’m sorry for the title, but I don’t know how to explain this.

When the player’s health =< 0 i would like to set his health to 15, but if I use this code:




function healthexample()
local ply = LocalPlayer()
local playerhealth = ply:Health()

timer.Create( "healthtimer", 1, 0, function()
	if playerhealth <= 0 then
	hook.Call( "dontdie" )
	end
end )
end

function sethealth()
ply:SetHealth( 15 )
end
hook.Add( "dontdie", "health example", sethealth)



Don’t works, because the player dies.

How can I do this?

Try hook.Add( “Think”, “stuff”, sethealth )

If “Think” doesnt work try
PlayerDeath or PlayerDeathThink

dumb rating for attempting to help

This won’t work since LocalPlayer() only works clientside and player/SetHealth() only works serverside.

it is useless, because there is already an infinite timer.

function sethealth()
timer.Create(“example”, 1, 0, function()
ply:SetHealth( 15 )
end )
end

I forgot it in the discussion.

if the health reaches zero they will die

it can go straight from 100 to 0 and not inbetween, keeping you from intervening

GM/PlayerShouldTakeDamage

better to prevent them taking damage than trying to heal them

[del]I would use the

GM/PlayerHurt hook to detect if the damage done to the player brings their health to 0.

Something like this.[/del]




hook.Add("PlayerHurt", "checkHealth", function( victim, attacker, hLeft, damage )
	if ( victim:IsPlayer() and hLeft <= 0   ) then
		victim:SetHealth(15)
	end
end)



[editline]5th February 2016 at 10:27AM[/editline]

SEE MY POST BELOW

Ehm, no, look here http://wiki.garrysmod.com/page/Entity/SetHealth the color is orange and blue, so it works in client and in server side.

clients can “set” the health, for whatever reason, but that “set” does not transmit to the server; the client can’t tell the server to override its own health; the server will know the real health value

just try it and watch it not work

Actually after looking at the wiki and based upon what you are trying to do I would suggest using

GM/EntityTakeDamage



hook.Add("EntityTakeDamage", "checkHealth", function( ply, dmginfo  )
	if ( ply:IsPlayer() and (ply:Health() - dmginfo:GetDamage())  <= 0   ) then
		ply:SetHealth(15)
	end
end)


You can’t set the health of other entities since this could be easily abused for cheats and the like.

You might have to overwrite it to stop players from being killed though.

Ok, I did it, but it don’t works.

What doesn’t work? What code are you using? Did you try any of the methods suggested in this thread? You can’t simply state it doesn’t work without telling us specifics.

Ok, this can work, but have to be fixed.
Because if I have 100HP, and a bot shoot me with a normal weapon, my hp sets to 15.

“dmginfo:GetDamage()” “ply:Health()” should be switched.
I guess he was wondering if you could figure that out yourself.

Oops, I didn’t even think about whether or not it would work I was trying to give him an example of how you use the damage info.