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);
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;
if volume < 0.03 then
sound.Volume = 0;
else
sound.Volume = volume;
end
end
3 comments
And to change this to a player I would?
... what?
This isn't working well. This could use a distance adjustment because distance in studs does not work well with the inverse square law.