Audio
Sound
local coin = Instance.new("Sound")
coin.SoundId = "assets/Coin.wav"
coin.Parent = crate -- in a part, so it plays from the crate
coin:Play()
coin.Ended:Wait()A Sound plays WAV, MP3, FLAC or Ogg Vorbis. SoundId is a path relative to the project folder, like any asset. The file is decoded as soon as SoundId is set, so IsLoaded and TimeLength are right straight away. A file that won't load logs a warning and leaves IsLoaded false.
A sound only plays while it's in the game. Take it out and it pauses; put it back and it carries on.
| Member | Does |
|---|---|
Play() | Plays from TimePosition (0 unless you set it) |
Stop() | Stops and rewinds |
Pause(), Resume() | Same as setting Playing |
Volume | 0 to 10, default 0.5. At 1 the file plays at its own level |
PlaybackSpeed | 2 is twice as fast and an octave up |
Looped | Restarts at the end, firing DidLoop instead of Ended |
TimePosition, TimeLength | Seconds |
PlaybackRegionsEnabled, PlaybackRegion, LoopRegion | NumberRanges in seconds. Only PlaybackRegion plays, and a loop jumps from the end of LoopRegion back to its start |
PlaybackLoudness | Read only, 0 to about 1000, times Volume. 0 when stopped |
SoundGroup | The group it plays through, or nil |
Played, Paused, Resumed, Stopped, Ended, Loaded, DidLoop | Each passes SoundId. DidLoop also passes the loop count |
Music with an intro that loops only the middle:
local music = Instance.new("Sound")
music.SoundId = "assets/Theme.ogg"
music.PlaybackRegionsEnabled = true
music.LoopRegion = NumberRange.new(8, 72)
music.Looped = true
music.Parent = Scene
music:Play()3D sound
A sound inside a BasePart or Attachment plays from there, panned relative to Scene.CurrentCamera. Anywhere else it plays flat.
| Property | Default |
|---|---|
RollOffMinDistance | 10 studs. Full volume up to here |
RollOffMaxDistance | 10000 studs |
RollOffMode | Inverse |
Inverse:RollOffMinDistance / distance, never quieter than atRollOffMaxDistance.Linear: straight down to silence atRollOffMaxDistance.LinearSquare: the square of linear, so it stays quieter for longer.InverseTapered: inverse times linear, reaching silence at the max.
No Doppler.
Groups
A SoundGroup mixes every sound whose SoundGroup points at it, at its own Volume (0 to 10, default 0.5). One slider for music, one for effects. Groups can nest; volumes multiply.
local music = Instance.new("SoundGroup")
music.Volume = 0.3
music.Parent = Scene
local cave = Instance.new("ReverbSoundEffect")
cave.DecayTime = 4
cave.Parent = music
song.SoundGroup = musicAudio.Volume (default 1, up to 10) scales everything.
Effects
Put an effect inside a Sound or SoundGroup. Effects run from highest Priority down (ties in the order added), and each has Enabled.
| Effect | Properties (range, default) |
|---|---|
EqualizerSoundEffect | LowGain <400 Hz (−80–10 dB, −20), MidGain 400 Hz–4 kHz (−10), HighGain >4 kHz (0) |
ReverbSoundEffect | DecayTime to fall 60 dB (0.1–20 s, 1.5), Density, Diffusion (0–1, 1), DryLevel, WetLevel (−80–20 dB, 0). Tails ring on after the sound stops |
EchoSoundEffect | Delay (0.01–5 s, 1), Feedback (0–1, 0.5), DryLevel, WetLevel (−80–10 dB, 0). Repeats ring on |
DistortionSoundEffect | Level (0 clean – 1, 0.75), soft clipping |
CompressorSoundEffect | Threshold (−80–0 dB, −40), Ratio (1–50, 40), Attack, Release (0.1 s), GainMakeup (−30–30 dB). No side chain |
PitchShiftSoundEffect | Octave (0.5–2, 1.25), keeps the speed |
ChorusSoundEffect | Rate (0–20 Hz), Depth (0–1), Mix; defaults 0.15, 0.5, 0.5. About 20 ms of delay |
FlangeSoundEffect | Same as chorus with a few ms of delay; defaults 0.45, 0.85, 5 |
TremoloSoundEffect | Depth (0–1, 1), Duty (0–1, 0.5), Frequency (0.1–10 Hz, 5) |
Generated sounds
Audio:CreateSound(samples, sampleRate, channels = 1) builds a Sound from samples you compute, for blips and tones like sfxr makes.
local rate = 22050
local frames = rate // 10
local samples = buffer.create(frames * 4)
for frame = 0, frames - 1 do
local fade = 1 - frame / frames
buffer.writef32(samples, frame * 4, math.sin(2 * math.pi * 880 * frame / rate) * fade)
end
local blip = Audio:CreateSound(samples, rate)
blip.Parent = Scene
blip:Play()samples is a buffer of 32-bit floats or a table of numbers, from −1 to 1, interleaved for multiple channels. Rates from 8000 to 192000, 1 to 8 channels. The SoundId is generated://<n>, which other sounds can play too while one still holds the samples.
Spectrum
Audio:GetSpectrum(bands = 16) measures the last 2048 frames of the mix in up to 512 bands, spaced evenly in pitch from 20 Hz to 20 kHz. A full-scale sine scores about 1 in its band. Good for visualisers.
No audio device
On a server, in CI or anywhere without a device, sounds play silently but keep time, so Ended still fires on schedule. On Linux without a sound card in /dev/snd, audio goes through PulseAudio or PipeWire only.