Sound

Show Deprecated

A Sound is an object that emits sound.

2D and 3D Sound

A sound placed in a BasePart or an Attachment will emit its sound from that part's BasePart.Position or the attachment's Attachment.WorldPosition. A sound exhibits the Doppler effect, meaning its frequency and pitch varies with the relative motion of whatever attachment or part it is attached to. The volume of the sound will be determined by the distance between the client's sound listener (by default the Camera position) and the position of the sound's parent. For more information on this see Sound.RollOffMode.

A sound is considered "global" if it is not parented to a BasePart or an Attachment. In this case, the sound will play at the same volume throughout the entire place.

Code Samples

Music Playing Part

local part = Instance.new("Part")
part.Anchored = true
part.Position = Vector3.new(0, 3, 0)
part.BrickColor = BrickColor.new("Bright red")
part.Name = "MusicBox"
part.Parent = workspace
-- create a sound
local sound = Instance.new("Sound", part)
sound.SoundId = "rbxassetid://9120386436"
sound.EmitterSize = 5 -- decrease the emitter size (for earlier volume drop off)
sound.Looped = true
sound.Parent = part
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = part
-- toggle the sound playing / not playing
clickDetector.MouseClick:Connect(function()
if not sound.IsPlaying then
part.BrickColor = BrickColor.new("Bright green")
sound:Play()
else
part.BrickColor = BrickColor.new("Bright red")
sound:Stop()
end
end)
Sound in the Workspace

local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
sound:Play()

Summary

Properties

  • read only
    not replicated
    not browsable
    read parallel
  • read only
    not replicated
    read parallel

    This property will be true when the Sound has loaded from Roblox servers and is ready to play.

  • hidden
    read only
    not replicated
    read parallel

    This read-only property will return true when the Sound is not playing.

  • hidden
    read only
    not replicated
    read parallel

    This read-only property will return true when the Sound is playing.

  • read parallel

    A range denoting a desired loop-start and loop-end within the Sound.PlaybackRegion, in seconds.

  • read parallel

    This sets whether or not the Sound repeats once it has finished when it is playing.

  • read parallel

    When true, the Sound will play when it is removed from the game, by parenting the Sound or one if its ancestors to nil.

  • read only
    not replicated
    read parallel

    A number between 0 and 1000 indicating how loud the Sound is currently playing back.

  • A range denoting a desired start (min) and stop (max) time within the Sound.TimeLength, in seconds.

  • If true, this property gives your Sound access to the Sound.PlaybackRegion and Sound.LoopRegion properties, which can more-accurately control its playback.

  • not replicated
    read parallel

    Determines the speed at which a Sound will play, with higher values causing the sound to play faster and at a higher pitch.

  • not replicated
    read parallel

    Indicates whether the Sound is currently playing.

  • not replicated
    read parallel

    The maximum distance, in studs, 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).

  • not replicated
    read parallel

    The minimum distance, in studs, at which a 3D Sound (direct child of a BasePart or Attachment) will begin to attenuate (decrease in volume).

  • Controls how the volume of a 3D Sound (parented to a BasePart or Attachment) behaves as the distance between the listener and sound's parent changes.

  • read parallel

    The SoundGroup that is linked to this Sound. Volume and SoundEffects applied to this sound group will pass onto the sound. A sound can only be in one sound group at a time.

  • read parallel

    This property is the content ID of the sound file a Sound object is associated with. Once a sound has been uploaded to Roblox the content ID can be found in the uploaded sound's URL.

  • read only
    not replicated
    read parallel

    The length of the Sound in seconds. If the Sound is not loaded, this value will be 0.

  • not replicated
    read parallel

    Shows the progress in seconds of the Sound. Can be changed to move the playback position of the Sound both before and during playback.

  • read parallel

    The volume of the Sound. Can be set between 0 and 10. Defaults to 0.5.

Methods

Events

Properties

ChannelCount

read only
not replicated
not browsable
read parallel

IsLoaded

read only
not replicated
read parallel

This property will be true when the Sound has loaded from Roblox servers and is ready to play.

In Roblox, audio files are not stored in games themselves but hosted on the Roblox servers and referenced by the Sound.SoundId property. This means that they need to be downloaded to a client's device before they can be played. This can take a while depending on the user's internet connection, the length of the sound and the number of other objects that need to be loaded.

