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

How can I make a sound file only play in a certain distance/radius?

I've been wondering about how I can make a sound file only be played in a certain distance and trail off the further away you get from the point of origin.

Several attempts on my part have failed.

I understand I should use 'Range' but from that point on, my endeavors have stopped.

(I'm trying to make it so it plays from a humanoid if the key (Script done) is pressed,)

2 answers

Just put your Sound object into a Part and set the Range property appropriately.

ROBLOX sounds placed inside parts already obey the inverse square law (twice as far, one quarter the volume). Though this is physical, it also means sounds can be heard from a very far distance away, which does not match our experience since other sounds / vibrations would drown them out.

In order to emulate / modify this behavior, though, we would be better using a Sound in the PlayerGui (which are heard at the same volume regardless of where you are), and having scripts adjust its volume based on distance.

We could then apply non-physical things like a cut-off distance.


For a LocalScript, you could use something like the following:

wait(1);
local player = game.Players.LocalPlayer;
local sound = player:WaitForChild("PlayerGui"):WaitForChild("Sound");
local soundPosition = Vector3.new(20,10,30);
-- Where the sound is in the world

while true do
	wait();
	local torsoPosition = player.Character.Torso.Position;
	local distance = (torsoPosition - soundPosition).magnitude;
	local volumeScale = 1 / (distance + 1) ^ 2;
	local volume = 0.45 * volumeScale;
	-- 0.45 being maximum volume in the above
	if volume < 0.03 then
		-- Very quiet, so we'll just silence it instead
		sound.Volume = 0;
	else
		sound.Volume = volume;
	end
end

3 comments

0

And to change this to a player I would?

0

... what?

0

This isn't working well. This could use a distance adjustment because distance in studs does not work well with the inverse square law.

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