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

How would you make a sound louder if a players health was lower?

Asked by 4 years ago
Edited 4 years ago

I'm currently making a kind of wasteland game, and I have a script to make the players screen get redder depending on their health.

while true do
    script.Parent.ImageTransparency = humanoid.Health / 100
    wait()
end

I want to make a ear ringing sound get louder depending on how low the players health is, but I'm not sure how to do that.

while true do
    script.Parent.ImageTransparency = humanoid.Health / 100
    script.Parent.Parent.Sound.Volume = humanoid.Health / 100 -- a way to reverse the value???
    wait()
end

I'm just a bit confused on how to do this.

**EDIT - CONTEXT: ** I want the sound to be silent at full health, but get louder as the players health goes down. I should have added this in first. bruh

0
Use 1-(health/100). This way, when health is at numbers like 100 and 100/100=1 and 1-1=0 and at 25, 25/100=.25 and 1-.25=.75, you can set that as the volume and scale it up by multiplying it to make it louder. hiimgoodpack 2009 — 4y

1 answer

Log in to vote
2
Answered by
appxritixn 2235 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

If I understand what you are asking, and I think I do, then this should work.

while true do
    if humanoid.Health > 0 then
        script.Parent.ImageTransparency = humanoid.Health / 100
        if humanoid.Health ~= 100 then 
            script.Parent.Parent.Sound.Volume = 1/(humanoid.Health / 100) 
        else
            script.Parent.Parent.Sound.Volume = 0 
        end
        wait()
    else
        print("Unable, health is 0 or lower")
        script.Parent.ImageTransparency = 0
    end 
end

EDIT: Added check for full health, added check for humanoid.Health at or below 0

0
Yeah, thats somewhat near there. It still makes noise when I'm at Full Health. It is pretty close to the answer I need! i should edit the original post to give it more context RGamesDev 22 — 4y
1
Ok, I updated the code. appxritixn 2235 — 4y
0
You also need a check for humanoid.Health >  0, as is, this code can still error with divide by zero if health is 0, which will stop your loop running. EmilyBendsSpace 1025 — 4y
1
Updated with check for humanoid.Heatlh appxritixn 2235 — 4y
View all comments (2 more)
0
FYI on line 4, 1/(health/100) is equal to 100/health. hiimgoodpack 2009 — 4y
1
Ok.. On line 4, there is no mention of 1/(health/100)... appxritixn 2235 — 4y
Ad

Answer this question