SoundService
A service that determines various aspects of how Sounds play in the experience. SoundService is also often used to store SoundGroups, although this is not mandatory for groups to work.
SoundService properties such as AmbientReverb, DistanceFactor, DopplerScale and RolloffScale can be used to change how all Sounds play in the experience, and the SetListener() function allows you to set the position from where sounds are heard.
Code Samples
The code in this sample, when ran from a LocalScript, will change the SoundService.AmbientReverb property of SoundService when the player is inside a BasePart tagged using CollectionService.
To add or remove tags and reverb types, change the entries in the 'reverbTags' table.
local Players = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")
local SoundService = game:GetService("SoundService")
local localPlayer = Players.LocalPlayer
local reverbTags = {
["reverb_Cave"] = Enum.ReverbType.Cave,
}
-- collect parts and group them by tag
local parts = {}
for reverbTag, reverbType in pairs(reverbTags) do
for _, part in pairs(CollectionService:GetTagged(reverbTag)) do
parts[part] = reverbType
end
end
-- function to check if a position is within a part's extents
local function positionInPart(part, position)
local extents = part.Size / 2
local offset = part.CFrame:PointToObjectSpace(position)
return offset.x < extents.x and offset.y < extents.y and offset.z < extents.z
end
local reverbType = SoundService.AmbientReverb
while true do
task.wait()
if not localPlayer then
return
end
local character = localPlayer.Character
-- default to no reverb
local newReverbType = Enum.ReverbType.NoReverb
if character and character.PrimaryPart then
local position = character.PrimaryPart.Position
-- go through all the indexed parts
for part, type in pairs(parts) do
-- see if the character is within them
if positionInPart(part, position) then
-- if so, pick that reverb type
newReverbType = type
break
end
end
end
-- set the reverb type if it has changed
if newReverbType ~= reverbType then
SoundService.AmbientReverb = newReverbType
reverbType = newReverbType
end
end
Summary
Properties
The ambient sound environment preset used by SoundService.
Determines where (if anywhere) to place an AudioListener by default.
The number of studs to be considered a meter by SoundService when calculating volume attenuation of Sounds parented to a BasePart or Attachment.
Degree to which the pitch of a Sound varies due to the Doppler effect.
Sets whether Sound playback from a client will replicate to the server.
Sets how fast Sound volume attenuates.
Methods
Returns the current SoundService listener type and what is set as the listener.
Plays a Sound locally, meaning the sound will only be heard by the client calling this method, regardless of where it's parented to.
Sets the listener for the SoundService.