Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local RegenDelay = 3
- local RegenAmount = {
- [10] = 15,
- [25] = 12.5,
- [40] = 10,
- [55] = 7.5,
- [70] = 5,
- [85] = 2.5,
- [100] = 1,
- }
- local RegenTimers = setmetatable({}, {
- __index = function(self, ply)
- self[ply] = util.Timer()
- return self[ply]
- end
- })
- local PreviousHealth = setmetatable({}, {
- __index = function(self, ply)
- self[ply] = ply:Health()
- return self[ply]
- end
- })
- local GetRegenPercent do
- local sorted_amounts = {} do
- for k, v in pairs(RegenAmount) do
- table.insert(sorted_amounts, {k / 100, v / 100})
- 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]
- local health = ply:Health()
- local max_health = ply:GetMaxHealth()
- local health_frac = health / max_health
- if health_frac <= 0 and regen_timer:Started() then
- regen_timer:Reset()
- elseif health_frac < 1 then
- if PreviousHealth[ply] < health then
- regen_timer:Start(RegenDelay)
- regen_timer.InitialDelay = true
- end
- if not regen_timer:Started() then
- regen_timer:Start(1 / (max_health * GetRegenPercent(health_frac)))
- regen_timer.InitialDelay = false
- end
- if regen_timer:Elapsed() then
- regen_timer:Reset()
- if not regen_timer.InitialDelay then
- ply:SetHealth(math.min(max_health, health + 1))
- end
- end
- end
- PreviousHealth[ply] = health
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement