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

play a sound when hp is low doesn't work?

i have been trying to make a localscript where it plays a sound when the hp of the player is low i only have 1 player per server if that helps with anything but here is my current script.

Game:GetService("ContentProvider"):Preload("http://www.roblox.com/asset/?id=178518020")

if game.Players.LocalPlayer.Character.Humanoid.Health <= 35 then
	Workspace.LowHP:Play()
end

i tried making the health a variable to save effort but gave that up after so much defeat.

2 answers

✓ Accepted answer

The issue is that will only happen ONCE when the script is loaded. When the script is loaded, the health is 100, so that is never, ever, run! The solution to your issue can be found here, on the wiki.

To use it, connect the HealthChanged event of your Humanoid to a function:

game.Players.LocalPlayer.Character.Humanoid.HealthChanged:connect(function(health)
	--health is their current health, so do what you want with it
end)

3 comments

0

the page you gave me confuses me, i understand i need to put HealthChanged somewhere, but the example doesn't help me with my if statement.

0

actually forget that last comment, however im still having trouble if you can help me alittle more. i still cant get it to work.

0

I've edited my answer appropriately.

game:GetService("ContentProvider"):Preload("http://www.roblox.com/asset/?id=178518020")

local char=game.Players.LocalPlayer.Character
char:WaitForChild("Humanoid")

Humanoid.HealthChanged:connect(function()
	if char.Humanoid.Health <= 35 then
	    script.Parent.LowHP:Play()
	end
end)

Put the sound inside the script, and put the script inside StarterGui. Wola :l

2 comments

0

This isn't a good answer. Please read this: https://scriptinghelpers.org/help/how-post-good-questions-answers to see why. You've not explained any code, only given it.

0

il give you a point for putting it into code for me, but weaver takes the cake for the explanation.

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