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

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

Asked by 9 years ago

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

Log in to vote
0
Answered by 9 years ago

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

Ad
Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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
0
And to change this to a player I would? MATTY1319 10 — 9y
0
... what? BlueTaslem 18071 — 9y
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. RAYAN1565 691 — 5y

Answer this question