How would I make low health sound not play again and again?

I’m using the HealthChanged code to play a low health sound every time the player’s health drops below a certain percentage. The problem is that the sound replays if the player takes damage again. How would I make the sound continuously play without replaying once the player is hurt again?

Snippet of the code:

local function onHealthChanged(Health)
	
	if Health >= Green then -- green
		LowHealthSound:Stop()
		HealthImage.Image = "rbxassetid://12442263965"
	end
	if Health <= Green then -- yellow
		LowHealthSound:Stop()
		HealthImage.Image = "rbxassetid://270195888"
	end
	if Health < Red then -- red
		LowHealthSound:Play()
		HealthImage.Image = "rbxassetid://270195929"
	end 
	HealthText.Text = Health
	MaxHealthText.Text = Humanoid.MaxHealth
end

Humanoid.HealthChanged:Connect(function(Health)
	onHealthChanged(Health)
end)
1 Like

Turn the Looped option on if it’s not

1 Like

it is, the problem is that the code detects a health change and plays the sound again, which silences the previous instance of it playing

Make a Boolean that if the players health is below a percetange is true, if it’s true don’t check for healthchanged, if it’s false check fro healthchanged
At the start of the scritp, just after the healthchanged function:

if belowMinimumHealth = true then return end

And make a variable, change belowMinimumHealth to your variable name

I’m not sure how that would help since I need the HealthChanged to actively check and play the sound. Either that, or I’m misunderstanding your words

Ok, i mean, you need the HealthChanged function to tell you when health changes until it’s below a certain number/percentage, but the problem is that your sound plays over and over when damaged, what I would do is:
Before the HealthChanged function, add thsi line:

local yourVariableName = yourNumber

change yourVariableName and yourNumber to what you need, now, after the HealthChanged function, and before everyone else add this:

if Humanoid.Health < yourVariableName then return end

As already stated, change yourVariableName to watch ever you need, and change Humanoid.Health to your path, this will make it so if the health si below a certain number, it will break the loop, teorethcilayy keepign your sound going

You can check if a sound is already playing from Sound.IsPlaying

local function onHealthChanged(Health)

	if Health >= Green then -- green
		LowHealthSound:Stop()
		HealthImage.Image = "rbxassetid://12442263965"
	end
	if Health <= Green then -- yellow
		LowHealthSound:Stop()
		HealthImage.Image = "rbxassetid://270195888"
	end
	if Health < Red then -- red
		if LowHealthSound.IsPlaying == false then --Check if the sound isnt playing first
			LowHealthSound:Play()
		end
		HealthImage.Image = "rbxassetid://270195929"
	end 
	HealthText.Text = Health
	MaxHealthText.Text = Humanoid.MaxHealth
end

Humanoid.HealthChanged:Connect(function(Health)
	onHealthChanged(Health)
end)
1 Like

That oesn’t seem to work, and I even tried changing it to if LowHealthSound.IsPlaying == true then return end

The sound was playing every time the humanoid heals, because when the humanoid heals, it’s health increases, therefore triggering the event. Here, it will check if the humanoid has been damaged or healed. If damaged and red, then it will play the sound.

local lastHealth = Humanoid.Health

local function onHealthChanged(health)
	if health < Red then
		if health - lastHealth < 0 then
			LowHealthSound.TimePosition = 0 -- resets the sound
			LowHealthSound:Play()
		end

		HealthImage.Image = "rbxassetid://270195929"
	end

	if health >= Green then
		HealthImage.Image = "rbxassetid://12442263965"
	end

	HealthText.Text = health
	MaxHealthText.Text = Humanoid.MaxHealth
end

In your original code it seems that yellow if statement will stop the sound even when the health is red. Try this:

local function onHealthChanged(Health)
	if Health >= Green then -- green
		LowHealthSound:Stop()
		HealthImage.Image = "rbxassetid://12442263965"
	elseif Health > Red and Health < Green then -- More accurate yellow
		LowHealthSound:Stop()
		HealthImage.Image = "rbxassetid://270195888"
	elseif Health <= Red then -- red
		if LowHealthSound.IsPlaying == false then --Check if the sound isnt playing first
			LowHealthSound:Play()
		end
		HealthImage.Image = "rbxassetid://270195929"
	end 
	HealthText.Text = Health
	MaxHealthText.Text = Humanoid.MaxHealth
end

Humanoid.HealthChanged:Connect(function(Health)
	onHealthChanged(Health)
end)

I have modified your code a little bit, see if it works.

local BelowLevel=false

local function onHealthChanged(Health)

	if Health >= Green then -- green
		BelowLevel=false
		LowHealthSound:Stop()
		HealthImage.Image = "rbxassetid://12442263965"
	end
	if Health <= Green then -- yellow
		BelowLevel=false
		LowHealthSound:Stop()
		HealthImage.Image = "rbxassetid://270195888"
	end
	if Health < Red then -- red
		if BelowLevel==false then
			BelowLevel=true
			LowHealthSound:Play()
		end
		HealthImage.Image = "rbxassetid://270195929"
	end 
	HealthText.Text = Health
	MaxHealthText.Text = Humanoid.MaxHealth
end

Humanoid.HealthChanged:Connect(function(Health)
	onHealthChanged(Health)
end)

Thank you so much, it actually worked!

1 Like

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