# Heal % of player's health?

Hey guys, I’m messing around with some code here and I’m wondering:

How do I heal a percentage of the player’s health? The current script heals 25 HP to the player that uses the entity, and the number is adjustable, but how would I go about making it heal 50% of the player’s total health instead of a numerical value? If, this IS possible. If not, just say the word.

As always, help appreciated.

If you want it to be total health, ply:GetMaxHealth() * your percentage.

Wow, of course. Why didn’t I see that earlier. Thanks a bunch.

[editline]20th March 2013[/editline]

Ah, now it’s giving me this
“attempt to index global ‘ply’ (a nil value)”
What should I do?

Also, sorry for all these really noob-ish questions.

ply is not defined

How would I define ply?

Yea, I’m sorry, that’s probably a really stupid question with a simple answer.

You should see function ENT:Use(item1, item2), figure out what they are and paste here.

Thanks, I’ll try that.
I’ll probably be back tomorrow with some more questions if I still don’t understand.
Thank for all the help guys.

function healpercent( ply, p ) – p is a num between 0 and 1 so 0.1 is 10 percent
ply:SetHealth( ply:Health() * ( 1 + p ) )
end

healpercent( ply, (tonumber( arg[1] or “50” ))/100)
end)

include(‘shared.lua’)

``````local healthamount = 25
``````

/*---------------------------------------------------------
Name: Initialize

This is the spawn function. It’s called when a client calls the entity to be spawned.
If you want to make your SENT spawnable you need one of these functions to properly create the entity.
ply is the name of the player that is spawning it.
tr is the trace from the player’s eyes.
---------------------------------------------------------*/
function ENT:SpawnFunction(ply, tr)

``````if (!tr.Hit) then return end

local SpawnPos = tr.HitPos + tr.HitNormal * 16

local ent = ents.Create("health_fakitest")
ent:SetPos(SpawnPos)
ent:Spawn()
ent:Activate()

return ent
``````

end

/---------------------------------------------------------
Name: Initialize
---------------------------------------------------------
/
function ENT:Initialize()

``````self.Entity:SetModel("models/items/healthkit.mdl")
self.Entity:PhysicsInit(SOLID_VPHYSICS)
self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
self.Entity:SetSolid(SOLID_VPHYSICS)

self.Entity:SetCollisionGroup(COLLISION_GROUP_WEAPON)

local phys = self.Entity:GetPhysicsObject()

if (phys:IsValid()) then
phys:Wake()
end

self.Entity:SetUseType(SIMPLE_USE)
``````

end

/---------------------------------------------------------
Name: PhysicsCollide
---------------------------------------------------------
/
function ENT:PhysicsCollide(data, physobj)

``````// Play sound on bounce
if (data.Speed &gt; 80 and data.DeltaTime &gt; 0.2) then
self.Entity:EmitSound("Default.ImpactSoft")
end
``````

end

/---------------------------------------------------------
Name: OnTakeDamage
---------------------------------------------------------
/
function ENT:OnTakeDamage(dmginfo)

``````// React physically when shot/getting blown
self.Entity:TakePhysicsDamage(dmginfo)
``````

end

/---------------------------------------------------------
Name: Use
---------------------------------------------------------
/
function ENT:Use(activator, caller)

``````self.Entity:Remove()
``````

if ( activator:IsPlayer() ) then

``````activator:EmitSound("items/smallmedkit1.wav", 500, 100)

if ( activator:Health() &gt;= 100 ) then return end

if ( activator:Health() &gt; 100 - healthamount ) then

activator:SetHealth( 100 )

else

activator:SetHealth( activator:Health() + healthamount )

self.Entity:Remove()

end
``````

end
end[/lua]

Yea sorry I’m retarded I don’t know why I didn’t post it earlier sorry