Need help creating something to instantly regenerate health every 3-4 seconds

As the title suggests, I need assistance in creating something that instantly regenerates your health in 3 or 4 seconds. I tried using timers but I couldn’t get them to work, I am rather new to Lua and don’t know what could be wrong as no errors are spit into console either. Any help would be greatly appreciated!

What do you mean by “something?” Like an entity?

Regenerate… Just use the EntityTakeDamageHook and create a timer.simple wo runs the ply:MaxHealth stuff…

I am referring to the player; to go into depth, let’s say he gets damaged. In a couple seconds he instantly regenerates to a preset value. The something I am referring to is a SWEP, that when held, does this, to elaborate. The SWEP I am working on works perfectly fine but I still need to implement this feature.

Should this happen when the SWEP is equipped or just on the player? If the former, should it be automatic or require them to hold on a fire key?

it should happen passively while the swep is equipped


local flRate = 1
local iAmount = 1

-- Serverside
hook.Add("Think", "HealthRegen", function()
	local tPlayers = player.GetAll()
	local flCurTime = CurTime()
	
	for i = 1, #tPlayers do
		local pPlayer = tPlayers*
		
		-- Heal if they have the weapon passively
		if (pPlayer:HasWeapon("weapon_whatever")) then
			local flNextRegen = pPlayer.m_flNextRegen
			
			-- Regen time hasn't been set yet or it's time for another heal
			if (flNextRegen == nil or flNextRegen <= flCurTime) then
				local iHealth = pPlayer:Health()
				local iNewHealth = math.min(iHealth + iAmount, pPlayer:GetMaxHealth())
				
				-- If the player isn't at max health
				if (iHealth ~= iNewHealth) then
					pPlayer:SetHealth(iNewHealth)
				end
				
				-- Heal again after flRate seconds
				pPlayer.m_flNextRegen = flCurTime + flRate
			end
		end
	end
end)

Oh! Thank you so much. I’ll test that out right now!

Edit: works excellently! Thanks again! I’ll try and make it regenerate armour as well now.

[editline]25th August 2017[/editline]

I’ve gotten it to regenerate armor, but is there a way to make it only regenerate while the SWEP is active? It’s healing me while I have another weapon out.

[editline]25th August 2017[/editline]

interesting… i have two different SWEPS which use the script, and one stops functioning and one starts working after I make an edit to the code…

Use GetActiveWeapon than HasWeapon

i’ve tried doing


 if (pPlayer:GetActiveWeapon() == "weapon_gladius") then 

but that didn’t work for me, I wouldn’t heal at all. I’m still very new to this so I don’t know what I did wrong here

GetActiveWeapon returns the entity, not the class you are comparing, add :GetClass() to that GetActiveWeapon to make the entity return it class

i’ll try that, thanks

[editline]25th August 2017[/editline]

I’ve tried


if (pPlayer:GetActiveWeapon():GetActiveClass():GetClass() == "weapon_gladius") then

to no avail either


if (pPlayer:GetActiveWeapon():GetClass() == "weapon_gladius") then

There is no such thing as GetActiveClass. You’re looking to direct apply

Entity:GetClass to the entity itself (in this case the player’s active weapon – this is also exampled in the description on the wiki)

whoopsies i confused GetClass and GetActiveWeapon I kinda squished them together

[editline]25th August 2017[/editline]


if (pPlayer:GetActiveWeapon():GetClass() == "weapon_praetorian_gladius") then

also doesn’t work for me either

[editline]25th August 2017[/editline]

Do I have to have the healing script under some kind of function like SWEP.Think()?

That will only work if the weapon is out.

[editline]26th August 2017[/editline]

In which case, you should use SWEP:Think. You described originally, though, that you wanted it to work passively.

That was poor wording on my part, I meant it should work passively while the weapon is out. I’ve tried SWEP:Think to constantly set armor/health to 100, however due to the nature of it, it instantly did so every tick and thus the wearer became practically invincible.

You can use a cooldown system. Where you have a variable on the SWEP which you define in the SWEP:Think() if its nil and the when its less than CurTime() the code runs again



function SWEP:Think()
      if !self.cooldown then self.cooldown = CurTime() -1 end

      if self.cooldown < CurTime() then
           -- Run Code
           self.cooldown = CurTime() + 10 -- How many seconds you want it not to run the code
      end
end


urg why can’t I use tabs

Because tabs are shit.

You should put the cooldown on the owner, not the weapon.

[editline]26th August 2017[/editline]

Also, there’s no need to do that if cooldown doesn’t exist: just pair the if-statements.

why not on the weapon? I don’t see why it needs to be on the player when we are running it of a variable which will only exist on the weapon. And so I don’t want the variable to carry on existing when the weapon is gone. Or is there something that I don’t know

and the if statements is style of code just out of habit