Still as efficient as the original, but now with the added ability to edit the rate during gameplay
Important information
It must be a server Script so that health can be replicated to other clients
The script’s name must be “Health” (without quotes) so that it can replace the default health script
The script’s parent must be StarterCharacterScripts so that it can continue to work after the player’s character dies or resets
(If creating SimpleHealth 2 from the source on GitHub) Add the following code between line 3 and 5:
script:SetAttribute("HealthPerSecond", 1)
Benefits over the default health script
Uses events exclusively, unlike how the default script uses an infinte while loop
It’s now possible to change the rate that a player’s character heals during gameplay, by editing the script’s “HealthPerSecond” attribute using a server-sided script
Examples on how you can use the rate during gameplay
The rate’s default value is 1. This is in-order to match the same regeneration rate that the default health script uses
Setting the rate’s value to any value greater-than 0 will cause the player’s character to heal faster
Setting the rate’s value to any value less-than 0 will cause the player’s character to take damage
Setting the rate to 0 will cause the character to be unable to regenerate their health
That is in fact what the script is doing, here’s the source code:
--!optimize 2
--!strict
local humanoid = script.Parent.Humanoid:: Humanoid
local healthPerSecond = script:GetAttribute("HealthPerSecond"):: number
humanoid.HealthChanged:Connect(function()
humanoid.Health += healthPerSecond * task.wait(0)
end)
script:GetAttributeChangedSignal("HealthPerSecond"):Connect(function()
healthPerSecond = script:GetAttribute("HealthPerSecond"):: number
end)
Although I can see now how the name How the rate works can be interpreted different to my original intention, which is to showcase examples on how one can change the rate to cause a different effect to occur, like for example if you wish to create a Tool that boost your healing rate. I’ll change the title to Examples on how you can use the rate during gameplay to better reflect this
It is necessary if you want to set the rate to a negative value, since HealthChange wouldn’t fire unless the Humanoid is hurt. Simply setting the rate to a negative value won’t cause the Humanoid’s health to start decreasing, although you did give me an idea on a way to remove the need to remember to jumpstart. I’ll work on it right now
Updated the script so that you no longer need to jumpstart if the rate is set to a negative value (the script will now handle it for you):
--!optimize 2
--!strict
local humanoid = script.Parent.Humanoid:: Humanoid
local healthPerSecond = script:GetAttribute("HealthPerSecond"):: number
humanoid.HealthChanged:Connect(function()
humanoid.Health += healthPerSecond * task.wait(0)
end)
script:GetAttributeChangedSignal("HealthPerSecond"):Connect(function()
healthPerSecond = script:GetAttribute("HealthPerSecond"):: number
if healthPerSecond < 0 then
humanoid.Health += healthPerSecond * task.wait(0)
end
end)