Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a sound play when you get hurt?

Asked by 7 years ago
Edited 7 years ago

i want it so when you get hurt, it makes a noise. The problem is that the script i have works, but only works in studio. It doesn't work in game. here is the 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)

also, the script is a local script, and it is inside of a screen gui.

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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 

--this function below fires whenever the humanoid's health changes.
--It also gets the health of the user at that time. (inside the parentheses)
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 

--this function below fires whenever the humanoid's health changes.
--It also gets the health of the user at that time. (inside the parentheses)
humanoid.HealthChanged:connect(function(health) 
    if gui.recentUserHP.Value > health then 
    --this asks if the most recent HP logged is higher than the users current HP
    sound:Play()
    elseif gui.recentUserHP.Value < health then
    --this is the opposite, it asks if the users HP is higher than the most recently logged value,
    --and if it is, that means the user has gained HP back.
    --therefore, it does nothing.
    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.

0
Sorry, im new here flufffybuns 89 — 7y
0
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. flufffybuns 89 — 7y
0
Okay, ill adjust it. konichiwah1337 233 — 7y
0
Adjusted, please read the edit near the bottom. konichiwah1337 233 — 7y
View all comments (2 more)
0
ok, i actually checked this right now. I'll see if it works. flufffybuns 89 — 7y
0
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. flufffybuns 89 — 7y
Ad

Answer this question