#about Health script

1 messages · Page 1 of 1 (latest)

reef spindle
#

Can the health regen and time be dynamic?
my idea was to add 2 value, 1 is a HealthRegen value, 1 is a HealthTime value, so that i can change the regeneration any time
The problem is that it doesnt work:
`-- Gradually regenerates the Humanoid's Health over time.

local REGEN_RATE = script.RegenAmount.Value -- Regenerate this fraction of MaxHealth per second.
local REGEN_STEP = script.RegenTime.Value -- Wait this long between each regeneration step.


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


while true do
while Humanoid.Health < Humanoid.MaxHealth do
local dt = wait(REGEN_STEP)
local dh = dtREGEN_RATEHumanoid.MaxHealth
Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)
end
Humanoid.HealthChanged:Wait()
end`

thorn sun
#

You have to get the value in the actual while loop

#

When you save the value like that to a variable you are just storing the INITIAL value

#

Once the value changes it won't update

reef spindle
#

ok thank you i will try that

compact kettle
# reef spindle ok thank you i will try that

try this:

-- Gradually regenerates the Humanoid's Health over time.

local REGEN_RATE = script.RegenAmount-- Regenerate this fraction of MaxHealth per second.
local REGEN_STEP = script.RegenTime-- Wait this long between each regeneration step.

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

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

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

while true do
    while Humanoid.Health < Humanoid.MaxHealth do
        local dt = wait(REGEN_STEP.Value)
        local dh = dt * REGEN_RATE.Value * Humanoid.MaxHealth
        Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)
    end
    Humanoid.HealthChanged:Wait()
end
reef spindle
#

but what does the health regeneration formula means?

compact kettle
# reef spindle but what does the health regeneration formula means?

"dt" is the deltaTime or the time that actually passed when doing wait() in absurd precision (wait(1) doesnt wait EXACTLY 1 second). so "dt" is to get rid of precision errors. next dh is just the regen rate * dt * the humanoid max helath to make the regeneration to max health take the same time for any max health and the last "math.min()" is to make the humanoid not regen past max health.