Health Regeneration

Hi guys; I’d like a simple script that adds 5 health per 15 seconds for every player. I’ve tried it with a timer but it just won’t work!
inb4 link to other work - I’ve tried other health regeneration scripts - they dont work.

Thanks in advance

I think this should help you:

No it won’t. That is old syntax.


local function HPRegen()
	for k,v in pairs(player.GetAll()) do 
		local hpdif = v:GetMaxHealth()-v:Health()
		if hpdif >= 5 then
			v:SetHealth(v:Health()+5)
		elseif hpdif < 5 then
			v:SetHealth(v:Health()+hpdif)
		end
	end
end

timer.Simple(15,HPRegen)


This should work.

[ERROR] lua/autorun/regen.lua:3: attempt to call method ‘GetMaxHealth’ (a nil value)

  1. unknown - lua/autorun/regen.lua:3

Timer Failed! [Simple][@lua/autorun/regen.lua (line 12)]

returned this error

replace getmaxhealth with 100

GetMaxHealth doesn’t work in single player, if you want to use this in single player change GetMaxHealth to 100, but if you are going to use it on a server/gamemode keep it the same.

No errors now, however its not actually regenerating anybody’s health - any ideas?
(Multiplayer)
Another quick question while I’m here: SteamCMD has to be used to get content for CSS and TF2 for SRCDS - correct? Well I did what the tutorial on the Steam website says, and it downloads CSS; I then move it to the orangebox folder. However no CSS props work…

Where did you put the script?

-snip-

Same as post above, posted at same time.

lua/autorun

Try lua/autorun/server. Also once you put it there, try putting the GetMaxHealths back as they shouldn’t error if the code is in there.

Did so and its still not working - sorry to keep bothering you guys

I’ve tested this on my own multiplayer:

garrysmod\lua\autorun\server\hpregen.lua

and the code inside is:


local function HPRegen()
	for k,v in pairs(player.GetAll()) do 
		local hpdif = v:GetMaxHealth()-v:Health()
		if hpdif >= 5 then
			v:SetHealth(v:Health()+5)
		elseif hpdif < 5 then
			v:SetHealth(v:Health()+hpdif)
		end
	end
end

timer.Simple(15,HPRegen)

This worked absolutely fine? Have you tried restarting GMod after putting it in?

I restarted the server and it still wont function. Let me try again.
Edit:
Working this time - Thanks so much guys, appreciate it a lot.

Once you get it working, make sure to check if the player has hp <= 0, if true then kill the player (so the players hp doesn’t go under 0)

Doesn’t timer.Simple run a function only once after a given delay? Unless its behaviour was changed, I don’t see how this would possibly work.

[editline]asdf[/editline]

In the event that the above code doesn’t actually work because you didn’t wait more than 30 seconds to see if the function is actually called periodically, here’s how to make a periodic timer:


timer.Create("playerHpRegen", 15, 0, HPRegen)


DF_READ = 1
DF_WRITE = 2
DF_BOTH = 3

function DataField( ent, key, name, force, fl )
    fl = fl or DF_BOTH
    
    if bit.band( fl, DF_READ ) ~= 0 then
        ent[ "Get" .. name ] = function( e )
            local res = e:GetSaveTable( )[ key ]
        
            if force == FORCE_NUMBER then
                return tonumber( res ) or 0
            elseif force == FORCE_STRING then
                return tostring( res )
            elseif force == FORCE_BOOL then
                return tobool( res )
            else
                return res
            end
        end
    end
    
    if bit.band( fl, DF_WRITE ) ~= 0 then
        ent[ "Set" .. name ] = function( e, v )
            if force == FORCE_NUMBER then
                e:SetSaveValue( key, tonumber( v ) or 0 )
            elseif force == FORCE_STRING then
                e:SetSaveValue( key, tostring( v ) )
            elseif force == FORCE_BOOL then
                e:SetSaveValue( key, tobool( v ) )
            else
                e:SetSaveValue( key, v )
            end
        end
    end
end

DataField( _R.Player, "m_flLastDamageTime", "LastDamagedTime", FORCE_NUMBER )

local function Tick( )
  for k, v in ipairs( player.GetAll( ) ) do
    if v:Alive( ) and v:Health( ) < 100 and v:GetLastDamagedTime( ) <= -15 then
      v:SetHealth( math.min( 100, v:Health( ) + 5 ) )
      v:SetLastDamagedTime( 0 )
    end
  end
end

hook.Add( "Tick", "Regeneration.Tick", Tick )


If you don’t mind a more complicated solution.

This would make it so that you regenerate only when you haven’t taken damage for 15 seconds first.

How easy would this be to turn into a medigun type swep?