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,)
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