I don’t know how to fix this:
any help?
I don’t know how to fix this:
any help?
The error message means your function is looping repeatedly, the error is to catch that
loop: humanoid.Health = oldHealth - formula —> humanoid.HealthChanged
how would i catch the loop? I kinda dont understand
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
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
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)
What is the purpose of this script? If it’s to regenerate health, a while true do loop might work better.
tried, didn’t worked but thanks for trying to help too
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)