SneakySquid

Simple Health Regen

Jul 17th, 2024 (edited)
2,209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.53 KB | Source Code | 0 0
  1. local InitialDelay, RegenTimers, PreviousHealth, RegenAmount = 3, {}, {}, {
  2. --  [HP Band (%)] = HP/s
  3.     [0] = -0,
  4.     [5] = 0.85,
  5.     [10] = 1.60,
  6.     [15] = 2.20,
  7.     [20] = 2.65,
  8.     [25] = 3.00,
  9.     [30] = 3.25,
  10.     [35] = 3.40,
  11.     [40] = 3.45,
  12.     [45] = 3.40,
  13.     [50] = 3.25,
  14.     [55] = 3.00,
  15.     [60] = 2.70,
  16.     [65] = 2.35,
  17.     [70] = 2.00,
  18.     [75] = 1.65,
  19.     [80] = 1.35,
  20.     [85] = 1.10,
  21.     [90] = 0.75,
  22.     [95] = 0.40,
  23.     [100] = 1,
  24. }
  25.  
  26. local GetRegenPercent do
  27.     local sorted_amounts = {} do
  28.         for k, v in pairs(RegenAmount) do
  29.             table.insert(sorted_amounts, {k / 100, v})
  30.         end
  31.  
  32.         table.sort(sorted_amounts, function(a, b)
  33.             return a[1] < b[1]
  34.         end)
  35.     end
  36.  
  37.     function GetRegenPercent(health_frac)
  38.         for i, v in ipairs(sorted_amounts) do
  39.             if health_frac < v[1] then
  40.                 return v[2]
  41.             end
  42.         end
  43.  
  44.         return 0
  45.     end
  46. end
  47.  
  48. hook.Add("PlayerTick", "Health Regen", function(ply)
  49.     local regen_timer = RegenTimers[ply]
  50.     if not regen_timer then
  51.         regen_timer = util.Timer()
  52.         RegenTimers[ply] = regen_timer
  53.     end
  54.  
  55.     local health = ply:Health()
  56.     local max_health = ply:GetMaxHealth()
  57.     local health_frac = health / max_health
  58.  
  59.     if regen_timer:Started() and (health_frac <= 0 or health_frac >= 1) then
  60.         regen_timer:Reset()
  61.     elseif health_frac > 0 and health_frac < 1 then
  62.         if (PreviousHealth[ply] or health) > health then
  63.             regen_timer:Start(InitialDelay)
  64.         end
  65.  
  66.         if regen_timer:Elapsed() then
  67.             regen_timer:Start(1 / GetRegenPercent(health_frac))
  68.             ply:SetHealth(math.min(max_health, health + 1))
  69.         end
  70.     end
  71.  
  72.     PreviousHealth[ply] = health
  73. end)
  74.  
Advertisement
Add Comment
Please, Sign In to add comment