Unable to fix Health regeneration repeation error

I don’t know how to fix this:

image
image

any help?

3 Likes

The error message means your function is looping repeatedly, the error is to catch that
loop: humanoid.Health = oldHealth - formula —> humanoid.HealthChanged

2 Likes

how would i catch the loop? I kinda dont understand

1 Like

when you set humanoid.Health to a new value in your function, it fires your event, which runs into the function that sets humanoid.Health again

you could create another variable, like local DesiredHealth = Instance.new(“NumberValue”) that you would parent to the player and use the Changed event on

1 Like

that doesn’t work since the value is always same with health
if health changes then value does and if value does then the Changed:Connect() fires too

1 Like

Use a debounce.

local isUpdatingHealth = false -- prevents the event from firing again when you're updating something
humanoid.HealthChanged:Connect(function(health)
   if isUpdatingHealth then
       return -- prevent the loop from ocurring
   end
   isUpdatingHealth = true -- enable the debounce
   humanoid.Health = -- set the new health here
   isUpdatingHealth = false -- reset the debounce
end)
2 Likes

What is the purpose of this script? If it’s to regenerate health, a while true do loop might work better.

2 Likes

tried, didn’t worked but thanks for trying to help too

1 Like

Can you show me your script? And were there any errors? What exactly are you trying to do?
Also, the value of oldHealth never changes, try adding oldHealth = humanoid.Health in the end of the event.

humanoid.HealthChanged:Connect(function(health)
        local damageTaken = oldHealth - health
        local formula = (damageTaken / 10) * 4
        humanoid.Health = oldHealth - formula
        oldHealth = humanoid.Health -- add this here

end)
1 Like