roblox-audio
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRoblox Audio
Roblox 音频系统
Roblox has two audio systems. The legacy system uses , , and objects. The new modular system uses , , , , and . The new system is preferred for new projects but the legacy system still works and is simpler for basic use cases.
SoundSoundGroupSoundEffectAudioPlayerAudioEmitterAudioListenerWireAudioDeviceOutputRoblox拥有两套音频系统:传统系统使用、和对象;新型模块化系统则采用、、、和。新项目优先推荐使用新型系统,但传统系统仍可正常运行,且在基础场景下更简便。
SoundSoundGroupSoundEffectAudioPlayerAudioEmitterAudioListenerWireAudioDeviceOutputLegacy System (Sound Objects)
传统系统(Sound对象)
Where to Place Sounds
音效放置位置
| Location | Behavior | Use for |
|---|---|---|
| Child of block/sphere/cylinder BasePart | Volumetric: emits from entire surface, volume changes with distance AND part size | Ambient zones, large area sounds |
| Child of Attachment, MeshPart, TrussPart, WedgePart | Point source: emits from single point, volume changes with distance | Footsteps, impacts, small objects |
| Within SoundService or Workspace | Global: same volume everywhere regardless of position | Background music, UI sounds |
| 位置 | 行为 | 适用场景 |
|---|---|---|
| 作为BasePart(方块/球体/圆柱体)的子对象 | 体积声源:从整个表面发声,音量随距离和部件大小变化 | 环境区域音效、大范围场景音效 |
| 作为Attachment、MeshPart、TrussPart、WedgePart的子对象 | 点声源:从单点发声,音量随距离变化 | 脚步声、碰撞声、小型物体音效 |
| 位于SoundService或Workspace中 | 全局声源:音量不随位置变化,全场一致 | 背景音乐、UI音效 |
Basic Playback
基础播放示例
luau
-- Background music (global, in SoundService)
local SoundService = game:GetService("SoundService")
local bgMusic = Instance.new("Sound")
bgMusic.Name = "BackgroundMusic"
bgMusic.SoundId = "rbxassetid://1843463175"
bgMusic.Looped = true
bgMusic.Volume = 0.25
bgMusic.Parent = SoundService
bgMusic:Play()luau
-- Positional sound (3D, on a part)
local waterfall = Instance.new("Sound")
waterfall.SoundId = "rbxassetid://ASSET_ID"
waterfall.Looped = true
waterfall.Volume = 0.5
waterfall.RollOffMode = Enum.RollOffMode.InverseTapered
waterfall.RollOffMinDistance = 10 -- full volume within this range
waterfall.RollOffMaxDistance = 100 -- silent beyond this range
waterfall.Parent = workspace.WaterfallPart
waterfall:Play()luau
-- 背景音乐(全局声源,置于SoundService)
local SoundService = game:GetService("SoundService")
local bgMusic = Instance.new("Sound")
bgMusic.Name = "BackgroundMusic"
bgMusic.SoundId = "rbxassetid://1843463175"
bgMusic.Looped = true
bgMusic.Volume = 0.25
bgMusic.Parent = SoundService
bgMusic:Play()luau
-- 3D定位音效(挂载在部件上)
local waterfall = Instance.new("Sound")
waterfall.SoundId = "rbxassetid://ASSET_ID"
waterfall.Looped = true
waterfall.Volume = 0.5
waterfall.RollOffMode = Enum.RollOffMode.InverseTapered
waterfall.RollOffMinDistance = 10 -- 此范围内保持最大音量
waterfall.RollOffMaxDistance = 100 -- 超出此范围音量降至0
waterfall.Parent = workspace.WaterfallPart
waterfall:Play()RollOff Modes
音量衰减模式
| Mode | Behavior |
|---|---|
| Realistic falloff (1/distance). Drops quickly near source, slowly far away. |
| Linear falloff between min and max distance. |
| Inverse near the source, tapers to silence at max. Most natural for games. |
| Attempt at realistic with linear square curve. |
- : distance (studs) where volume starts decreasing. Default 10.
RollOffMinDistance - : distance where volume reaches zero. Default 10000 (effectively infinite).
RollOffMaxDistance
| 模式 | 行为 |
|---|---|
| 真实物理衰减(1/距离):声源附近音量快速下降,远距离下降缓慢 |
| 最小与最大距离间线性衰减 |
| 声源附近为逆衰减,在最大距离处渐变为静音,是游戏中最自然的模式 |
| 模拟真实物理的线性平方曲线衰减 |
- :音量开始衰减的距离(单位: studs),默认值10
RollOffMinDistance - :音量降至0的距离,默认值10000(几乎无限制)
RollOffMaxDistance
SoundGroups (Audio Mixer)
SoundGroups(音频混音器)
SoundGroups let you control volume of multiple sounds at once. Nest them for a mix tree.
luau
-- Create mix hierarchy in SoundService
local master = Instance.new("SoundGroup")
master.Name = "Master"
master.Volume = 1
master.Parent = SoundService
local music = Instance.new("SoundGroup")
music.Name = "Music"
music.Volume = 0.5
music.Parent = master -- nested under Master
local sfx = Instance.new("SoundGroup")
sfx.Name = "SFX"
sfx.Volume = 0.8
sfx.Parent = master
-- Assign a sound to a group
bgMusic.SoundGroup = musicAdjusting affects ALL nested groups. Players can control Music vs SFX independently.
master.VolumeSoundGroups可同时控制多个音效的音量,还可嵌套形成混音层级。
luau
-- 在SoundService中创建混音层级
local master = Instance.new("SoundGroup")
master.Name = "Master"
master.Volume = 1
master.Parent = SoundService
local music = Instance.new("SoundGroup")
music.Name = "Music"
music.Volume = 0.5
music.Parent = master -- 嵌套在Master组下
local sfx = Instance.new("SoundGroup")
sfx.Name = "SFX"
sfx.Volume = 0.8
sfx.Parent = master
-- 将音效分配到对应组
bgMusic.SoundGroup = music调整会影响所有嵌套组的音量,玩家可独立控制音乐与音效的音量。
master.VolumeDynamic Effects
动态音效效果
Parent these to a Sound or SoundGroup to modify audio:
| Effect | What it does | Use for |
|---|---|---|
| Simulates room reflections | Caves, large halls, bathrooms |
| Control volume of frequency bands | Muffled underwater, radio effect |
| Reduces dynamic range | Consistent volume, ducking |
| Thickens sound with copies | Ethereal voices, dream sequences |
| Adds distortion/overdrive | Damaged radio, horror |
| Sweeping comb filter | Sci-fi, psychedelic |
| Changes pitch without speed | Chipmunk voice, deep voice |
| Periodic volume modulation | Vibrato, pulsing |
luau
-- Add reverb to all sounds in a cave zone
local caveGroup = Instance.new("SoundGroup")
caveGroup.Name = "Cave"
caveGroup.Parent = master
local reverb = Instance.new("ReverbSoundEffect")
reverb.DecayTime = 3.0
reverb.Density = 1.0
reverb.Diffusion = 1.0
reverb.WetLevel = -6 -- dB, how much reverb vs dry signal
reverb.Parent = caveGroup将以下效果挂载到Sound或SoundGroup上可修改音频:
| 效果 | 功能 | 适用场景 |
|---|---|---|
| 模拟房间反射效果 | 洞穴、大厅、浴室 |
| 控制不同频段音量 | 水下模糊音效、收音机效果 |
| 压缩动态范围 | 保持音量一致、音效闪避 |
| 通过复制音频增厚音效 | 空灵人声、梦境场景 |
| 添加失真/过载效果 | 损坏的收音机、恐怖场景 |
| 扫频梳状滤波效果 | 科幻、迷幻场景 |
| 不改变速度的前提下调整音调 | 花栗鼠音、低沉嗓音 |
| 周期性音量调制 | 颤音、脉冲音效 |
luau
-- 为洞穴区域的所有音效添加混响
local caveGroup = Instance.new("SoundGroup")
caveGroup.Name = "Cave"
caveGroup.Parent = master
local reverb = Instance.new("ReverbSoundEffect")
reverb.DecayTime = 3.0
reverb.Density = 1.0
reverb.Diffusion = 1.0
reverb.WetLevel = -6 -- 混响信号与干信号的比例(单位:dB)
reverb.Parent = caveGroupDucking (Lower music when SFX plays)
音效闪避(播放音效时降低音乐音量)
Use CompressorSoundEffect with a SideChain:
luau
-- Music ducks when SFX plays
local compressor = Instance.new("CompressorSoundEffect")
compressor.Threshold = -20
compressor.Ratio = 4
compressor.Attack = 0.01
compressor.Release = 0.2
compressor.SideChain = sfx -- SFX group triggers the compression
compressor.Parent = music -- Music group gets compressed使用带侧链的CompressorSoundEffect实现:
luau
-- 播放音效时自动降低音乐音量
local compressor = Instance.new("CompressorSoundEffect")
compressor.Threshold = -20
compressor.Ratio = 4
compressor.Attack = 0.01
compressor.Release = 0.2
compressor.SideChain = sfx -- 以SFX组作为触发源
compressor.Parent = music -- 对Music组应用压缩New Modular System (Audio Objects)
新型模块化系统(Audio对象)
The new system uses explicit wiring between audio components. Each object maps to a real-world audio device:
| Object | Real-world equivalent | Role |
|---|---|---|
| Media player / turntable | Produces audio from an asset |
| Speaker in 3D space | Emits audio positionally |
| Microphone | Picks up audio from 3D space |
| Headphones/speakers | Plays to the real player |
| Physical microphone | Captures real player voice |
| Audio cable | Carries stream between objects |
| TTS engine | Converts text to speech |
| EQ pedal | Modifies frequency response |
| Compressor pedal | Controls dynamic range |
| Reverb unit | Adds room reflections |
新型系统通过显式连接音频组件实现功能,每个对象对应现实中的音频设备:
| 对象 | 现实设备对应 | 角色 |
|---|---|---|
| 媒体播放器/唱片机 | 从资源生成音频 |
| 3D空间扬声器 | 播放定位音频 |
| 麦克风 | 拾取3D空间音频 |
| 耳机/扬声器 | 向玩家输出音频 |
| 物理麦克风 | 采集玩家语音 |
| 音频线缆 | 在对象间传输音频流 |
| TTS引擎 | 将文本转换为语音 |
| EQ效果器 | 调整频率响应 |
| 压缩效果器 | 控制动态范围 |
| 混响器 | 添加房间反射效果 |
2D Audio (non-positional)
2D音频(非定位)
AudioPlayer → Wire → AudioDeviceOutputluau
-- In SoundService:
local player = Instance.new("AudioPlayer")
player.AssetId = "rbxassetid://MUSIC_ID"
player.Looping = true
player.Volume = 0.5
player.Parent = SoundService
local output = Instance.new("AudioDeviceOutput")
output.Parent = SoundService
local wire = Instance.new("Wire")
wire.SourceInstance = player
wire.TargetInstance = output
wire.Parent = SoundService
player:Play()AudioPlayer → Wire → AudioDeviceOutputluau
-- 在SoundService中创建:
local player = Instance.new("AudioPlayer")
player.AssetId = "rbxassetid://MUSIC_ID"
player.Looping = true
player.Volume = 0.5
player.Parent = SoundService
local output = Instance.new("AudioDeviceOutput")
output.Parent = SoundService
local wire = Instance.new("Wire")
wire.SourceInstance = player
wire.TargetInstance = output
wire.Parent = SoundService
player:Play()3D Audio (positional)
3D音频(定位)
AudioPlayer → Wire → AudioEmitter (on Part)
AudioListener (on Camera/Character) → Wire → AudioDeviceOutputluau
-- On the part that emits sound:
local player = Instance.new("AudioPlayer")
player.AssetId = "rbxassetid://SFX_ID"
player.Parent = workspace.CampfirePart
local emitter = Instance.new("AudioEmitter")
emitter.Parent = workspace.CampfirePart
local wire = Instance.new("Wire")
wire.SourceInstance = player
wire.TargetInstance = emitter
wire.Parent = workspace.CampfirePart
-- AudioListener is auto-created by SoundService.ListenerLocation setting
-- AudioDeviceOutput is auto-created when ListenerLocation is Character or Camera
player:Play()AudioEmitter.DistanceAttenuationAudioPlayer → Wire → AudioEmitter(挂载在部件上)
AudioListener(挂载在相机/角色上) → Wire → AudioDeviceOutputluau
-- 在发声部件上创建:
local player = Instance.new("AudioPlayer")
player.AssetId = "rbxassetid://SFX_ID"
player.Parent = workspace.CampfirePart
local emitter = Instance.new("AudioEmitter")
emitter.Parent = workspace.CampfirePart
local wire = Instance.new("Wire")
wire.SourceInstance = player
wire.TargetInstance = emitter
wire.Parent = workspace.CampfirePart
-- AudioListener由SoundService.ListenerLocation设置自动创建
-- 当ListenerLocation设为Character或Camera时,AudioDeviceOutput会自动创建
player:Play()AudioEmitter.DistanceAttenuationTriggering Audio from Scripts
通过脚本触发音频
luau
-- Basic trigger pattern
local audioPlayer = script.Parent -- AudioPlayer
local part = audioPlayer.Parent
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChildOfClass("Humanoid") then
audioPlayer:Play()
end
end)luau
-- 基础触发模式
local audioPlayer = script.Parent -- AudioPlayer对象
local part = audioPlayer.Parent
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChildOfClass("Humanoid") then
audioPlayer:Play()
end
end)Patterns
实用模式
Preloading Critical Audio
预加载关键音频
luau
local ContentProvider = game:GetService("ContentProvider")
local criticalSounds = {
Instance.new("Sound"), -- temp instances for preloading
Instance.new("Sound"),
}
criticalSounds[1].SoundId = "rbxassetid://HIT_SOUND"
criticalSounds[2].SoundId = "rbxassetid://JUMP_SOUND"
ContentProvider:PreloadAsync(criticalSounds)
-- Now these assets are cached and play instantlyluau
local ContentProvider = game:GetService("ContentProvider")
local criticalSounds = {
Instance.new("Sound"), -- 用于预加载的临时对象
Instance.new("Sound"),
}
criticalSounds[1].SoundId = "rbxassetid://HIT_SOUND"
criticalSounds[2].SoundId = "rbxassetid://JUMP_SOUND"
ContentProvider:PreloadAsync(criticalSounds)
-- 现在这些资源已缓存,可立即播放One-Shot SFX (auto-cleanup)
一次性音效(自动清理)
luau
local function playSFX(parent: BasePart, soundId: string)
local sound = Instance.new("Sound")
sound.SoundId = soundId
sound.RollOffMode = Enum.RollOffMode.InverseTapered
sound.RollOffMinDistance = 10
sound.RollOffMaxDistance = 80
sound.Parent = parent
sound:Play()
sound.Ended:Once(function()
sound:Destroy()
end)
endluau
local function playSFX(parent: BasePart, soundId: string)
local sound = Instance.new("Sound")
sound.SoundId = soundId
sound.RollOffMode = Enum.RollOffMode.InverseTapered
sound.RollOffMinDistance = 10
sound.RollOffMaxDistance = 80
sound.Parent = parent
sound:Play()
sound.Ended:Once(function()
sound:Destroy()
end)
endClient-Side Playback
客户端播放
Play sounds on the client for zero-latency feedback. Server tells client WHAT to play:
luau
-- Server
SFXRemote:FireClient(player, "hit", workspace.Enemy.HumanoidRootPart.Position)
-- Client
SFXRemote.OnClientEvent:Connect(function(sfxName: string, position: Vector3)
local part = Instance.new("Part")
part.Position = position
part.Anchored = true
part.Transparency = 1
part.CanCollide = false
part.Parent = workspace
playSFX(part, SFX_IDS[sfxName])
task.delay(3, function() part:Destroy() end)
end)在客户端播放音效可实现零延迟反馈,服务器仅告知客户端需要播放的内容:
luau
-- 服务器端
SFXRemote:FireClient(player, "hit", workspace.Enemy.HumanoidRootPart.Position)
-- 客户端
SFXRemote.OnClientEvent:Connect(function(sfxName: string, position: Vector3)
local part = Instance.new("Part")
part.Position = position
part.Anchored = true
part.Transparency = 1
part.CanCollide = false
part.Parent = workspace
playSFX(part, SFX_IDS[sfxName])
task.delay(3, function() part:Destroy() end)
end)Common Mistakes
常见错误
- Playing sounds on the server for feedback: Server sounds have network latency. Play on client for responsive SFX. Server only for sounds ALL players must hear identically.
- No RollOff configuration: Default RollOffMaxDistance is 10000 studs (basically infinite). Set it explicitly or sounds bleed across the entire map.
- Stacking rapid sounds: Playing the same sound 10x in 0.1s creates ear-splitting overlap. Check or use a cooldown.
sound.IsPlaying - Forgetting Looped = false on one-shots: Default is false, but if you clone from a template that has Looped = true, your SFX plays forever.
- Not destroying one-shot sounds: Sounds parented to destroyed parts get GC'd, but sounds in SoundService accumulate. Always Destroy() after Ended.
- Volume > 1: Causes clipping/distortion. Keep ≤ 1. Use SoundGroup volume for amplification.
- Using legacy system for voice chat integration: The new AudioPlayer/Wire system is required for voice chat features (AudioDeviceInput, AudioSpeechToText).
- No SoundGroup hierarchy: Without groups, players can't independently control music vs SFX volume in settings.
- 在服务器播放反馈类音效:服务器音效存在网络延迟,应在客户端播放以获得响应迅速的音效,仅在所有玩家需听到完全一致的音效时使用服务器播放。
- 未配置RollOff参数:默认RollOffMaxDistance为10000 studs(几乎无限制),需显式设置,否则音效会传遍整个地图。
- 快速重复播放同一音效:0.1秒内播放10次同一音效会产生刺耳的重叠,应检查或使用冷却机制。
sound.IsPlaying - 一次性音效未关闭Looped:默认Looped为false,但如果从设置了Looped=true的模板克隆音效,会导致音效无限播放。
- 未销毁一次性音效:挂载在已销毁部件上的音效会被垃圾回收,但位于SoundService中的音效会不断累积,播放结束后务必调用Destroy()。
- 音量设置>1:会导致音频削波/失真,音量应保持≤1,可通过SoundGroup的音量实现放大。
- 使用传统系统集成语音聊天:语音聊天功能(AudioDeviceInput、AudioSpeechToText)需使用新型AudioPlayer/Wire系统。
- 未创建SoundGroup层级:没有分组的话,玩家无法在设置中独立控制音乐与音效的音量。