Sound

Afficher les obsolètes

*Ce contenu est traduit en utilisant l'IA (Beta) et peut contenir des erreurs. Pour consulter cette page en anglais, clique ici.

Sound est un objet qui émet du son.Lorsqu'il est placé dans un BasePart ou un Attachment, cet objet émettra son son de la partie BasePart.Position ou de l'point d'attacheAttachment.WorldPosition .Dans ce placement, un Sound expose l'effet Doppler, ce qui signifie que sa fréquence et son ajustement varient en fonction de la vitesse relative de toute pièce ou partie à laquelle il est attaché.En outre, son volume sera déterminé par la distance entre l'écouteur sonore du client (par défaut la position Camera ) et la position du parent du son.Pour plus d'informations, voir RollOffMode .

Un son est considéré comme « global » s'il n'est pas parenté à un ou à un ».Dans ce cas, le son sera diffusé au même volume dans tout l'emplacement.

Échantillons de code

The code in this sample demonstrates how a sound parented to a Part or Attachment will play locally and experience volume drop off the further the player's camera is away from the part.

A part is instanced, and a sound is instanced and parented to the part. A click detector is set up on the part that will check if the sound is playing, using Sound.IsPlaying and play or stop the sound depending.

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)

Résumé

Propriétés

  • Lecture uniquement
    Non répliqué
    Non navigable
    Sécurité des scripts Roblox
    Lecture parallèle
  • Lecture uniquement
    Non répliqué
    Lecture parallèle

    Cette propriété est true lorsque la Sound a été chargée des serveurs Roblox et est prête à jouer.

  • Caché
    Lecture uniquement
    Non répliqué
    Lecture parallèle

    Propriété de lecture seule qui renvoie true lorsque le Sound n'est pas en train de jouer.

  • Caché
    Lecture uniquement
    Non répliqué
    Lecture parallèle

    Propriété de lecture seule qui renvoie true lorsque le Sound est en train de jouer.

  • Lecture parallèle

    Une plage désignant le début et la fin d'une boucle souhaitée dans le PlaybackRegion, en secondes.

  • Lecture parallèle

    Définit si oui ou non la répétition Sound se produit une fois qu'elle a terminé de jouer.

  • Lecture parallèle

    Lorsque true , le Sound jouera lorsqu'il sera retiré de l'expérience.

  • Lecture uniquement
    Non répliqué
    Lecture parallèle

    Un nombre entre 0 et 1000 indiquant à quel point le Sound est actuellement en train de jouer retour.

  • Lecture parallèle

    Une plage désignant un temps de début et de fin souhaité dans la TimeLength, en secondes.

  • Si true , cette propriété donne à votre Sound accès aux propriétés PlaybackRegion et LoopRegion qui peuvent contrôler plus précisément sa lecture.

  • Non répliqué
    Lecture parallèle

    Détermine la vitesse à laquelle un Sound jouera, les valeurs plus élevées provoquant le son de jouer plus rapidement et à un ton plus élevé.

  • Non répliqué
    Lecture parallèle

    Indique si le Sound est en train de jouer actuellement.

  • Lecture parallèle

    La distance maximale, en studs, l'écouteur d'un client peut être de l'origine du son et l'entendre encore.S'applique uniquement à Sounds parents à un BasePart ou Attachment .

  • Lecture parallèle

    La distance minimale, en studs, à laquelle un Sound qui est parenté à un BasePart ou Attachment commencera à s'atténuer (diminution du volume).

  • Lecture parallèle

    Contrôle comment le volume d'un Sound qui est parenté à un BasePart ou Attachment atténue (disparaît) à mesure que la distance entre l'écouteur et le parent change.

  • Lecture parallèle

    Le SoundGroup qui est lié à ce Sound.

  • SoundId:ContentId
    Lecture parallèle

    ID du contenu du fichier son à associer au Sound.

  • Lecture uniquement
    Non répliqué
    Lecture parallèle

    La longueur du Sound en secondes.

  • Non répliqué
    Lecture parallèle

    Progression du Sound en secondes. Peut être modifié pour déplacer la position de lecture du Sound à la fois avant et pendant la lecture.

  • Lecture parallèle

    Le volume du Sound.

