How to play sound only when HP changes?

I am using a custom hp value, not Roblox default. Right now my damage sound plays constantly in game when I only want it to play once when the value decreases.

Here is the code inside my HealthSystem script.

local BaseHP = 5
local HP = Instance.new("NumberValue")
HP.Name = "HP"

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		HP.Parent = player
		HP.Value = BaseHP
		while true do
			if HP.Changed then
		
		
				game.Workspace.SFX.Damage:Play()
				wait(0.4)
			end
			end
		end)
	end)

This is inside my health ui

HPvalue = script.Parent

while true do
	HPvalue.Text = game.Players.LocalPlayer.HP.Value
	wait(0.4)
end

See the examples at the bottom of this page:

https://developer.roblox.com/en-us/api-reference/event/Humanoid/HealthChanged

I’m not using Roblox’s health system i’m creating my own that revolves around a numbervalue

My bad.

First I would suggest to just make all the damage changes through a single function which subtracts health but also plays the sound.

But if you don’t wanna do that, you could use the NumberValue | Roblox Creator Documentation event.

In which script? aren’t I already using that?

local hpmark = HP.Value
HP.Changed:connect(function(newhp)
     if newhp < hpmark then
            —play sound
     end
     hpmark = newhp
end)

Unable to check for errors as I am on mobile, apologies.

you’re doing while true do .Changed

I tried the code submitted in the above reply and it still doesn’t work

Are you sure the HP Value is even changing in the first place, like is the HP Value changing on the client and you’re trying to play this sound on the server
or what

I think that might be it since i’m using a server and local script, the only thing in the local one is to keep the HP ui the same as the value in the player, but I could probably make the server one a local script if I just changed how i reference the player