Need help with my low health sound

Hi, I’m fairly new to scripting and I need help with my low health script. It’s supposed to start playing when the player’s health is below 25, but it doesn’t play at all. It’s very simple, and I’ve re-made this script in different ways many times and all of them either looped endlessly, or didn’t play the sound at all. I created a value called “health”, and it matches the amount of HP the player currently has. The script is in StarterPlayerScripts. If you can help, please respond if you want to.

Here’s the script:

local health = game.ReplicatedStorage.health

if health.Value < 26 then
repeat
game.Workspace.lowhp:Play()
until
health.value > 24
end

Thanks for considering helping me!

2 Likes

your script checks the health only once so you need to loop that. You can use

Humanoid.HealthChanged:Connect(function()
   --your code
end)

to check whenever the health changes.

2 Likes

I had to change this a bit, I changed it to

local health = game.ReplicatedStorage.health

health.Changed:Connect(function()
if health.Value < 26 then
repeat
game.Workspace.lowhp.Playing = true
until
health.value > 24
end
end)

Three things happened, one, the health value froze, two, the entire screen froze until I was above 25 health, and three, the sound played once. Not sure what caused it, if you could tell me that would be appreciated.

1 Like

put a wait inside the repeat loop

2 Likes

Short and simple, it worked perfectly. Thank you so much for helping me with this!

2 Likes

StarterCharacterScripts

local Sound = workspace.Camera:FindFirstChild("NearDeathSound")
if Sound == nil then
	Sound = Instance.new("Sound", workspace.Camera)
	Sound.SoundId = ""
	Sound.Volume = 0
	Sound.Looped = true
    Sound.Name = "NearDeathSound"
end
if not Sound.IsLoaded then
	Sound.Loaded:Wait()
end
Sound:Play()
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	if Humanoid.Health <= 25 then
		Sound.Volume = 1
	else
		Sound.Volume = 0
	end
end)
4 Likes

Personally, I would have the low health music playing constantly at 0 volume. Here’s how I would do it:

Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
   local Ratio = Humanoid.Health / Humanoid.MaxHealth
   LowHealthMusic.Volume = 1 - math.clamp(Ratio / 0.25, 0, 1)
end)

Every time the player’s health changes, this function will fire. It then calculates the ratio of health-to-maxhealth the player has. 1 is full health, 0.5 is 50% health, and 0 is dead. Then, I set the music’s volume to start being noticeable at 25% health and it slowly increases in volume until the player is dead. It will also get softer and disappear if the player regains enough health.

2 Likes

That was actually my original plan, decided to change it because I couldn’t figure it out. Thanks for recommending this though, and nice script too.

This is a really high quality alternative to make a low health sound, thanks for recommending! Probably going to switch to this script.

1 Like