SneakySquid icon

Simple Health Regen

SneakySquid | PRO | 06/11/26 09:11:20 AM UTC (Edited) | 0 ⭐ | 2087 👁️ | Never ⏰ | [Script, gmod, Garry's mod, garrysmod, Glua, health regen]
Lua |

1.53 KB

|

Source Code

|

0 👍

/

0 👎

local InitialDelay, RegenTimers, PreviousHealth, RegenAmount = 3, {}, {}, {
--  [HP Band (%)] = HP/s
    [0] = -0,
    [5] = 0.85,
    [10] = 1.60,
    [15] = 2.20,
    [20] = 2.65,
    [25] = 3.00,
    [30] = 3.25,
    [35] = 3.40,
    [40] = 3.45,
    [45] = 3.40,
    [50] = 3.25,
    [55] = 3.00,
    [60] = 2.70,
    [65] = 2.35,
    [70] = 2.00,
    [75] = 1.65,
    [80] = 1.35,
    [85] = 1.10,
    [90] = 0.75,
    [95] = 0.40,
    [100] = 1,
}
 
local GetRegenPercent do
    local sorted_amounts = {} do
        for k, v in pairs(RegenAmount) do
            table.insert(sorted_amounts, {k / 100, v})
        end
 
        table.sort(sorted_amounts, function(a, b)
            return a[1] < b[1]
        end)
    end
 
    function GetRegenPercent(health_frac)
        for i, v in ipairs(sorted_amounts) do
            if health_frac < v[1] then
                return v[2]
            end
        end
 
        return 0
    end
end
 
hook.Add("PlayerTick", "Health Regen", function(ply)
    local regen_timer = RegenTimers[ply]
    if not regen_timer then
        regen_timer = util.Timer()
        RegenTimers[ply] = regen_timer
    end
 
    local health = ply:Health()
    local max_health = ply:GetMaxHealth()
    local health_frac = health / max_health
 
    if regen_timer:Started() and (health_frac <= 0 or health_frac >= 1) then
        regen_timer:Reset()
    elseif health_frac > 0 and health_frac < 1 then
        if (PreviousHealth[ply] or health) > health then
            regen_timer:Start(InitialDelay)
        end
 
        if regen_timer:Elapsed() then
            regen_timer:Start(1 / GetRegenPercent(health_frac))
            ply:SetHealth(math.min(max_health, health + 1))
        end
    end
 
    PreviousHealth[ply] = health
end)
 

Comments