Developers can use the Sound.IsLoaded property and the Sound.Loaded event if they wish to verify a sound has loaded before playing it.

Code Samples

Load Sound

local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("The sound has loaded!")

IsPaused

hidden
read only
not replicated
read parallel

This read-only property will return true when the Sound is not playing. Note that this property will not only return true once a sound has been paused using the Sound:Pause() function but also if it has been stopped using the Sound:Stop() function or never been played.

This property will only be true when Sound.IsPlaying is false.

As IsPaused is read only it can not be used to stop the sound, Sound:Stop() and Sound:Pause() should be used instead.

Code Samples

Sound IsPlaying and SoundIsPaused

local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
if not sound.isLoaded then
sound.Loaded:Wait()
end
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- true, false
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- false, true
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- true, false
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- false, true

IsPlaying

hidden
read only
not replicated
read parallel

This read-only property will return true when the Sound is playing.

This property can only be true when Sound.IsPaused is false.

As IsPlaying is read only it can not be used to play the sound, Sound:Play() should be used instead.

Code Samples

Sound IsPlaying and SoundIsPaused

local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
if not sound.isLoaded then
sound.Loaded:Wait()
end
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- true, false
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- false, true
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- true, false
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- false, true

LoopRegion

read parallel

A range denoting a desired loop-start and loop-end within the Sound's Sound.PlaybackRegion in seconds.

Looped

read parallel

This sets whether or not the Sound repeats once it has finished when it is playing.

Looped sounds are suitable for a range of applications including music and background ambient sounds. The Sound.DidLoop event can be used to track the number of times as sound has looped.

Code Samples

Sound Looping

local Players = game:GetService("Players")
local function onPlayerAdded(player)
local function onCharacterAdded(character)
-- wait for the head to be added
local head = character:WaitForChild("Head")
local sound = Instance.new("Sound")
sound.Name = "TestSound"
sound.SoundId = "rbxasset://sounds/uuhhh.mp3" -- oof
sound.Parent = head
sound.Looped = true
sound.DidLoop:Connect(function(_soundId, numOfTimesLooped)
print("oof! " .. tostring(numOfTimesLooped))
end)
sound:Play()
end
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)
Loop a Number of Times

local function loopNTimes(sound, numberOfLoops)
if not sound.IsPlaying then
sound.Looped = true
local connection = nil
connection = sound.DidLoop:Connect(function(_soundId, numOfTimesLooped)
print(numOfTimesLooped)
if numOfTimesLooped >= numberOfLoops then
-- disconnect the connection
connection:Disconnect()
-- stop the sound
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)

PlayOnRemove

read parallel

When true, the Sound will play when it is removed from the game.

Note the sound will play when the Instance.Parent property of the Sound or one of its ancestors is set to nil. This means all of the following will cause the sound to play when PlayOnRemove is true. Note, this includes Instance:Destroy() as the destroy function sets the parent to nil.


sound:Destroy()
sound.Parent = nil
sound.Parent.Parent = nil

Code Samples

Sound PlayOnRemove

local sound = Instance.new("Sound")
sound.Name = "TestSound"
sound.SoundId = "rbxasset://sounds/uuhhh.mp3" -- oof
sound.Parent = workspace
sound.PlayOnRemove = true
task.wait(3)
sound:Destroy()

PlaybackLoudness

read only
not replicated
read parallel

A number between 0 and 1000 indicating how loud the Sound is currently playing back.

This property reflects the amplitude of the sound's playback in the instance of time it is read. Therefore, for most sounds it will fluctuate constantly. Due to this it can appear in the Roblox Studio properties window as 0, however when read by code in the command bar or Scripts it will return the correct value.

Code Samples

Volume Amplitude Bar