Méthodes

Évènements

Propriétés

ChannelCount

Lecture uniquement
Non répliqué
Non navigable
Sécurité des scripts Roblox
Lecture parallèle

IsLoaded

Lecture uniquement
Non répliqué
Lecture parallèle

Cette propriété est true lorsque la Sound a été chargée des serveurs Roblox et est prête à jouer.Vous pouvez utiliser cette propriété et l'événement Loaded pour vérifier qu'un son a été chargé avant de le jouer.

Échantillons de code

This simple function will verify a Sound has loaded by checking the Sound.IsLoaded property. If Sound.IsLoaded is false it will wait for the Loaded event before resuming.

It is important to check Sound.IsLoaded before connecting to the Sound.Loaded event, as if the sound has already loaded the Sound.Loaded event will not fire and the function will yield indefinitely.

Load Sound

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

IsPaused

Caché
Lecture uniquement
Non répliqué
Lecture parallèle

Cette propriété de lecture seule renvoie true lorsque le Sound n'est pas en train de jouer.Notez qu'il peut retourner true si un son a été interrompu en utilisant Pause() , s'il a été arrêté en utilisant Stop() , ou que le son n'a jamais été joué.

Comme IsPaused est en lecture seule, il ne peut pas être utilisé pour arrêter le son ; Stop() ou Pause() doit être utilisé à la place.

Échantillons de code

This code sample contains demonstrates when the Sound.IsPlaying and Sound.IsPaused properties will be true or false.

A sound is instanced in the Workspace and the Sound.IsLoaded property is checked to ensure it has loaded, if it has not the Sound.Loaded event is used to yield the script until the sound has.

As the sound is played, paused and stopped the Sound.IsPlaying and Sound.IsPaused properties are printed to demonstrate how they respond to each of these functions. Note Sound.IsPaused will always be true if even if the sound has been stopped rather than paused.

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

Caché
Lecture uniquement
Non répliqué
Lecture parallèle

Cette propriété de lecture seule renvoie true lorsque le Sound est en train de jouer.

Comme IsPlaying est en lecture seule, il ne peut pas être utilisé pour jouer le son ; Play() doit être utilisé à la place.

Échantillons de code

This code sample contains demonstrates when the Sound.IsPlaying and Sound.IsPaused properties will be true or false.

A sound is instanced in the Workspace and the Sound.IsLoaded property is checked to ensure it has loaded, if it has not the Sound.Loaded event is used to yield the script until the sound has.

As the sound is played, paused and stopped the Sound.IsPlaying and Sound.IsPaused properties are printed to demonstrate how they respond to each of these functions. Note Sound.IsPaused will always be true if even if the sound has been stopped rather than paused.

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

Lecture parallèle

Une plage désignant le début et la fin d'une boucle souhaitée dans le PlaybackRegion, en secondes.

  • Si Class.Sound.LoopRegion|LoopRegion.Min``>``Class.Sound.PlaybackRegion|PlaybackRegion.Min , la boucle commence à partir de LoopRegion.Min .

  • Si Class.Sound.LoopRegion|LoopRegion.Min``<``Class.Sound.PlaybackRegion|PlaybackRegion.Min , la boucle commence à partir de PlaybackRegion.Min .

  • Si Class.Sound.LoopRegion|LoopRegion.Max``>``Class.Sound.PlaybackRegion|PlaybackRegion.Max , la boucle commence à PlaybackRegion.Max .

  • Si Class.Sound.LoopRegion|LoopRegion.Max``<``Class.Sound.PlaybackRegion|PlaybackRegion.Max , la boucle commence à exactement à ce moment-là.

  • Si Class.Sound.LoopRegion|LoopRegion.Min``==``Class.Sound.LoopRegion|LoopRegion.Max , le Sound utilise la propriété PlaybackRegion à la place.

