How to detect when a Player regenerates health

In my game I want a sound to be played when a player regenerates their health, but I can’t find a way to detect when a player gains health.

1 Like

Check the health every X seconds and check if theres a difference between what it was X seconds ago and now
Its not a perfect solution and would probably require some refining and perhaps a debounce but I would think itd be a good start

A basic example of this would be:

local lastValue = hum.Health
while true do
    if hum.Health - lastValue > 0 then --you might want to change it from 0 to like 1 to make sure a substantial amount of health was gained
        print("health gained")
    end
    wait(0.5) --amount of time could be changed to suit your needs
end

That works pretty well but I only want the sound to play once every time the player regens their health

you mean when the player regens their max health

not exactly, I want the sound to play when the player starts to regen their health, not after they fully regens their health to max

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local function HealthChanged(Health)
    print(Health)
end

character.Humanoid.HealthChanged:Connect(HealthChanged)

DevHub HealthChanged

2 Likes

I only need it to detect when someone’s health goes up, not up and down

I did some tweaking with the script to stop it from playing the sound every time I was healing so that the sounds didn’t overlap and it worked!

This should be better.

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local lastHealth = 100;

local function HealthChanged(Health)
    if (Health - lastHealth) > 0 then
        print("Regen!")
       lastHealth = Health
    end
end

character.Humanoid.HealthChanged:Connect(HealthChanged)
1 Like