-- to be placed in StarterPlayer > StarterPlayerScripts
local Players = game:GetService("Players")
-- wait for local player PlayerGui
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")
-- create a ScreenGui
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = playerGui
-- create a holder for our bar
local frame = Instance.new("Frame")
frame.AnchorPoint = Vector2.new(0.5, 0.5)
frame.Position = UDim2.new(0.5, 0, 0.5, 0)
frame.Size = UDim2.new(0.3, 0, 0.05, 0)
frame.Parent = screenGui
-- create a bar
local bar = Instance.new("Frame")
bar.Position = UDim2.new(0, 0, 0, 0)
bar.Size = UDim2.new(1, 0, 1, 0)
bar.BackgroundColor3 = Color3.new(0, 1, 0)
bar.Parent = frame
-- create a sound
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = screenGui
sound:Play()
-- define a max loudness
local maxLoudness = 30
-- animate the amplitude bar
while true do
local amplitude = math.clamp(sound.PlaybackLoudness / maxLoudness, 0, 1)
bar.Size = UDim2.new(amplitude, 0, 1, 0)
wait(0.05)
end

PlaybackRegion

read parallel

A range denoting a desired start (min) and stop (max) time within the Sound.TimeLength, in seconds.

  • If PlaybackRegion.Min > 0, the sound begins to play from the PlaybackRegion.Min time.

  • If PlaybackRegion.Min < 0, the sound begins to play from 0.

  • If PlaybackRegion.Max > Sound.TimeLength, the sound stops at Sound.TimeLength.

  • If PlaybackRegion.Max < Sound.TimeLength, the sound stops at exactly that time.

  • If PlaybackRegion.Min == PlaybackRegion.Max, the PlayBackRegion is inactive.

PlaybackRegionsEnabled

read parallel

If true, this property gives your Sound access to the Sound.PlaybackRegion and Sound.LoopRegion properties, which can more-accurately control its playback.

PlaybackSpeed

not replicated
read parallel

Determines the speed at which a Sound will play. The greater the value the faster the sound will play back.

For example, a value of 2 will cause the Sound to play 2x faster, whereas a value of 0.5 will cause it to play 2x slower. When PlaybackSpeed is equal to 1, the sound will take Sound.TimeLength (in seconds) to complete.

Note that increasing the PlaybackSpeed of a sound will cause it to play at a higher pitch.

Code Samples

Sound PlaybackSpeed

local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
sound:Play()
task.wait(10)
sound.PlaybackSpeed = 3 -- 3x faster
task.wait(5)
sound.PlaybackSpeed = 0.5 -- 2x slower
task.wait(5)
sound.PlaybackSpeed = 1 -- default

Playing

not replicated
read parallel

Indicates whether the Sound is currently playing. This can be toggled, and this property will always replicate.

In the studio editor, Sounds are not playable because time is halted. Setting Sound.Playing to true in edit mode doesn't do anything. However, you can play audio from within plugins by setting the Sounds as descendants of a PluginGui.

This property should not be confused with Sound.IsPlaying which is a read-only property. Playing can be set to true or false to start or stop the playback of a sound.

Note that when Playing is set to false, the Sound.TimePosition property of the sound will not reset. This means when the Playing is set to true again the audio will continue from the time position it was at when it was stopped. However, if the Sound:Play() function is used to resume the sound the time position will reset to 0.

Code Samples

Sound Playing

local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
print("playing sound")
sound.Playing = true
task.wait(10)
print("stopping sound")
sound.Playing = false
task.wait(5)
sound.Playing = true
local timePosition = sound.TimePosition
print("resumed at time position: " .. tostring(timePosition)) -- c. 10 seconds

RollOffMaxDistance

not replicated
read parallel

The maximum distance, in studs, 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 RollOffMaxDistance impacts the attenuation of a sound (manner in which it fades out) is dependent on the Sound.RollOffMode property. When RollOffMode is set to use an inverse type distance model (Inverse or InverseTapered) the RollOffMaxDistance will not effect the attenuation of the sound. This means that low values for RollOffMaxDistance will cause the sound to abruptly cut off when the listener reaches the RollOffMaxDistance. In most cases this is not desirable and developers are advised not to use low RollOffMaxDistance values.

When 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 RollOffMaxDistance). This is less realistic, but in some cases allows attenuation to be handled in a more intuitive way.

RollOffMinDistance

not replicated
read parallel

The minimum distance, in studs, at which a 3D Sound (direct child of a BasePart or Attachment) will begin to attenuate (decrease in volume).

Sounds parented to a BasePart or Attachment that are descendants of the Workspace are considered 3D sounds and their volume while playing is dependent on the distance between the client's sound listener (Camera position by default) and the Sound's parent. Three properties influence this behavior RollOffMinDistance, Sound.RollOffMaxDistance, and Sound.RollOffMode.

