How to make a sound increase in pitch based on a players health?

I want to make a heartbeat sound increase in pitch the lower the players health gets. I’m not sure what the math behind this is but I want it to be a slightly subtle increase in pitch the lower it gets.

I tried integrating it into my script that handles all the hurt effects and while it sorta works, it makes the playbackspeed extremely low, what could I do to make the effect less drastic? Here is the code:

``````char.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
if char.Humanoid ~= nil then
if char.Humanoid.Health <= 40 and HeartBeat.IsPlaying == false then
HeartBeat.PlaybackSpeed = char.Humanoid.Health / 100
HeartBeat:Play()
LowHealthBlur.Size = 8
elseif char.Humanoid.Health >= 40 then
HeartBeat:Stop()
LowHealthBlur.Size = 0
end
end
end)

``````

Multiply it instead of dividing it

Could you elaborate? Wouldn’t multiplying just make it extremely fast? If the character started at 100 health a playback speed of 100 times anything would be ridiculously fast.

If you’re looking to increase the playback speed, this should work:

`HeartBeat.PlaybackSpeed = 1.5 - char.Humanoid.Health / 200`
This code has a speed of 1.5 at 0 health and 1 at 100 health.

1 Like

This almost works, although I think it’s in reverse, it lowers the pitch the lower the health gets instead.

I originally thought you misworded and wanted a decrease instead of an increase since you were dividing the health, but I’ve edited my post now. Sorry for the confusion.

Its ok, thank you for your help!

Instead of a GetPropertyChangedSignal for health, “Humanoid” instances have an existing event named “HealthChanged” which is fired whenever the “Health” property changes, this event has an additional optional parameter representing the new value of the “Health” property, so you could use the following instead:

``````local players = game:GetService("Players")

local humanoid = character:WaitForChild("Humanoid")
humanoid.HealthChanged:Connect(function(health)
if HeartBeat.IsPlaying then
return
end
if health <= 40 then
HeartBeat.PlaybackSpeed = 1.5 - health / 200
heartBeat:Play()
LowHealthBlur.Size = 8
elseif health > 40 then
HeartBeat:Stop()
LowHealthBlur.Size = 0
end
end)
end)
end)
``````

As you can see far fewer references are required (which means it should run more efficiently).

I also want to warn you that if health is “40” then both conditions would end up being satisfied (however only the body of the first one executes as it is satisfied first) I’ve made a slight change to the conditional logic to circumvent this.

1 Like