Custom health script acting weird

I have a custom health script based on the original roblox Health script. I added some stuff and im trying to make it so that if its been 10 seconds and you have below 100 health and havent taken any damage, you get a boost and your regen gets extra fast.

-- Gradually regenerates the Humanoid's Health over time.

local REGEN_RATE = 1/100 -- Regenerate this fraction of MaxHealth per second.
local REGEN_STEP = 1 -- Wait this long between each regeneration step.
local REGENS = 0

--------------------------------------------------------------------------------

local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'

--------------------------------------------------------------------------------

while true do
	local healthbefore = Humanoid.Health
	while Humanoid.Health < Humanoid.MaxHealth do
		local dt = wait(REGEN_STEP)
		local healthafter = Humanoid.Health
		REGENS += 1
		print(REGENS)
		if healthafter < healthbefore then
			REGENS = 0
		end
		if REGENS >= 10 then
			REGEN_STEP = 0.2
		else
			REGEN_STEP = 1
		end
		local dh = REGEN_STEP*REGEN_RATE*Humanoid.MaxHealth
		Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)
	end
	Humanoid.HealthChanged:Wait()
end

At the first damaging, I damage myself and after 10 seconds, the server is printing the regen count at the speed it should but my health isnt going up the amount it should as you can see by looking at the green health bar.
At the second time, the server just starts printing 1 repeatedly as the REGENS for a while until the regens randomly start going up

Video

For the first issue, maybe try changing the regen rate instead of just the regen step. For your other problem, the variable healthbefore never updates due to the second while loop not ending. (It would only end when you’re back to full hp.)

I want it to get health every 0.2 seconds so you get health quicker and it looks better

The logic is simply wrong in your code. REGEN_STEP only affects how often the health updates, REGEN_RATE affects the actual regeneration rate.

This code changes the regeneration rate (REGEN_RATE and REGEN_BOOSTED_RATE) depending on how much time has passed (REGEN_BOOSTED_TIME). It also resets the time if the Humanoid ever takes damage by checking if the new health is lower than the latest health.

--!strict

local REGEN_RATE: number = 1/100
local REGEN_BOOSTED_RATE: number = REGEN_RATE * 5
local REGEN_BOOSTED_TIME: number = 10
local REGEN_STEP: number = 1

local Character: Model = script.Parent
local Humanoid: Humanoid = Character:WaitForChild("Humanoid") :: Humanoid

local damageConnection: RBXScriptConnection? = nil

while true do
	local timeElapsed: number = 0
	local lastHealth: number = Humanoid.Health
	
	damageConnection = Humanoid.HealthChanged:Connect(function(newHealth: number): ()
		if newHealth < lastHealth then
			timeElapsed = 0
		end
	end)
	
	while Humanoid.Health < Humanoid.MaxHealth do
		local deltaTime: number = task.wait(REGEN_STEP)
		timeElapsed += deltaTime
		
		local deltaHealth: number = Humanoid.MaxHealth * deltaTime * (if timeElapsed >= REGEN_BOOSTED_TIME then REGEN_BOOSTED_RATE else REGEN_RATE)
		Humanoid.Health = math.min(Humanoid.Health + deltaHealth, Humanoid.MaxHealth)
		lastHealth = Humanoid.Health
	end
	
	Humanoid.HealthChanged:Wait()
	
	if damageConnection ~= nil then
		damageConnection:Disconnect()
		damageConnection = nil
	end
end
1 Like

Im trying to affect the regeneration speed. I want it so that instead of it just going up in 5’s every second, make it go up in 1’s every 0.2 seconds

I don’t think you understand what I mean by REGEN_STEP.

If you have a REGEN_RATE of 10, a REGEN_STEP of 1 or 0.2 will regenerate the same amount of health of 10/second, just the 0.2 updates the health value 5x more frequently.

By default, REGEN_BOOSTED_RATE is the normal REGEN_RATE * 5, change this to any value you want. I would also recommend changing the REGEN_STEP to 0.2 so it looks smoother.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.