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

How do get a sound to play when health is very low?

I am making a 3rd Person Shooter game, so I made a health Gui with this script

wait(.1)
while true do
	hp = game.Players.LocalPlayer.Character.Humanoid.Health/100
	script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15)
	wait(.2)
end

So how do I get this sound SoundService.Heartbeat:Play() to play when the player's health is below 25, The sound is only a second long so how do I loop it and play it if the player's health is below 25 and stop after 25

3 answers

Use the HealthChanged event to do so. Also, try putting it in a different LocalScript

local hum = game.Players.LocalPlayer.Character.Humanoid
local SoundService = game:GetService("SoundService")

hum:GetPropertyChangedSignal("Health"):Connect(function()
      if health <= 25 then
           SoundService.Heartbeat.Looped = true
           SoundService.Heartbeat:Play()
      elseif SoundService.Heartbeat.Playing == true then
           SoundService.Heartbeat:Stop()
end
end)

10 comments

0

Tested it, but it didn't work

0

Any errors?

0

OhMan, use GetPropertyChangedSignal and use <=

0

Thank you Silents. Smartics, try now.

0

OhManLolLol I am getting this error https://ibb.co/WkZq4fW

0

Try it now

0

As I stated Earlier I am trying to make a gui that takes info from the player's health and represents it in its own way I just want a sound to be playing when health <= 25, When i try your script on my Gui which is local the Gui health is stuck at 25 but the actual health is 100

0

Don't use it in the same script as the original one because your script divides health by 100, my script is just looking for health at it's normal form.

0

Sorry for late reply, But where do I put your Local script? ServerScriptService? maybe

0

Put it in StarterCharacterScripts

Yeah so just use this to detect when the players health has changed.

local player = game.Players.LocalPlayer
local character = player:WaitForChild("Character")
local humanoid = character.Humanoid
local SoundPlaying = false

humanoid.HealthChanged:Connect(function(newHealth)
	if newHealth <= 25 and SoundPlaying == false then 
		-- You need to look up how to make a sound play. It's simple so take the time to learn it.
		-- This is a debounce
		SoundPlaying = true

      	elseif SoundPlaying == true and newHealth > 25 then
           	-- Make the sound stop playing. Look up how it's simple to learn. Just do it lol.
		
		-- Undo the debounce
		SoundPlaying = false
	end
end)

You asked how you would track the health and play a sound. I showed you how to track the health. You need to learn how to do a sound by yourself cause it's not too difficult.

1 comment

0

I forget if healthChanged passes newHealth. If it doesn't just replae newHealth with humanoid.Health lol

er find the players health and then check if the health is > than 25 then idk

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