The way the Sound attenuates (fades out) after the distance between the listener and the sound exceeds the RollOffMinDistance is determined by the RollOffMode.

RollOffMode

read parallel

This property sets how 3D Sounds attenuate (fade out) as the distance between the listener and the sound's parent increase. It can be set to one of the values of the Enum.RollOffMode enum.

The following code will set RollOffMode to Linear:


sound.RollOffMode = Enum.RollOffMode.Linear

The different modes

The following options are available:

ModeDescription
InverseVolume attenuates from Sound/RollOffMinDistance in an inverse manner.
InverseTaperedA hybrid model. Follows the Inverse model when close to RollOffMinDistance and the Linear Square model when close to Sound/RollOffMaxDistance.
LinearVolume attenuates between RollOffMinDistance and Sound/RollOffMaxDistance with a linear relationship.
LinearSquareVolume attenuates between RollOffMinDistance and Sound/RollOffMaxDistance with a linear squared relationship.

Inverse vs Linear Distance Attenuation

By default sounds are set to use inverse distance attenuation (Enum.RollOffMode.Inverse) which mirrors how sounds attenuate in the real world. Under inverse distance attenuation, sounds will begin to attenuate once the distance between the listener and the Sound's parent exceeds RollOffMinDistance. The rate of attenuation depends on the emitter size, as sounds with larger EmitterSize's will attenuate at a slower rate. Inverse rate of inverse distance attenuation is further influenced by SoundService.RolloffScale.

RollOffMaxDistance will not effect attenuation under the inverse model but will cause the sound to cut off completely once this distance is reached. This can be particularly abrupt when using low values for max distance.

Linear distance attenuation works differently. Under linear distance attenuation the sound will attenuate between RollOffMinDistance and RollOffMaxDistance, falling silent once MaxDistance is reached. RollOffMinDistance still denotes the point at which the sound will begin attenuating. However, the audible volume at any point now depends on the point the listener is at between EmitterSize and MaxDistance. This means, in contrast to the inverse distance attenuation model, the audible volume of the sound will approach silence at MaxDistance point. This is less realistic, but may be more desirable in some cases.

SoundGroup

read parallel

The SoundGroup that is linked to this Sound. SoundGroup.Volume and SoundEffects applied to this sound group will pass onto the sound. A sound can only be in one sound group at a time.

SoundGroups are used to manage the volume and effects of multiple Sounds at once. A Sound is added to a SoundGroup by setting the SoundGroup property of the sound.

Code Samples

SoundGroups

local SoundService = game:GetService("SoundService")
-- create a sound group
local soundGroup = Instance.new("SoundGroup")
soundGroup.Parent = SoundService
-- create a sound
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.PlaybackSpeed = 2
sound.SoundGroup = soundGroup
sound.Parent = workspace
-- play the sound
sound:Play()
task.wait(10)
-- change the volume
soundGroup.Volume = 0.1
task.wait(3)
-- return the volume
soundGroup.Volume = 0.5
task.wait(4)
-- add a sound effect
local reverb = Instance.new("ReverbSoundEffect")
reverb.Parent = soundGroup

SoundId

read parallel

This property is the content ID of the sound file a Sound object is associated with. Once a sound has been uploaded to Roblox the content ID can be found in the uploaded sound's URL.

It's important to remember the URL is not the same as the content ID. It will work when pasted directly into the SoundId property of a Sound in Roblox studio, as Studio will automatically correct it, however if it is being set from a Script then the correct content ID will need to be used, using the number from the URL. For example:


"https://www.roblox.com/catalog/9120386436" -- Web URL (will not work)
"http://www.roblox.com/asset/?id=9120386436" -- Content ID (will work)
"rbxassetid://9120386436" -- Content ID (alternative version, will work)

Code Samples

Sound in the Workspace

local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
sound:Play()

TimeLength

read only
not replicated
read parallel

The length of the Sound in seconds. If the Sound is not loaded, this value will be 0.

This property is often used in conjunction with Sound.PlaybackSpeed to adjust the speed of a sound so it lasts for a specific duration (see examples). When Sound.PlaybackSpeed is equal to 1, the sound will take TimeLength seconds to complete.

