These are sufficient approaches to Healing, although yes you could do this, There is already a script that Handles Health Regeneration, You would either need to Remove it, or override it with a New Script named Health
within StarterCharacterScripts
Its best to take Note that with this kind of Health System, the Health will NEVER update if below 10, I’m not sure why you would have this system in the first place if it will become defunct after this condition takes effect, Another thing to note that wait()
is deprecated now, and should be replaced with task.wait()
, task.wait()
is MUCH better with performance, and is faster with Handling yields
You can use HealthChanged
to determine a Change in Health, unlike GetPropertyChangedSignal
, It will return the new Health.
For Example: you could have it so you can Save your Last Health Point to determine whether the Player has took damage or not, this is useful for Custom Damage Scripts where they use UI as a Way to determine this state:
LastHpPoint = Humanoid.Health -- Do this before making Changes
Humanoid.HealthChanged:Connect(function(HpPoint)
if HpPoint < LastHpPoint then -- if the New Health is less than the Old
-- Take Damage GUI
end
-- Do some Tweening or something to determine Health Change
LastHpPoint = HpPoint -- Last Health Point is now Recent
end)
This is a very useless rant about whatever i was on so, read if you like:
But to get on track, we can use math
functions to determine whether the Value should be 10
or above using math.floor(math.min())
to determine this:
while task.wait(1) do
Hum.Health = math.floor(math.min(10, Hum.Health + .3))
end
Although this would work, it isn’t efficient, and can have the script get stuck, so we would have to use if
statements and math.max()
to determine the math, this is so if a value goes over 10
, we can easily bring it back down without removing the use of the function:
while task.wait(1) do
local Result = math.floor(math.max(10, Hum.Health + .3))
if Result > 10 then
Hum.Health += .3
else
Hum.health = Result
end
end
But since you want the loop to end when the Humanoid’s health is equal to 10, you can modify the code to do so:
while Hum.Health > 10 do
task.wait(1)
local Result = math.floor(math.max(10, Hum.Health + .3))
if Result > 10 then
Hum.Health += .3
else
Hum.health = Result
end
end
-- or do this:
repeat task.wait(1)
-- too lazy to add code
until Hum.Health > 10
But again, you don’t have to have all these functions as they would basically do nothing to help, this would just make it easier to determine if the Health is actually below or equal 10. I dont exactly see the purpose of doing so however unless you are making some sort of Revive Script