Simple Health - A more efficient alternative to the default health script

A custom health regeneration script that uses events and recursion instead of loops

Important note: In order for this script to work correctly, it must be parented to StarterCharacterScripts with its name set to “Health” and its RunContext set to “Legacy”

You can find this script here :slight_smile:

Source code:

--!strict
local rate: number = 1 -- The unit is health per 1 second

-- Only values above this line are safe to adjust --

local humanoid: Humanoid = script.Parent.Humanoid

local wait = task.wait

humanoid.HealthChanged:Connect(function(health: number)
	if health < humanoid.MaxHealth then
		humanoid.Health += wait(0) * rate
	end
end)
1 Like

Why not just do it like this?

--!strict
local rate: number = 1 -- The unit is health per 1 second

-- Only values above this line are safe to adjust --

local humanoid: Humanoid = script.Parent:WaitForChild("Humanoid")

humanoid.HealthChanged:Connect(function(health: number)
	if health < humanoid.MaxHealth then
		humanoid.Health += task.wait() * rate
	end
end)
3 Likes

Since due to recursion the function will act as a loop, I stored task.wait in order to save the script from having to look-up the task global every iteration. The 0 is personal preference and you can leave it out if you want, since it does the same job as nil in this case (The reason why I use it is due to a theory of mine of how the task.wait function works behind the scenes)

I personally ran some tests and concluded that localizing global functions before using them in loops resulted in better performance in most cases

5 Likes