Code Samples

Play a Sound for a Specific Duration

local function playForDuration(sound, duration)
if not sound.IsLoaded then
sound.Loaded:wait()
end
local speed = sound.TimeLength / duration
sound.PlaybackSpeed = speed
sound:Play()
end
local sound = script.Parent
playForDuration(sound, 5)

TimePosition

not replicated
read parallel

Shows the progress in seconds of the Sound. Can be changed to move the playback position of the sound. If the sound is already playing then playback will snap to the specified position. If it is not playing the Sound will begin playback at the set position when the sound is next played.

As a Sound is played, TimePosition increases at a rate of Sound.PlaybackSpeed per second. Once TimePosition reaches Sound.TimeLength the Sound will stop unless it is looped. This means, unless Sound.Looped is set to true setting TimePosition to a value equal or greater to Sound.TimeLength will stop the sound.

Note that setting TimePosition to a value greater than the length in a looped track will not cause it to wrap around. If that behavior is desired the developer should do the following.


if newPosition >= sound.TimeLength then
newPosition = newPosition - sound.TimeLength
end
sound.TimePosition = newPosition

Setting TimePosition to a value less than zero currently does not influence playback, but this behavior should not be relied upon.

Code Samples

Sound TimePosition

local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Parent = workspace
sound.TimePosition = 30
sound:Play()
task.wait(5)
sound.TimePosition = 100
task.wait(5)
sound.TimePosition = 0

Volume

read parallel

The volume of the Sound. Can be set between 0 and 10. Defaults to 0.5

Note that if the Sound is a member of a SoundGroup its playback volume (but not the Volume property) will be influenced by the SoundGroup.Volume property of that SoundGroup. The effect of this is multiplicative, meaning that a sound will play at the same volume if its Volume is 0.1 and its SoundGroup volume is 5 and vice-versa.

Code Samples

Sound Volume

local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.PlaybackSpeed = 2
sound.Parent = workspace
sound.Volume = 2
sound:Play()
task.wait(7)
sound.Volume = 0.2

Methods

Pause

void

Sets Sound.Playing to false. This pauses the playback of the Sound if the sound is playing. Unlike Sound:Stop() it does not reset Sound.TimePosition meaning the sound can be resumed using Sound:Resume().

The impact of the different Sound functions on Sound.Playing and Sound.TimePosition are shown below.

FunctionSound.PlayingSound.TimePosition
Sound:Play()TrueLast value set in Lua (default 0)
Sound:Pause()False-
Sound:Resume()True-
Sound:Stop()False0

Returns

void

Code Samples

Sound Functions

-- create a sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- listen for events
sound.Played:Connect(function(_soundId)
print("Sound.Played event")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused event")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed event")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped event")
end)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0

Play

void

Plays the Sound. Sets Sound.TimePosition to the last value set by a Script (or 0 if it has not been set), and then sets Sound.Playing to true.

The impact of the different Sound functions on Sound.Playing and Sound.TimePosition are shown below.

FunctionSound.PlayingSound.TimePosition
Sound:Play()TrueLast value set in Lua (default 0)
Sound:Pause()False-
Sound:Resume()True-
Sound:Stop()False0

Returns

void

Code Samples

Sound Functions

-- create a sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- listen for events
sound.Played:Connect(function(_soundId)
print("Sound.Played event")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused event")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed event")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped event")
end)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0

Resume

void

Resumes the Sound. Sets Sound.Playing to true. Does not alter Sound.TimePosition and thus can be used to resume the playback of a sound stopped using Sound:Pause().

The impact of the different sound functions on Sound.Playing and Sound.TimePosition are shown below.

FunctionSound.PlayingSound.TimePosition
Sound:Play()TrueLast value set in Lua (default 0)
Sound:Pause()False-
Sound:Resume()True-
Sound:Stop()False0

Returns

void

Code Samples

Sound Functions

-- create a sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- listen for events
sound.Played:Connect(function(_soundId)
print("Sound.Played event")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused event")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed event")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped event")
end)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0

Stop

void

Stops the Sound. Sets Sound.Playing to false then sets Sound.TimePosition to 0.

The impact of the different sound functions on Sound.Playing and Sound.TimePosition are shown below.