Looped

Lecture parallèle

Cela détermine si oui ou non la répétition Sound se produit une fois qu'elle a terminé de jouer.Les sons en boucle sont adaptés à une gamme d'applications, y compris la musique et les sons ambiants de fond.

L'événement DidLoop peut être utilisé pour suivre le nombre de fois où le son a bouclé.

Échantillons de code

This code sample includes a function that will play a sound and allow it to loop for a given number of times before stopping it.

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

Lecture parallèle

Lorsque true , le Sound jouera lorsqu'il sera retiré de l'expérience en parentant le Sound ou un si ses ancêtres sont à nil .Cela signifie que tout ce qui suit provoquera le son de jouer lorsque PlayOnRemove est true :

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

PlaybackLoudness

Lecture uniquement
Non répliqué
Lecture parallèle

Un nombre entre 0 et 1000 indiquant à quel point le Sound est actuellement en train de jouer retour.Cette propriété reflète l'amplitude de la lecture du son dans l'instance du temps auquel il est lu.

Échantillons de code

In this sample Sound.PlaybackLoudness is used to create an amplitude bar that shows the amplitude of a sound playing.

This code sample should be placed in StarterPlayerScripts.

A simple GUI is created, a frame holding that bar and a frame containing the bar. A Sound is then played and the size of the bar is set to reflect the Sound.PlaybackLoudness on a loop.

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

Lecture parallèle

Une plage désignant un temps de début et de fin souhaité dans la TimeLength, en secondes.

PlaybackRegionsEnabled

Lecture parallèle

Si true , cette propriété donne à votre Sound accès aux propriétés PlaybackRegion et LoopRegion qui peuvent contrôler plus précisément sa lecture.

PlaybackSpeed

Non répliqué
Lecture parallèle

Détermine la vitesse à laquelle un Sound jouera, les valeurs plus élevées provoquant le son de jouer plus rapidement et à un ton plus élevé.

Échantillons de code

In this example a Sound is played in the Workspace. The PlaybackSpeed property is increased and decreased at intervals to demonstrate its impact on the playback of the Sound.

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

Non répliqué
Lecture parallèle

Indique si le Sound est en train de jouer actuellement. Cela peut être basculé, et cette propriété se répliquera toujours.

Dans la fenêtre Propriétés de Studio, alors que vous êtes en mode Édition , le basculement de Playing``true ne commence pas à jouer le son, mais le son commencera à jouer pendant l'temps d'exécution.

Cette propriété ne doit pas être confondue avec IsPlaying qui est une propriété de lecture seule.

Notez que lorsque Playing est défini sur false, la propriété TimePosition du son ne sera pas réinitialiser, ce qui signifie que lorsque Playing est défini à nouveau sur true, l'audio se poursuivra à partir de la position du temps à laquelle il a été interrompu lorsqu'il a été arrêté.Cependant, si la fonction Play() est utilisée pour reprendre le son, la position du temps sera réinitialisée à 0 .

Échantillons de code

This sample demonstrates how the Sound.Playing property can be used to start and stop playback of a sound.

A sound is instanced in the Workspace and playback is started by setting Sound.Playing to true. After ten seconds the playback is stopped by setting Sound.Playing to false. When the playback is again resumed using Sound.Playing it resumes at the previous Sound.TimePosition it was stopped at. This is demonstrated by printing the TimePosition property immediately after resuming the sound.

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

Lecture parallèle

La distance maximale, en studs, l'écouteur d'un client peut être de l'origine du son et l'entendre encore.S'applique uniquement à Sounds parents à un BasePart ou Attachment .

La manière dont RollOffMaxDistance l'impact de l'atténuation d'un son (la manière dont il disparaît) dépend de la propriété RollOffMode.

RollOffMinDistance

Lecture parallèle

