PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

MaxDistance

Sound

float

The maximum distance a client’s listener can be from the Sound's origin and still hear it. Only applies to Sounds parented to a Part or Attachment (3D sounds).

How MaxDistance impacts the attenuation of a sound (manner in which it fades out) is dependent on the Sound/RollOffMode property. When Sound/RollOffMode is set to use an inverse type distance model (Inverse or InverseTapered) the MaxDistance will not effect the attenuation of the sound. This means that low values for MaxDistance will cause the sound to abruptly cut off when the listener reaches the MaxDistance. In most cases this is not desirable and developers are advised not to use low MaxDistance values.

When Sound/RollOffMode is set to a linear type distance model (Linear or LinearSquared) the sound will attenuate between Sound/EmitterSize and MaxDistance (with playback volume reaching zero at MaxDistance). This is less realistic, but in some cases allows attenuation to be handled in a more intuitive way.


Code Samples


Sound MaxDistance

This sample includes a brief demonstration of how Sound.MaxDistance works.

A Part is instanced in the Workspace with a looped Sound playing within it. A function is made to listen to the Sound’s MaxDistance and change the size of a sphere whenever it changes. The sphere’s radius will always be equal to the Sound’s MaxDistance.

This example helps demonstrate how the sound is only audible when the client’s listener (by default the Camera’s position) is within the sphere.

-- create a part
local part = Instance.new("Part")
part.Position = Vector3.new(0, 3, 0)
part.Size = Vector3.new(1, 1, 1)
part.Anchored = true
part.Parent = game.Workspace

-- create a part to visualise the MaxDistance
local sphere = Instance.new("Part")
sphere.Name = "MaxDistanceSphere"
sphere.CFrame = part.CFrame
sphere.Anchored = true
sphere.CanCollide = false
sphere.Size = Vector3.new(1, 1, 1)
sphere.Shape = Enum.PartType.Ball
sphere.Transparency = 0.8
sphere.BrickColor = BrickColor.new("Lime green")
sphere.Parent = game.Workspace

-- create a looped sound
local sound = Instance.new("Sound", part)
sound.Name = "TestSound"
sound.MaxDistance = 20
sound.SoundId = "rbxasset://sounds/uuhhh.mp3" -- oof
sound.Looped = true

-- a function that changes the sphere size to reflect the sound's MaxDistance
local function updateSize()
	local distance = sound.MaxDistance
	sphere.Size = Vector3.new(distance * 2, distance * 2, distance * 2)
end

-- update the sphere size
updateSize()
-- listen for any changes to MaxDistance
sound:GetPropertyChangedSignal("MaxDistance"):Connect(updateSize)

-- play the sound
sound:Play()