[CODE]function RegenHP( ply )
if ply:Health() <= 99 then
timer.Simple(5,function()
if ply:Health() >= 100 then return end
ply:SetHealth(ply:Health() + 1)
ply:ChatPrint("A magic unicorn healed you 1 health!")
end)
end
end[/CODE]
I can run the script without any errors showing up but it doesn't do anything D:
How/where are you calling your function?
I'm new to lua and this is the second thing that I've attempted to make to learn the basics.
How would I call it? I tried simply using "RegenHP()" on it's own but nothing happens part from errors :3
you need an hook.
[CODE]
function HPTimer(ply)
local HPRegenSec = 2
timer.Create( "HP"..ply:SteamID().."", HPRegenSec, 0, function() if ply:Health() <= 99 then ply:SetHealth(ply:Health() +1 ) ply:ChatPrint("A magic unicorn healed you 1 health!") end )
end
hook.Add( "PlayerInitialSpawn", "HPRegTimer", HPTimer )
function DestroyTimer(ply)
timer.Destroy("HP"..ply:SteamID().."")
end
hook.Add("PlayerDisconnected", "DestroyTimer", DestroyTimer)
[/CODE]
that would be my, untested, noobish way to do it.
You'll get an error because you've made it to expect the player entity as the argument "ply".
If you just call the function all of your methods (ply:SetHealth(), ply:Health()) will fail as ply is nil.
The way I'd encourage you to call it is using an [url=http://wiki.garrysmod.com/page/GM/EntityTakeDamage]EntityTakeDamage[/url] hook and only heal the player when they've took damage.
Since the first parameter of EntityTakeDamage is the entity that took damage, it might not always return a player. You need to ensure the entity is a player by using ent:IsPlayer(), and ending the function if so.
You should also calculate the players true health after the damage is dealt, as when this hook is called the damage will not have been applied to the entity, that's where the second parameter comes in.
It returns a damage structure table, which you can use to get the damage of the attack using dmg:GetDamage(), and so calculate the final health using
[lua]ent:Health() -dmg:GetDamage()[/lua]
to work out whether or not you should start healing them.
Sorry, you need to Log In to post a reply to this thread.