La distance minimale, en studs, à laquelle un Sound qui est parenté à un BasePart ou Attachment commencera à s'atténuer (diminution du volume).

La manière dont RollOffMinDistance l'impact de l'atténuation d'un son (la manière dont il disparaît) dépend de la propriété RollOffMode.

RollOffMode

Lecture parallèle

Cette propriété contrôle la façon dont le volume d'un Sound qui est parenté à un BasePart ou Attachment atténue (disparaît) à mesure que la distance entre l'écouteur et le parent change.

Pour des détails sur les différents modes, voir Enum.RollOffMode .

SoundGroup

Lecture parallèle

Le SoundGroup qui est lié à ce Sound.

SoundId

ContentId
Lecture parallèle

Cette propriété est l'ID du contenu du fichier son à associer au Sound. Voir ressources audio pour plus d'informations.

TimeLength

Lecture uniquement
Non répliqué
Lecture parallèle

La longueur du Sound en secondes. Si le Sound n'est pas chargé, cette valeur sera 0 .

Cette propriété est souvent utilisée en conjonction avec PlaybackSpeed pour ajuster la vitesse d'un son afin qu'il dure pendant une durée spécifique.

Échantillons de code

This code sample includes a simple function that uses Sound.TimeLength and Sound.PlaybackSpeed to play a sound that'll take the given duration to complete. It achieves this by setting the PlaybackSpeed of the sound to be equal to the TimeLength of the sound divided by the desired duration.

Note that as TimeLength is equal to 0 when the sound has not loaded, the function will yield while it loads the sound.

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

Non répliqué
Lecture parallèle

Cette propriété reflète la progression du Sound en secondes.Il peut être modifié pour déplacer la position de lecture du son avant et pendant la lecture.

Lorsqu'un Sound est joué, TimePosition augmente à un rythme de PlaybackSpeed par seconde.Une fois TimePosition atteint TimeLength, le son s'arrêtera à moins qu'il ne soit Looped.

Remarquez que le paramètre TimePosition à une valeur supérieure à la longueur dans une boucle ne provoquera pas son enroulement.Si ce comportement est souhaité, envisagez le snippet de code suivant :


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

Échantillons de code

This sample demonstrates how Sound.TimePosition can be used to jump to particular points in an audio file both before and during Sound playback.

A Sound is created in the Workspace and set to start at 30 seconds in. During playback, it jumps forwards to 100 seconds and then back to the start (0 seconds).

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

Lecture parallèle

Le volume du Sound . Peut être défini entre 0 et 10 et se définit par défaut sur 0.5 .

Notez que si le Sound est un membre d'un SoundGroup , son volume de lecture (mais pas sa propriété Volume ) sera influencé par la propriété du groupe SoundGroup.Volume.

Méthodes

Pause

()

Cette méthode met en pause la lecture du Sound si elle joue, en définissant Playing à false .Contrairement à Stop() , il ne réinitialise pas TimePosition , ce qui signifie que le son peut être repris en utilisant Resume() .


Retours

()

Échantillons de code

This sample gives a simple demonstration of what each of the Sound functions (Sound.Play, Sound.Stop, Sound.Pause and Sound.Resume) do to Sound.Playing and Sound.TimePosition.

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

()

