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

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

Asked by
geoff89 15
10 years ago

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

Log in to vote
3
Answered by 10 years ago

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)
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. geoff89 15 — 10y
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. geoff89 15 — 10y
0
I've edited my answer appropriately. TheLuaWeaver 301 — 10y
Ad
Log in to vote
1
Answered by
Nickoakz 231 Moderation Voter
10 years ago
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

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. TheLuaWeaver 301 — 10y
0
il give you a point for putting it into code for me, but weaver takes the cake for the explanation. geoff89 15 — 10y

Answer this question