As some of us in the Roblox developer community may know, the default Roblox health regeneration script uses a while true do
loop to regenerate players’ health. Upon seeing this, I asked myself: “Why does Roblox Corporation, a corporate entity that makes billions of dollars a year while totally caring about their player community (/j) still use this unoptimized garbage for their default regen script that’s used in the majority of games on the platform?”
So, I took it upon myself to re-script the health regen script. After about 20+ hours of scripting, testing and implementing brand-new features (not all in a row), I’m confident enough to publicly disclose that code for people to use in their own StarterCharacterScripts
. So here you all go!
The script, collapsed for your reading convenience
WARNING: This script requires Attributes named “Multiplier” and “DeepWounded” to work.
-- == Catz's Custom Health Regen Script v1.0 == --
-- Because Roblox's default script is unoptimized.
-- Services
local RunService = game:GetService("RunService")
-- Values
local Character = script.Parent
local Humanoid:Humanoid = Character:WaitForChild("Humanoid")
local RegenTick = 0
local RegenConnection:RBXScriptConnection = nil
local WoundConnection:RBXScriptConnection = nil
local DeathConnection:RBXScriptConnection = nil
-- Functions
function RegenerationLoop(total,dt)
RegenTick += dt
if RegenTick >= (1/script:GetAttribute("Multiplier")) then
RegenTick = 0
Humanoid.Health += (Humanoid.MaxHealth/100)
end
end
function DeepWounded(Status)
pcall(function() RegenConnection:Disconnect() end)
if Status == true then
RegenTick = 0
else
RegenConnection = RunService.Stepped:Connect(RegenerationLoop)
end
end
function Death()
pcall(function() RegenConnection:Disconnect() end)
pcall(function() WoundConnection:Disconnect() end)
pcall(function() DeathConnection:Disconnect() end)
end
-- Connections
RegenConnection = RunService.Stepped:Connect(RegenerationLoop)
WoundConnection = script:GetAttributeChangedSignal("DeepWounded"):Connect(DeepWounded)
DeathConnection = Humanoid.Died:Connect(Death)
The new health regen script (which is by no means official) uses a mix of RunService
connections and RBXScriptConnection:Disconnect()
to protect against unnecessary code execution and memory leaks. By “unnecessary execution” I primarily mean that we don’t want to attempt to regenerate health when the Humanoid is already dead. Additionally, via the use of Attributes (through :SetAttribute()
) one can customize the speed of the health regeneration in real-time, or stop it completely (via adjusting “Multiplier” and “DeepWounded” respectively).
Anyway here’s a brief Q&A section (also collapsed), and please let me know your thoughts on my work in the replies.
A brief Q&A section
Q: Why did you call it “DeepWounded”?
A: I got the name of this attribute from Dead by Daylight, actually.
Q: Does this work on NPCs?
A: Yes, just change Character:WaitForChild("Humanoid")
to Character:WaitForChild("<whatever you named your NPC's humanoid>")
and you’re good to go.
Q: But why go through any of this?
A: Yes.
Q: Can I use your script?
A: Yes, all I ask is that you credit me (although this isn’t required). I might release it on the Roblox Developer Library if this post gets enough attention.
Q: Are you a furry, as your avatar implies?
A: Yes, and please keep your distance with the flame throwers.
More questions may be added to this section if they get asked frequently enough