Heartbeat Audio Volume Relevant To Humanoid Health

Hello! I’m basically just trying to do what the title says. I’m trying to get my heartbeat audio to match the humanoid’s health property. Here is my current code, that doesn’t seem to work.

local player = game.Players.LocalPlayer

local character = script.Parent

local humanoid = character:FindFirstChild("Humanoid")

local sound = player.PlayerGui.HurtOverlay.Heartbeat

while true do

wait()

sound.Volume = humanoid.Health / 100

end
1 Like

Maybe you can possibly try getting the humanoid and character through player.character. Not 100% sure if it will work though.

in what way is it not functioning?

The audio isn’t playing, and there are no errors in the output.

Did you ever code it to play the sound?

is the audios playing property set to true or false? if false you have to play the sound with code

I thought I had playing on, but I guess I didn’t. That fixed it, but now the audio volume is high when the health is high instead of when the health is low.

1 Like

Here, I made it more efficient and fixed the volume.

your current formula of

sound.Volume = humanoid.Health / 100

makes it so that its higher when its max, health/maxhealth = 1 when you’re at full health

you’d wanna make it return 0 when you’re max

sound.Volume = (humanoid.MaxHealth-humanoid.Health)/humanoid.MaxHealth

Insted of using a while true loop, you should bind it to humanoid:GetPropertyChangedSignal(“Health”).

That worked too! Now I’m wondering, is there any way to keep the volume equal to or less than 10 so the volume isn’t too much? I know I’m asking for a lot but I was just curious if there was a way somehow.

given the formula of

sound.Volume = (humanoid.MaxHealth-humanoid.Health)/humanoid.MaxHealth

it’ll return a volume of 0-1, however if you define a max volume variable, you can multiply that entire formula by said variable to get it to be 0 to whatever volume you desire

local player = game.Players.LocalPlayer

local character = script.Parent

local humanoid = character:FindFirstChild("Humanoid")

local sound = player.PlayerGui.HurtOverlay.Heartbeat

local heartBeatVolume = 2 -- can make this any number really

while true do

wait()

sound.Volume = ((humanoid.MaxHealth-humanoid.Health)/humanoid.MaxHealth)*heartBeatVolume

end

I tried this,

local player = game.Players.LocalPlayer

local character = script.Parent

local humanoid = character:FindFirstChild("Humanoid")

local sound = player.PlayerGui.HurtOverlay.Heartbeat

local soundVolume = 0.1 -- can make this any number really

humanoid.HealthChanged:Connect(function()

wait()

sound.Volume = ((humanoid.MaxHealth-humanoid.Health)/humanoid.MaxHealth)*soundVolume

end)

But now no audio is playing, or at least the volume isn’t working.

.1 is really quiet, the number you put for soundVolume will be the max volume when you’re at 0 health, the higher that number the louder it’ll be

Ah I see, I set it to 10 now, because that’s what I want the max to be, but I still don’t hear any audio. No errors in output either.

i see you used humanoid.HealthChanged, are you changing the health to test the volume or just running it without setting anything? try printing in the function to see if it runs and print the sound volume to see if it alters or not

I was trying what someone else suggested, but even when trying both while true do and humanoid.HealthChanged:Connect(function() it doesn’t even print anything. Is it because I’m using a LocalScript?

I don’t think a healthchanged event exists. Try using:

humanoid:GetPropertyChangedSignal(“Health”):Connect(function()
-- Code will run when health is changed
end)

That’s what I originally tried, but I keep getting incomplete statement.

Oh I forgot to add a bracket, try it now.