health/armor regeneration functions

hi im fairly new to lua and know very little. but anyway i was trying to use a regeneration mod with some armors that when equipped give the player more health/armor, the problem being that it doesn’t also increase their health and armor regen max amount. meaning once they lose the extra health or armor they don’t get it back since the regen mod is the whole way of “healing” in the gamemode.


if ( CLIENT ) then return end

CreateConVar( "healthregen_enabled", 1, FCVAR_ARCHIVE )
CreateConVar( "healthregen_speed", 1, FCVAR_ARCHIVE )
CreateConVar( "healthregen_maxhealth", 100, FCVAR_ARCHIVE )
CreateConVar( "healthregen_delay", 5, FCVAR_ARCHIVE )

local function Think()
	local enabled = GetConVarNumber( "healthregen_enabled" ) > 0
	local speed = 1 / GetConVarNumber( "healthregen_speed" )
	local max = GetConVarNumber( "healthregen_maxhealth" )
	local time = FrameTime()
	
	for _, ply in pairs( player.GetAll() ) do
		if ( ply:Alive() ) then
			local health = ply:Health()
	
			if ( health < ( ply.LastHealth or 0 ) ) then
				ply.HealthRegenNext = CurTime() + GetConVarNumber( "healthregen_delay" )
			end
			
			if ( CurTime() > ( ply.HealthRegenNext or 0 ) && enabled ) then
				ply.HealthRegen = ( ply.HealthRegen or 0 ) + time
			 	if ( ply.HealthRegen >= speed ) then
					local add = math.floor( ply.HealthRegen / speed )
					ply.HealthRegen = ply.HealthRegen - ( add * speed )
					if ( health < max || speed < 0 ) then
						ply:SetHealth( math.min( health + add, max ) )
					end
				end
			end
			
			ply.LastHealth = ply:Health()
		end
	end
end
hook.Add( "Think", "HealthRegen.Think", Think )

i just don’t really know how to create new functions for all 4 of the commands. any help would be greatly appreciated.


CreateConVar( "healthregen_enabled", 1, FCVAR_ARCHIVE )
CreateConVar( "healthregen_speed", 1, FCVAR_ARCHIVE )
CreateConVar( "healthregen_maxhealth", 100, FCVAR_ARCHIVE )
CreateConVar( "healthregen_delay", 5, FCVAR_ARCHIVE )

putting that in GM:Think() is extremely inefficient and you should feel bad.

[lua]

CreateConVar( “healthregen_enabled”, 1, FCVAR_ARCHIVE )
CreateConVar( “healthregen_speed”, 1, FCVAR_ARCHIVE )
CreateConVar( “healthregen_maxhealth”, 120, FCVAR_ARCHIVE )
CreateConVar( “healthregen_maxarmor”, 120, FCVAR_ARCHIVE )
CreateConVar( “healthregen_delay”, 1, FCVAR_ARCHIVE )

local function RegenPlyInitSpawn( ply )
ply:SetMaxHealth( GetConVarNumber( “healthregen_maxhealth” ) )

local tmrstring = tostring( ply ) .."_HealthRegen"
timer.Create( tmrstring, GetConVarNumber( "healthregen_delay" ), 0, function()
	if( !IsValid( ply ) ) then
		timer.Destroy( tmrstring )
		return
	end
	if( !ply:Alive() ) then return end

	ply:SetHealth( math.min( ply:GetMaxHealth(), ply:Health() + GetConVarNumber( "healthregen_speed" ) ) )
	ply:SetArmor( math.min( GetConVarNumber("healthregen_maxarmor"), ply:Armor() + GetConVarNumber( "healthregen_speed" ) ) )
end )

end
hook.Add( “PlayerInitialSpawn”, RegenPlyInitSpawn )

[/lua]

Enjoy~

thats not what i meant… i need functions to use in other scripts to change just that players hp/armor regen. all u made are console commands.

If you want to use those methods in other scripts you need to first make them global methods, not local. Then you’ll need to include this file in those scripts.