How to make the volume higher when something gets closer

Hello,

So what i want is when a object comes closer to you, a noise sound will increase volume.
I want the sound increase when the magnitude is equal or lower than 50.

LocalScript:

local plr = game.Players.LocalPlayer
local image = script.Parent.Static
local noise = script:WaitForChild("Static")

game:GetService("RunService").Heartbeat:connect(function()
	local mag = (workspace["Red Killer"].HumanoidRootPart.Position - plr.Character.HumanoidRootPart.Position).Magnitude
	if plr.Character and plr.Character:FindFirstChild('HumanoidRootPart') and mag <= 50 then
		image.ImageTransparency = (mag*2)/100
		noise.Volume = -- I don't know!!
	end
end)
1 Like

what is the maximum volume you want?
like if magnitude was 0

I would like it to be 1.
It doesn’t really matter because if it was 5 i can just divide by 5.

Thank you for trying to help!

local players = game:GetService("Players")
local runService = game:GetService("RunService")

local plr = players.LocalPlayer
local image = script.Parent.Static
local noise = script:WaitForChild("Static")

runService.Heartbeat:Connect(function()
    local mag = (workspace["Red Killer"].HumanoidRootPart.Position - plr.Character.HumanoidRootPart.Position).Magnitude

    if plr.Character and plr.Character:FindFirstChild('HumanoidRootPart') and mag <= 50 then
        image.ImageTransparency = (mag * 2) / 100
        noise.Volume = (50 - mag) * 0.01 + 0.5
    end
end)

is this what you want?
it should work

Hello again,

It is in the correct way.
The only thing is i want 50 to be max and not 100.
If the magnitude is 100 then its 0 while 50 must be 0.

Thank you for the reply!

I never made 100 the max?
50 is still the max

local players = game:GetService("Players")
local runService = game:GetService("RunService")

local plr = players.LocalPlayer
local image = script.Parent.Static
local noise = script:WaitForChild("Static")

runService.Heartbeat:Connect(function()
    local mag = (workspace["Red Killer"].HumanoidRootPart.Position - plr.Character.HumanoidRootPart.Position).Magnitude

    if plr.Character and plr.Character:FindFirstChild('HumanoidRootPart') then
        image.ImageTransparency = if mag <= 50 then (mag * 2) / 100 else 1
        noise.Volume = if mag <= 50 then (50 - mag) * 0.02 else 0
    end
end)

you could try this code

I guess you’re doing this because none of the default modes perform the way you’d like?

https://developer.roblox.com/en-us/api-reference/property/Sound/RollOffMode

Whell, the thing is when i do print((50 - mag) * 0.01 + 0.5) while mag = 100 then i get 0.
I want mag to be 50 and get 0 and not 0.5.

oh my bad

edited my recent post just now to include (50 - mag) * 0.02 instead

thanks for telling me

Thank you!
That’s exactly what i need!

https://developer.roblox.com/en-us/api-reference/property/Sound/RollOffMode

You can also just toy around with this property until you get the desired effect.

Sir_Highness already said that in his reply
don’t know why you replied with that

He should’ve been given the solution mark then since it’s usually better to use that than to manage sound programmatically.

well I agree with you, I’m just saying look before you reply

Why? 2 people offering the same solution is typically a good indication that, that solution is the correct one to use.

well you could have expanded on the property
if you don’t want to expand on it, then why reply with the same thing

your reply seemed like a copy of the other reply, which imo seems like it doesn’t have any purpose being there

why do you care how long I take?
wasn’t typing the whole time

I’m not gonna respond anymore anyways, I gtg