Here's the issues I found with your script.
local humanoid = game.Players.LocalPlayer.Character.Humanoid
local gui = script.Parent
local frame = gui.Frame
local sound = Instance.new ("Sound",gui)
sound.SoundId = "http://www.roblox.com/asset/?id=386232234"
local health = humanoid.Health
humanoid.Changed:connect(function()
if humanoid.Health < health then
sound:Play()
end
health = humanoid.Health
end)
Issue 0.5:
The way you wrote your script was hard to read, so I advise learning how to properly write code.
I already did it for you with these scripts, so maybe you can take notes off of it.
Issue 1:
The line if humanoid.Health < health then is simply not required. You could easily use another event by the name of "HealthChanged" to fire the script and even gather the current HP value at that time.
Issue 2:
Using the Changed event means that if ANY part of the humanoid is changed at all, it will fire.
Once it fires, the function doesn't stop until the if statement within is fulfilled. This could easily cause issues later on.
Here's the script you'll need. You might have to adjust it.
local gui = script.Parent
local frame = gui.Frame
local sound = Instance.new ("Sound",gui)
sound.SoundId = "http://www.roblox.com/asset/?id=386232234"
local humanoid = game.Players.LocalPlayer.Character.Humanoid
humanoid.HealthChanged:connect(function(health)
sound:Play()
end)
Also, PLEASE use the website's codeblock system. Whenever you make a question, theres a little Lua icon. Highlight your code, and then press that button. It'll enclose the code with squiggly lines and convert it to something way more easy to read.
EDIT ! :
So I read your comment, I can see the issue regarding the health regen you have in your game. Simple fix.
So for starters, add an IntValue into your screenGUI manually. Rename it something obvious, like "recentUserHP" or something.
Keep the value at 0.
The intvalue will be used to store the most recent HP loss or gain, and we can use a simple mathematical function to determine whether or not to play the sound. If we tried this with a local variable, it wouldn't work.
local gui = script.Parent
local frame = gui.Frame
local sound = Instance.new ("Sound",gui)
sound.SoundId = "http://www.roblox.com/asset/?id=386232234"
local humanoid = game.Players.LocalPlayer.Character.Humanoid
humanoid.HealthChanged:connect(function(health)
if gui.recentUserHP.Value > health then
sound:Play()
elseif gui.recentUserHP.Value < health then
end
end)
If this helped you out, please accept me as your answer. There's a button to the right, it helps me get reputation which I can utilize for certain tasks.
Welcome to SH, by the way.
6 comments
Sorry, im new here
Also health regen is on so when ever it adds health it makes the sound also. I only want it to make a sound when health is going down.
Okay, ill adjust it.
Adjusted, please read the edit near the bottom.
ok, i actually checked this right now. I'll see if it works.
ok, i just checked it but for some reason, it wouldn't make the sound. While i'm dead though, it makes the sound if I touch something that hurts me. When I become alive again, it doesn't work still.