Sound

Pokaż przestarzałe

*Ta zawartość została przetłumaczona przy użyciu narzędzi AI (w wersji beta) i może zawierać błędy. Aby wyświetlić tę stronę w języku angielskim, kliknij tutaj.

Sound jest obiektem, który emituje dźwięk.Gdy umieszczony zostanie w BasePart lub Attachment, ten obiekt wyemituje swój dźwięk z części BasePart.Position lub załącznika Attachment.WorldPosition.W tej lokalizacji Sound pokazuje efekt Dopplera, co oznacza, że jego częstotliwość i wysokość zmienia się wraz z ruchom relatywnym jakiegokolwiek załącznika lub części, do których jest przymocowany.Ponadto jego objętość zostanie określona przez odległość między odbiornikiem dźwięku klienta (domyślnie pozycją Camera ) a pozycją rodzica dźwięku.Aby uzyskać więcej informacji, zobacz RollOffMode.

Dźwięk jest uważany za "globalny", jeśli jest nie powiązany z BasePart lub Attachment.W tym przypadku dźwięk zagra na tym samym poziomie głośności przez całe miejsce.

Przykłady kodu

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)

Podsumowanie

Właściwości

Metody

Zdarzenia

Właściwości

ChannelCount

Tylko do odczytu
Bez replikacji
Brak możliwości przeglądania
Zabezpieczenia skryptów Roblox
Odczyt równoległy

IsLoaded

Tylko do odczytu
Bez replikacji
Odczyt równoległy

Właściwość ta jest true gdy Sound załadowano z serwerów Roblox i jest gotowa do grać.Możesz użyć tej właściwości i wydarzenia Loaded, aby zweryfikować, czy dźwięk został wczytany przed jego odtworzeniem.

Przykłady kodu

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

Ukryte
Tylko do odczytu
Bez replikacji
Odczyt równoległy

Ta właściwość tylko odczytowa zwraca true, gdy Sound nie gra.Zauważ, że może on zwrócić true, jeśli dźwięk został wstrzymany za pomocą Pause() , jeśli został zatrzymany za pomocą Stop() lub dźwięk nigdy nie został odtwarzany.

Ponieważ jest odczytywany tylko w trybie odczytu, nie może być używany do zatrzymania dźwięku; lub powinien zostać zamiast tego użyty.

Przykłady kodu

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

Ukryte
Tylko do odczytu
Bez replikacji
Odczyt równoległy

Ta właściwość tylko odczytowa zwraca prawdę, gdy Sound gra.

Ponieważ IsPlaying jest tylko do odczytu, nie może być używany do odtwarzania dźwięku; zamiast tego należy użyć Play().

Przykłady kodu

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

Odczyt równoległy

Zakres oznaczający początek i koniec pożądanego cyklu w PlaybackRegion , w sekundach.

  • Jeśli Class.Sound.LoopRegion|LoopRegion.Min``>``Class.Sound.PlaybackRegion|PlaybackRegion.Min , pętla zaczyna się od LoopRegion.Min .

  • Jeśli Class.Sound.LoopRegion|LoopRegion.Min``<``Class.Sound.PlaybackRegion|PlaybackRegion.Min , pętla zaczyna się od PlaybackRegion.Min .

  • Jeśli Class.Sound.LoopRegion|LoopRegion.Max``>``Class.Sound.PlaybackRegion|PlaybackRegion.Max , pętla zaczyna się od PlaybackRegion.Max .

  • Jeśli Class.Sound.LoopRegion|LoopRegion.Max``<``Class.Sound.PlaybackRegion|PlaybackRegion.Max , pętla zaczyna się o dokładnie w tym czasie.

  • Jeśli , używa właściwości zamiast tego.

Looped

Odczyt równoległy

To ustala, czy Sound powtórzy się po zakończeniu odtwarzania.Dźwięki pętlowe są odpowiednie do wielu zastosowań, w tym muzyki i tła dźwięków ambient.

Wydarzenie DidLoop można wykorzystać do śledzenia liczby razy, kiedy dźwięk się powtarzał.

Przykłady kodu

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

Odczyt równoległy

Gdy true , Sound zagra, gdy zostanie usunięty z doświadczenia przez rodzicowanie Sound lub jeden, jeśli jego przodkowie do nil .Oznacza to, że wszystko co następuje spowoduje, że dźwięk zostanie odtwarzany, gdy PlayOnRemove jest true :

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

