How to create a custom health regeneration?

This is my first topic(BAD ENGLISH,I used a translator)

How to create a custom health regeneration? just like the “item Asylum”([🎄] item asylum - Roblox)

When you are injured, waiting for 5 seconds will restore 2.5 HP per second. If you are injured again in healing, the restoration will be stopped and you will wait again

I have tried many people’s custom health regeneration scripts, but they all heal while getting injured.
I also tried to write one myself, but I couldn’t think of it

This is my failure script

local REGEN_STEP = 5 -- Wait this long between each regeneration step.

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

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







while true do
	Humanoid.HealthChanged:Wait()
	local health = Humanoid.Health
	local dt = task.wait(REGEN_STEP)
	
	if Humanoid.Health < Humanoid.MaxHealth and Humanoid.Health >= health then
		local dh = 2.5 -- restore hp 
		while Humanoid.Health >= health do
			print(health)
			health = Humanoid.Health
			task.wait(1) --restore 2.5 HP per second
			if Humanoid.Health >= health then
				Humanoid.Health += dh
				health = Humanoid.Health
			end
			
		end
		
	end
	
end

You can add a debounce system and prevent regeneration once the player is injured by using a variable

also I heavily recommend you not to use while loops since it can cause memory leaks, instead use RunService

2 Likes

First of all I’d suggest looking into disabling the default Roblox regeneration mechanics that you can do so by disabling the Health script in the Roblox character and through accessing the HealthRegenerationRate and HealthRegenerationDelay attributes in the Humanoid. Mostly so ur custom system and the default regen system don’t overlap each other and make a mess.

2 Likes

i have already done so ,thanks

Copy the default Health script that exists under your character model in workspace, copy paste it inside StarterPlayer.StarterCharacterScripts and modify it there. This will overwrite the default health script. Do not rename it tho.

1 Like

but it’s heal while getting injured

Healing happens from that default script, if you do what I said and modify the code inside of it, you should be able to change the healing behaviour. If I’m not mistaken the default script is just a while loop that adds a bit of health per some timeframe.

1 Like

Ah, I understand. I made a mistake
image
When I click the two circled buttons, it doesn’t actually change the health, only pressing Enter will make the change
So actually, my script is working properly
I have also improved my script:

local REGEN_STEP = 5 -- Wait this long between each regeneration step.

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

local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'::Humanoid
local humanoid = Humanoid
--------------------------------------------------------------------------------
local dh = 2.5 -- restore hp 



local canregen = true



Humanoid.HealthChanged:Connect(function()
	local health = Humanoid.Health
	local dt = task.wait(REGEN_STEP)

	if Humanoid.Health < Humanoid.MaxHealth and Humanoid.Health >= health and canregen then
		canregen = false
		while Humanoid.Health >= health and Humanoid.Health < Humanoid.MaxHealth do
			task.wait(1) --restore 2.5 HP per second
			if Humanoid.Health >= health then
            print("added")
				Humanoid.Health += dh
				health = Humanoid.Health
			else
				break
			end
			
		end
		canregen = true
	end
	
	
	
end)

Thank you all for your answers!

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