FunctionSound.PlayingSound.TimePosition
Sound:Play()TrueLast value set in Lua (default 0)
Sound:Pause()False-
Sound:Resume()True-
Sound:Stop()False0

Returns

void

Code Samples

Sound Functions

-- create a sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- listen for events
sound.Played:Connect(function(_soundId)
print("Sound.Played event")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused event")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed event")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped event")
end)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0

Events

DidLoop

Event that fires whenever the Sound loops. Returns soundId and numOfTimesLooped, giving the contentID of the sound and the number of times looped respectively.

When the Sound is stopped the looped counter resets meaning the next DidLoop event will return 1 for numOfTimesLooped.

Parameters

soundId: string

The Sound.SoundId of the Sound that looped.

numOfTimesLooped: number

The number of times the Sound has looped.


Code Samples

Loop a Number of Times

local function loopNTimes(sound, numberOfLoops)
if not sound.IsPlaying then
sound.Looped = true
local connection = nil
connection = sound.DidLoop:Connect(function(_soundId, numOfTimesLooped)
print(numOfTimesLooped)
if numOfTimesLooped >= numberOfLoops then
-- disconnect the connection
connection:Disconnect()
-- stop the sound
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)

Ended

Fires when the Sound has completed playback and stopped. Note this event will not fire for sounds with Sound.Looped set to true as they continue playing upon reaching their end.

This event is often used to destroy a sound when it has completed playback.


sound:Play()
sound.Ended:Wait()
sound:Destroy()

This event only fires if the sound has reached its end. This means it will also not fire when the sound is stopped before playback has completed, for this use Sound.Stopped.

Parameters

soundId: string

The Sound.SoundId of the Sound that has ended.


Loaded

The Sound.Loaded event fires when the Sound is loaded.

Note this event will only fire at the time the sound is loaded. This means if it is listened for when the sound is already loaded it will not return. Therefore it is recommended to check Sound.IsLoaded prior to connecting to this event.

Parameters

soundId: string

The Sound.SoundId of the Sound that loaded.


Code Samples

Load Sound

local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("The sound has loaded!")

Paused

Fires whenever the Sound is paused using Sound:Pause().

As with Sound.Played, Sound.Resumed and Sound.Stopped only the respective sound function will cause the event to fire. This means that Pause will only fire when Sound:Pause() is called.

Parameters

soundId: string

The Sound.SoundId of the Sound that was paused.


Code Samples

Sound Functions

-- create a sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- listen for events
sound.Played:Connect(function(_soundId)
print("Sound.Played event")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused event")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed event")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped event")
end)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0

Played

Fires whenever the Sound is played using the Sound:Play() function.

As with Sound.Stopped, Sound.Paused and Sound.Resumed only the respective sound function will cause the event to fire. This means that Played will only fire when Sound:Play() is called. This event will not fire if the Sound is played due to the sound being destroyed and Sound.PlayOnRemove being set to true.

Parameters

soundId: string

The Sound.SoundId of the Sound that was played.


Code Samples

Sound Functions

-- create a sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- listen for events
sound.Played:Connect(function(_soundId)
print("Sound.Played event")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused event")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed event")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped event")
end)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0

Resumed

Fires when the Sound is resumed using Sound:Resume().

As with Sound.Played, Sound.Paused and Sound.Stopped only the respective sound function will cause the event to fire. This means that Resumed will only fire when Sound:Resume() is called.

Parameters

soundId: string

The Sound.SoundId of the Sound being resumed.


Code Samples

Sound Functions

-- create a sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- listen for events
sound.Played:Connect(function(_soundId)
print("Sound.Played event")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused event")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed event")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped event")
end)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0

Stopped

Fires when the Sound is stopped due to the Sound:Stop() function.

As with Sound.Played, Sound.Paused and Sound.Resumed only the respective sound function will cause the event to fire. This means that Stopped will only fire when Sound:Stop() is called. Destroying a sound while it is playing will not cause this event to fire.

Parameters

soundId: string

The Sound.SoundId of the Sound that stopped.


Code Samples

Sound Functions

-- create a sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- listen for events
sound.Played:Connect(function(_soundId)
print("Sound.Played event")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused event")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed event")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped event")
end)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0