Cette méthode joue le Sound et définit TimePosition sur la dernière valeur définie par un script (ou 0 si elle n'a pas été configurer), puis définit Playing sur true .


Retours

()

Échantillons de code

This sample gives a simple demonstration of what each of the Sound functions (Sound.Play, Sound.Stop, Sound.Pause and Sound.Resume) do to Sound.Playing and Sound.TimePosition.

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

()

Cette méthode reprend le Sound et définit Playing à true .Ne modifie pas TimePosition et peut donc être utilisé pour reprendre la lecture d'un son interrompu par Pause() .


Retours

()

Échantillons de code

This sample gives a simple demonstration of what each of the Sound functions (Sound.Play, Sound.Stop, Sound.Pause and Sound.Resume) do to Sound.Playing and Sound.TimePosition.

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

()

Cette méthode arrête le Sound et définit Playing à false puis définit TimePosition à 0 .


Retours

()

Échantillons de code

This sample gives a simple demonstration of what each of the Sound functions (Sound.Play, Sound.Stop, Sound.Pause and Sound.Resume) do to Sound.Playing and Sound.TimePosition.

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

Évènements

DidLoop

Se déclenche chaque fois que les boucles Sound s'exécutent.Retourne soundId et numOfTimesLooped , donnant l'ID du contenu du son et le nombre de fois en boucle respectivement.

Lorsque le Sound est arrêté via Stop() , le compteur en boucle se réinitialise, ce qui signifie que l'événement suivant DidLoop retournera 1 pour numOfTimesLooped.

Paramètres

soundId: string

Le de la boucle qui s'est répétée.

numOfTimesLooped: number

Le nombre de fois que le Sound a bouclé.


Échantillons de code

This code sample includes a function that will play a sound and allow it to loop for a given number of times before stopping it.

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

Se déclenche lorsque le Sound a terminé la lecture et s'est arrêté. Cet événement est souvent utilisé pour détruire un son lorsqu'il a terminé la lecture :


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

Notez que cet événement ne ne tirera pas pour les sons avec défini à , car ils continueront à jouer lorsqu'ils atteindront leur terminer.Cet événement ne se déclenchera pas non plus lorsque le son est arrêté avant que la lecture n'ait été terminée ; pour cela, utilisez l'événement .

Paramètres

soundId: string

Le SoundId du Sound qui s'est terminé.


Loaded

S'enflamme lorsque le Sound est chargé.

Comme cet événement ne se déclenche qu'au moment du chargement du son, il est recommandé de vérifier la propriété du son IsLoaded avant de se connecter à cet événement.

Paramètres

soundId: string

Le SoundId de la Sound qui a été chargé.


Échantillons de code

This simple function will verify a Sound has loaded by checking the Sound.IsLoaded property. If Sound.IsLoaded is false it will wait for the Loaded event before resuming.

It is important to check Sound.IsLoaded before connecting to the Sound.Loaded event, as if the sound has already loaded the Sound.Loaded event will not fire and the function will yield indefinitely.

Load Sound

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

Paused

Il se déclenche chaque fois que le Sound est interrompu en utilisant Pause() .

Paramètres

soundId: string

Le SoundId de la Sound qui a été interrompu.


Échantillons de code

This sample gives a simple demonstration of what each of the Sound functions (Sound.Play, Sound.Stop, Sound.Pause and Sound.Resume) do to Sound.Playing and Sound.TimePosition.

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

Se déclenche chaque fois que le Sound est joué en utilisant Play() .Cet événement ne se déclenchera pas si le est joué en raison du fait que est réglé sur et que le son est détruit.

Paramètres

soundId: string

Le SoundId de la Sound qui a été joué.


Échantillons de code

This sample gives a simple demonstration of what each of the Sound functions (Sound.Play, Sound.Stop, Sound.Pause and Sound.Resume) do to Sound.Playing and Sound.TimePosition.

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

S'enflamme lorsque le Sound est repris en utilisant Resume() .

Paramètres

soundId: string

Le SoundId de la Sound étant repris.


Échantillons de code

This sample gives a simple demonstration of what each of the Sound functions (Sound.Play, Sound.Stop, Sound.Pause and Sound.Resume) do to Sound.Playing and Sound.TimePosition.

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

S'enflamme lorsque le Sound est arrêté en utilisant Stop() . Détruire un son pendant qu'il est en train de jouer ne provoquera pas le lancerde cet événement.

Paramètres

soundId: string

Le SoundId de la Sound qui s'est arrêté.


Échantillons de code

This sample gives a simple demonstration of what each of the Sound functions (Sound.Play, Sound.Stop, Sound.Pause and Sound.Resume) do to Sound.Playing and Sound.TimePosition.

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