PlaybackLoudness

Tylko do odczytu
Bez replikacji
Odczyt równoległy

Liczba pomiędzy 0 a 1000 wskazująca, jak głośno Sound obecnie wstecz.Właściwość ta odzwierciedla amplitudę odtwarzania dźwięku w instancji czasu, w którym jest przeczytane.

Przykłady kodu

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

Odczyt równoległy

Zakres oznaczający pożądaną początkową i końcową godzinę w TimeLength, w sekundach.

PlaybackRegionsEnabled

Odczyt równoległy

Jeśli true, ta właściwość daje dostęp Sound do właściwości PlaybackRegion i LoopRegion, które mogą bardziej dokładnie kontrolować jego odtwarzanie.

PlaybackSpeed

Bez replikacji
Odczyt równoległy

Określa prędkość, z jaką Sound grać, z wyższymi wartościami powodującymi, że dźwięk będzie odtwarzany szybciej i na wyższej wysokości.

Przykłady kodu

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

Bez replikacji
Odczyt równoległy

Wskazuje, czy Sound jest obecnie odtwarzany. Można to przełączyć, a ta właściwość zawsze będzie się powielać.

W oknie Właściwości w Studio, podczas trybu Edytuj , przełączanie Playing``true nie zaczyna odtwarzać dźwięku, ale dźwięk zacznie odtwarzać się podczas uruchamiania.

Właściwość ta nie powinna być mylona z IsPlaying, która jest właściwością tylko do odczytu.

Zauważ, że gdy Playing jest ustawiony na false, właściwość TimePosition dźwięku nie zostanie resetować, co oznacza, że gdy Playing zostanie ponownie ustawiony na true, dźwięk będzie nadal odtwarzał się z pozycji czasowej, w której został zatrzymany.Jeśli jednak funkcja Play() jest używana do wznowienia dźwięku, pozycja czasowa zostanie zresetowana do 0.

Przykłady kodu

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

Odczyt równoległy

Maksymalna odległość, w szpilkach, słuchacz klienta może być od pochodzenia dźwięku i nadal go usłyszeć.Dotyczy tylko Sounds rodzica związanego z BasePart lub Attachment.

Jak RollOffMaxDistance wpływa na osłabienie dźwięku (sposób, w jaki znika) zależy od właściwości RollOffMode.

RollOffMinDistance

Odczyt równoległy

Minimalna odległość, w szpilkach, w której Sound który jest powiązany z BasePart lub Attachment zacznie się zmniejszać (spadek objętości).

Jak RollOffMinDistance wpływa na osłabienie dźwięku (sposób, w jaki znika) zależy od właściwości RollOffMode.

RollOffMode

Odczyt równoległy

Właściwość ta kontroluje, w jaki sposób głośność Sound, która jest powiązana z BasePart lub Attachment zmniejsza się (nanbuje) wraz z odległością między odbiorcą a rodzicem.

Dla szczegółów na temat różnych trybów, patrz Enum.RollOffMode .

SoundGroup

Odczyt równoległy

The SoundGroup , które jest powiązane z tym Sound .

SoundId

ContentId
Odczyt równoległy

Ta właściwość jest ID zawartości pliku dźwiękowego, który należy powiązać z Sound. Zobacz Zasoby audio dla więcej informacji.

TimeLength

Tylko do odczytu
Bez replikacji
Odczyt równoległy

Długość Sound w sekundach. Jeśli Sound nie zostanie załadowane, wartość ta będzie 0 .

Właściwość ta jest często używana w połączeniu z PlaybackSpeed w celu dostosowania prędkości dźwięku, aby trwał przez określony czas.

Przykłady kodu

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

Bez replikacji
Odczyt równoległy

Właściwość ta odzwierciedla postęp Sound w sekundach.Można ją zmienić, aby przesunąć pozycję odtwarzania dźwięku zarówno przed, jak i podczas odtwarzania.

Gdy jest odtwarzany jako Sound, TimePosition zwiększa się w tempie PlaybackSpeed na sekundę.Gdy TimePosition dotrze do TimeLength, dźwięk się zatrzyma, chyba że jest Looped.

