Help with Health Regen While Loop

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Create a loop to heal players by .3 every 1 second until health is >= 10

  2. What is the issue? Include screenshots / videos if possible!
    The below code will enter the loop and heal them once but not register anything after (won’t even break out of the while loop).
    ‘’’
    while humanoid.Health < 10 do
    humanoid.Health:TakeDamage(-.3)
    wait(1)
    end
    ‘’’

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Tried incorporating a task.wait(), using a variable for the health, setting the health directly, etc. Many attempts but keep getting similar results. I do know know why it keeps breaking, this is in a module script by the way being called from an other damage dealing script but that has not been an issue until now.

1 Like

Try this instead:

while wait(1) do
   if humanoid.Health < 10 then
      humanoid.Health:TakeDamage(-.3)
   end
end
1 Like

This did not work either. Still working on other possible solutions, but I’m having a hard time getting it to enter the if statement.

Try something like this:

local player = game.Players.LocalPlayer

-- Function to heal the player
local function healPlayer()
  -- Check if player's health is above 10
  if player.Character.Humanoid.Health > 10 then
    return
  end

  -- Increase player's health by 0.3
  player.Character.Humanoid.Health = player.Character.Humanoid.Health + 0.3
end

-- Call the healPlayer function every second
while true do
  healPlayer()
  wait(1)
end

If the code in the post is all that is in the script. Then the code won’t ever run in the first place because the Health isn’t below 10 when the user spawns in and assumably when the script runs.

Instead of running an infinite while loop on the script. Instead hook up a GetPropertyChangedSignal function that detects whenever the Health property of the Humanoid is changed. In that function you can then use an if statement to check if the health is below 10. If it is, run a while loop much like what you’ve already provided. Make sure to use a debounce as well as everytime the loop heals the user it’ll fire the function!

2 Likes
while task.wait() do
 if humanoid.Health <= 10 then
   humanoid.Health += 0.3;
 end
end

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

1 Like

To answer some of the prior questions, this was a part of a much larger script with a downed system in a fighting game which takes various collisions into place. The issue ended up being that the script which calls the module script here was being destroyed before the while loop could process any of the wait() 's (how it get this far in the processing I do not know). I had to set up a Bindable Event from the module script to another server script which worked. Thank you all for the help, learned a lot.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.