HP Increase via regen only if player is at 100 HP or above

So I’ve tried a few things for this. I’m looking to make an HP “increase” script that increases the player’s HP to 130. The issue is, you cannot just use ply:SetHealth(130) because players can abuse this, by simply un-equipping it and re-equipping it.

For context, this is a pointshop item.

So, I tried this:



function ITEM:Think(ply, modifications)
        if SERVER then
	        ply:SetMaxHealth( 130 )
	        ply:SetHealth( math.Clamp( ply:Health() + 0.001, 100, 130 ) )
        end
end


It regens to 130, which is the plan. The issue here is that I want it to only regen if the player is above 100 hp. I have no idea how to do this, and for some magic reason I thought clamping it to 100 would allow the player to die and go below 100 but no, it does not, which I guess makes sense since I used it wrong.

What would be a way to edit this so that it only regens the player slowly to 130, if they are above 100?

Thanks in advance.

Calling ply:SetMaxHealth() in a think hook is not the best idea.

Here’s a better idea:



function ITEM:Think( ply, modifications )
	if ( SERVER && ply:Health() >= 100 ) then
		ply:SetHealth( math.min( ply:Health() + 0.001, math.floor( ply:GetMaxHealth() * 1.3 ) ) )
	end
end

You don’t need the math.Clamp since the minimum health is at least 100 every single time.
So we use math.min, which returns the MINIMUM value out of given two.

We use ply:GetMaxHealth() * 1.3 to get the 130 and to also support any modifications to max health other scripts or the gamemode might have made. So, lets say, if max health for given gamemode is 200, the max overheal would be 200 * 1.3 = 260

We math.floor the max health to keep it a whole value, not something like 159.9 if max health is not a “nice value” but something like 123. You might want to change it to math.ceil, but that’s up to you.

What you also could do is something like this:



function ITEM:Think( ply, modifications )
	if ( !ply.LastHealthIncrease ) then ply.LastHealthIncrease = 0 end

	if ( SERVER && ply:Health() >= 100 && ply.LastHealthIncrease + 1 < CurTime() ) then
		ply:SetHealth( math.min( ply:Health() + 1, math.floor( ply:GetMaxHealth() * 1.3 ) ) )
		ply.LastHealthIncrease = CurTime()
	end
end

And this variant will not spam the SetHealth function every tick, instead giving a player 1 point of health every second, you can of course change it to your liking, but this is the best way to do this.

Very in depth, and works great. Thanks!