Zauważ, że ustawienie TimePosition do wartości większej niż długość w pętli nie spowoduje, że się zwinie.Jeśli chcesz tego zachowania, rozważ następujący kodowy snippet:


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

Przykłady kodu

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

Odczyt równoległy

Głosność Sound . Można ją ustawić między 0 a 10 i domyślnie wynosi 0.5 .

Zauważ, że jeśli Sound jest członkiem grupaSoundGroup, jej głośność odtwarzania (ale nie jej właściwość Volume) będzie wpływać na właściwość grupy SoundGroup.Volume.

Metody

Pause

()

Ta metoda wstrzymuje odtwarzanie Sound jeśli odtwarza, ustawiając Playing na false.W przeciwieństwie do Stop() nie resetuje TimePosition , co oznacza, że dźwięk można wznowić za pomocą Resume() .


Zwroty

()

Przykłady kodu

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

()

Ta metoda odtwarza Sound i ustawia TimePosition na ostatnią wartość ustawioną przez skrypt (lub 0 jeśli nie została ustawiać), a następnie ustawia Playing na true.


Zwroty

()

Przykłady kodu

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

()

Ta metoda wznowi Sound i ustawi Playing na true.Nie zmienia TimePosition i dlatego może być używany do wznowienia odtwarzania dźwięku wstrzymanego przez Pause() .


Zwroty

()

Przykłady kodu

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

()

Ta metoda zatrzymuje Sound i ustawia Playing na false , następnie ustawia TimePosition na 0 .


Zwroty

()

Przykłady kodu

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

Zdarzenia

DidLoop

Wystrzeliwuje za każdym razem, gdy pętle Sound się powtarzają.Wyświetla soundId i numOfTimesLooped, dając ID zawartości dźwięku i liczbę razy powtórzonych odpowiednio.

Gdy Sound zostanie zatrzymany za pomocą Stop() , pętla licznika zostanie zresetowana, co oznacza, że następne wydarzenie DidLoop zwróci 1 dla numOfTimesLooped.

Parametry

soundId: string

The SoundId of the Sound that looped.

numOfTimesLooped: number

Liczba razy, w których Sound powtórzyło się.


Przykłady kodu

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

Wypala się, gdy Sound zakończy odtwarzanie i się zatrzyma. To wydarzenie jest często używane do niszczenia dźwięku po zakończeniu odtwarzania:


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

Zauważ, że to wydarzenie nie nie będzie strzelać do dźwięków z ustawionym na , ponieważ nadal będą grać po dotarciu do kończyć.To wydarzenie również nie będzie się palić , gdy dźwięk zostanie zatrzymany przed rozpoczęciem odtwarzania; dla tego użyj wydarzenia Stopped.

Parametry

soundId: string

The SoundId z Sound który się skończył.


Loaded

Wypala się, gdy Sound jest wczytywany.

Ponieważ to wydarzenie wystrzeliwuje się tylko w momencie gdy dźwięk jest wczytywany, zaleca się sprawdzenie właściwości dźwięku IsLoaded przed połączeniem z tym wydarzeniem.

Parametry

soundId: string

The SoundId z Sound które zostały załadowane.


Przykłady kodu

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

Wystrzeliwuje za każdym razem, gdy Sound jest wstrzymywany za pomocą Pause() .

Parametry

soundId: string

The SoundId z Sound które zostało wstrzymane.


Przykłady kodu

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

Wystrzeliwuje za każdym razem, gdy Sound jest odtwarzany za pomocą Play().To wydarzenie nie nie wybuchnie, jeśli Sound zostanie odtworzone z powodu PlayOnRemove ustawienia na true i zniszczenia dźwięku.

Parametry

soundId: string

The SoundId z Sound który został odtwarzany.


Przykłady kodu

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

Wystrzeliwuje, gdy Sound zostanie wznowione za pomocą Resume() .

Parametry

soundId: string

The SoundId z Sound zostaje wznowiony.


Przykłady kodu

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

Wystrzeliwuje, gdy Sound zostanie zatrzymany za pomocą Stop() . Zniszczenie dźwięku, gdy jest odtwarzany, nie spowoduje wystrzeliwania tego wydarzenia.

Parametry

soundId: string

The SoundId z Sound który się zatrzymał.


Przykłady kodu

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