local humanoid = game.Players.LocalPlayer.Character.Humanoid local oldHealth = humanoid.Health humanoid.HealthChanged:connect(function(health) if oldHealth > 30 and health <= 30 then game.Workspace.LowHealth:Stop() game.Workspace.Criticalhealth:Play() elseif oldHealth < 30 and health >= 30 then game.Workspace.Criticalhealth:Stop() game.Workspace.LowHealth:Play() end oldHealth = health end)
I have done some tweaking to your variables. First why did I add events? Well, I did that so it could wait till the player and character join instead of automatically getting it (If we were to automatically get it, and the player or character didn't add, it would return nil). By the way, connect is deprecated, use Connect I stored the sounds inside a table so you could easily access it. I also didn't find the variable, oldHealth very useful since the argument in the HealthChanged event is the amount of health they have.
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait() -- Gets player from game.Players or Waits till the player adds local char = player.Character or player.CharacterAdded:Wait() -- Gets Character from player or Waits till the character adds local humanoid = char:WaitForChild("Humanoid") local sounds = { workspace.LowHealth, workspace.CriticalHealth } humanoid.HealthChanged:Connect(function(health) if health <= 30 then sounds[1]:Stop() sounds[2]:Play() elseif health >= 30 then sounds[2]:Stop() sounds[1]:Play() end end)
If this answer fixed your problem, please accept it, if not, please ask questions Please use the code block(the Lua button), and put your code inside(and please indent), it makes things readable