Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to have your character make a sound if he's injured?

Like if you got shot, it'll play a sound. Like "Braaa" or "OOOH." And it randomly selects a sound each time you are you are injured. Something like this.

			local NewHealth = Humanoid.Health + (Humanoid.Health/Humanoid.MaxHealth) + 1
			if NewHealth = Humanoid.Health and Humanoid.Health = -1 < 
				then
				sound1:play()
			end

And another sound will play depending on how much damage you take. Like in older games (Preferably DOOM, Quake, and Serious Sam) where when you take damage, your guy will make a sound.

if NewHealth = Humanoid.Health and Humanoid.Health = - 1< -- you take greater than 1 damage, it'll ---play
      then
      sound1:play() 
end

1 comment

0

This is all in a health GUI script, by the way.

2 answers

✓ Accepted answer

To get when a player gets hurt we need to connect the humanoid Changed event and then check in the function if the property changed was Health. After we check if the property changed was the health we then check if the last health we got from the humanoid is below the current health after that we would play the sound.

The Lua conversion:

-- This goes at the top of the script.
local lh = 0
local ply = game.Players.LocalPlayer

-- This goes in the character event in the health script
local humanoid = ply.Character.Humanoid
lh = ply.Character.Humanoid.Health
humanoid.Changed:connect(function(prop)
	if prop == "Health" then
		if lh > humanoid.Health then -- This checks if health is below the last health from the humanoid
			print("Play sound")
		end
		lh = ply.Character.Humanoid.Health
	end
end)

I check if this works and it does, so if it doesn't you can ask me to help you more.

3 comments

0

So, how does this make your character play a sound, exactly?

0

You would have to replace print("Play sound") to playing it.

0

do you have to keep the green part?

i think next to play type in () the id of the audio idk

This discussion is preserved as it was. New answers, comments, and votes are closed.