Sound
*Pronto este contenido estará disponible en tu idioma seleccionado.
Sound is an object that emits sound. When placed in a BasePart or an Attachment, this object will emit its sound from that part's BasePart.Position or the attachment's Attachment.WorldPosition. In this placement, 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. Additionally, its volume 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, see 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.
Muestras de código
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)
Resumen
Propiedades
This property is true when the Sound has loaded from Roblox servers and is ready to play.
Read-only property which returns true when the Sound is not playing.
Read-only property which returns true when the Sound is playing.
A range denoting a desired loop start and loop end within the PlaybackRegion, in seconds.
Sets whether or not the Sound repeats once it has finished playing.
When true, the Sound will play when it is removed from the experience.
A number between 0 and 1000 indicating how loud the Sound is currently playing back.
A range denoting a desired start and stop time within the TimeLength, in seconds.
If true, this property gives your Sound access to the PlaybackRegion and LoopRegion properties which can more-accurately control its playback.
Determines the speed at which a Sound will play, with higher values causing the sound to play faster and at a higher pitch.
Indicates whether the Sound is currently playing.
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 BasePart or Attachment.
The minimum distance, in studs, at which a Sound which is parented to a BasePart or Attachment will begin to attenuate (decrease in volume).
Controls how the volume of a Sound which is parented to a BasePart or Attachment attenuates (fades out) as the distance between the listener and parent changes.
The SoundGroup that is linked to this Sound.
Content ID of the sound file to associate with the Sound.
The length of the Sound in seconds.
Progress of the Sound in seconds. Can be changed to move the playback position of the Sound both before and during playback.
The volume of the Sound.
Métodos
Propiedades
ChannelCount
IsLoaded
This property is true when the Sound has loaded from Roblox servers and is ready to play. You can use this property and the Loaded event to verify a sound has loaded before playing it.
Muestras de código
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("The sound has loaded!")
IsPaused
This read-only property returns true when the Sound is not playing. Note that it can return true if a sound has been paused using Pause(), if it has been stopped using Stop(), or the sound has never been played.
As IsPaused is read-only, it cannot be used to stop the sound; Stop() or Pause() should be used instead.
Muestras de código
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
This read-only property returns true when the Sound is playing.
As IsPlaying is read-only, it cannot be used to play the sound; Play() should be used instead.
Muestras de código
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
A range denoting a desired loop start and loop end within the PlaybackRegion, in seconds.
If LoopRegion.Min > PlaybackRegion.Min, the loop starts from LoopRegion.Min.
If LoopRegion.Min < PlaybackRegion.Min, the loop starts from PlaybackRegion.Min.
If LoopRegion.Max > PlaybackRegion.Max, the loop starts at PlaybackRegion.Max.
If LoopRegion.Max < PlaybackRegion.Max, the loop starts at exactly that time.
If LoopRegion.Min == LoopRegion.Max, the Sound uses the PlaybackRegion property instead.
Looped
This sets whether or not the Sound repeats once it has finished playing. Looped sounds are suitable for a range of applications including music and background ambient sounds.
The DidLoop event can be used to track the number of times as sound has looped.
Muestras de código
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
When true, the Sound will play when it is removed from the experience by parenting the Sound or one if its ancestors to nil. This means all of the following will cause the sound to play when PlayOnRemove is true:
- sound:Destroy()
- sound.Parent = nil
- sound.Parent.Parent = nil
PlaybackLoudness
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.
Muestras de código
-- 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
A range denoting a desired start and stop time within the 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, this property is inactive.
PlaybackRegionsEnabled
If true, this property gives your Sound access to the PlaybackRegion and LoopRegion properties which can more-accurately control its playback.
PlaybackSpeed
Determines the speed at which a Sound will play, with higher values causing the sound to play faster and at a higher pitch.
Muestras de código
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
Indicates whether the Sound is currently playing. This can be toggled, and this property will always replicate.
In Studio's Properties window, while in Edit mode, toggling Playing to true does not begin playing the sound, but the sound will begin playing during runtime.
This property should not be confused with IsPlaying which is a read-only property.
Note that when Playing is set to false, the TimePosition property of the sound will not reset, meaning that when Playing is set to true again, the audio will continue from the time position it was at when it was stopped. However, if the Play() function is used to resume the sound, the time position will reset to 0.
Muestras de código
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
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 BasePart or Attachment.
How RollOffMaxDistance impacts the attenuation of a sound (manner in which it fades out) is dependent on the RollOffMode property.
RollOffMinDistance
The minimum distance, in studs, at which a Sound which is parented to a BasePart or Attachment will begin to attenuate (decrease in volume).
How RollOffMinDistance impacts the attenuation of a sound (manner in which it fades out) is dependent on the RollOffMode property.
RollOffMode
This property controls how the volume of a Sound which is parented to a BasePart or Attachment attenuates (fades out) as the distance between the listener and parent changes.
For details on the different modes, see Enum.RollOffMode.
SoundId
This property is the content ID of the sound file to associate with the Sound. See Audio Assets for more information.
TimeLength
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 PlaybackSpeed to adjust the speed of a sound so that it lasts for a specific duration.
Muestras de código
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
This property reflects the progress of the Sound in seconds. It can be changed to move the playback position of the sound both before and during playback.
As a Sound is played, TimePosition increases at a rate of PlaybackSpeed per second. Once TimePosition reaches TimeLength, the sound will stop unless it is Looped.
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, consider the following code snippet:
local newPosition = 1.5if newPosition >= sound.TimeLength thennewPosition = newPosition - sound.TimeLengthendsound.TimePosition = newPosition
Muestras de código
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
The volume of the Sound. Can be set between 0 and 10 and defaults to 0.5.
Note that if the Sound is a member of a SoundGroup, its playback volume (but not its Volume property) will be influenced by the group's SoundGroup.Volume property.
Métodos
Pause
This method pauses playback of the Sound if it is playing, setting Playing to false. Unlike Stop(), it does not reset TimePosition, meaning the sound can be resumed using Resume().
Devuelve
Muestras de código
-- 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
This method plays the Sound and sets TimePosition to the last value set by a script (or 0 if it has not been set), then sets Playing to true.
Devuelve
Muestras de código
-- 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
This method resumes the Sound and sets Playing to true. Does not alter TimePosition and thus can be used to resume playback of a sound paused through Pause().
Devuelve
Muestras de código
-- 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
This method stops the Sound and sets Playing to false, then sets TimePosition to 0.
Devuelve
Muestras de código
-- 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
Eventos
DidLoop
Fires whenever the Sound loops. Returns soundId and numOfTimesLooped, giving the content ID of the sound and the number of times looped respectively.
When the Sound is stopped through Stop(), the looped counter resets meaning the next DidLoop event will return 1 for numOfTimesLooped.
Parámetros
Muestras de código
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. This event is often used to destroy a sound when it has completed playback:
sound:Play()sound.Ended:Wait()sound:Destroy()
Note that this event will not fire for sounds with Looped set to true, as they continue playing upon reaching their end. This event will also not fire when the sound is stopped before playback has completed; for this use the Stopped event.
Parámetros
Loaded
Fires when the Sound is loaded.
As this event only fires at the time the sound is loaded, it's recommended to check the sound's IsLoaded property prior to connecting to this event.
Parámetros
Muestras de código
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 Pause().
Parámetros
Muestras de código
-- 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 Play(). This event will not fire if the Sound is played due to PlayOnRemove being set to true and the sound being destroyed.
Parámetros
Muestras de código
-- 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 Resume().
Parámetros
Muestras de código
-- 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 through using Stop(). Destroying a sound while it is playing will not cause this event to fire.
Parámetros
Muestras de código
-- 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