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

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

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

1 comment

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.

1 answer

✓ Accepted answer

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

6 comments

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

1

Ok, I updated the code.

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.

1

Updated with check for humanoid.Heatlh

0

FYI on line 4, 1/(health/100) is equal to 100/health.

1

Ok.. On line 4, there is no mention of 1/(health/100)...

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