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
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.
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)
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.