Sound

Tampilkan yang Tidak Digunakan Lagi

*Konten ini diterjemahkan menggunakan AI (Beta) dan mungkin mengandung kesalahan. Untuk melihat halaman ini dalam bahasa Inggris, klik di sini.

A Sound adalah objek yang mengeluarkan suara.

Suara 2D dan 3D

Sebuah suara ditempatkan di dalam BasePart atau Attachment akan menghasilkan suaranya d

Sebuah suara dianggap "global" jika tidak diberi label ke suatu BasePart atau Attachment . Dalam hal ini, suara akan dimainkan dengan volume yang sama di seluruh tempat.

Contoh Kode

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)

This sample gives a simple example of how a Sound that is not parented to a Part or Attachment will play at a constant volume throughout the place. A sound is instanced and parented to the Workspace, and then played.

Sound in the Workspace

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

Rangkuman

Properti

  • Hanya Baca
    Tidak Direplikasi
    Tidak Dapat Dijelajahi
    Keamanan Skrip Roblox
    Baca Paralel
  • Hanya Baca
    Tidak Direplikasi
    Baca Paralel

    Elemen ini akan benar ketika Sound telah dimuat dari server Roblox dan siap untuk dimainkan.

  • Tersembunyi
    Hanya Baca
    Tidak Direplikasi
    Baca Paralel

    Properti baca-hanya ini akan mengembalikan true ketika Sound tidak bermain.

  • Tersembunyi
    Hanya Baca
    Tidak Direplikasi
    Baca Paralel

    Properti baca hanya ini akan mengembalikan benar saat Sound bermain.

  • Sebuah rentang yang menunjukkan awal dan akhir dari lompatan dalam Sound.PlaybackRegion , dalam detik.

  • Baca Paralel

    Ini menentukan apakah atau tidak Sound berulang setelah selesai ketika bermain.

  • Baca Paralel

    Ketika benar, Sound akan dimainkan ketika dihapus dari game, dengan menyempurnakan Sound atau satu jika leluhurnya nol.

  • Hanya Baca
    Tidak Direplikasi
    Baca Paralel

    Sebuah angka antara 0 dan 1000 menunjukkan seberapa keras Sound bermain kembali.

  • Jangkauan yang menunjukkan waktu mulai (min) dan berhenti (max) yang diinginkan dalam Sound.TimeLength , dalam detik.

  • Jika benar , property ini memberi Anda akses ke Sound dan Sound.PlaybackRegion properti, yang dapat lebih akurat mengontrol playback-nya.

  • Tidak Direplikasi
    Baca Paralel

    Mengontrol kecepatan di mana Sound bermain, dengan nilai yang lebih tinggi menyebabkan suara bermain lebih cepat dan pada ketinggian yang lebih tinggi.

  • Tidak Direplikasi
    Baca Paralel

    Menunjukkan apakah Sound saat ini bermain.

  • Jarak maksimum, dalam studs, pendengar klien dapat berasal dari Sound\s asal dan masih mendengarnya. Hanya berlaku untuk Sounds yang diwarisi dari Part atau Attachment (suara 3D).

  • Jarak minimum, dalam studs, di mana 3D Sound (anak langsung dari BasePart atau Attachment ) akan mulai menurun (menurun dalam volume).

  • Mengontrol bagaimana volume 3D Sound (berada di bawah Class.BasePart) berperilaku sebagai jarak antara orang yang mendengarkan dan orang yang mendengarkan orang yang mendengarkan.

  • Class.SoundGroup yang terkait dengan Sound ini. Volume dan Efek Suara yang diterapkan pada kelompok suara ini akan diterapkan ke suara. Sebuah suara hanya dapat berada dalam satu kelompok suara pada satu waktu.

  • SoundId:ContentId
    Baca Paralel

    Properti ini adalah ID konten dari file suara yang sebuah Sound objek terkait. Setelah suara diunggah ke Roblox, ID konten dapat ditemukan dalam URL suara yang diunggah.

  • Hanya Baca
    Tidak Direplikasi
    Baca Paralel

    Panjang Sound dalam detik. Jika Sound tidak dimuat, nilai ini akan menjadi 0.

  • Tidak Direplikasi
    Baca Paralel

    Menunjukkan kemajuan dalam detik dari Sound . Dapat diubah untuk menyesuaikan posisi playback dari Sound kedua sebelum dan selama playback.

  • Baca Paralel

    Volume dari Sound dapat diatur antara 0 dan 10. Standar untuk 0,5.

Metode

Acara

Properti

ChannelCount

Hanya Baca
Tidak Direplikasi
Tidak Dapat Dijelajahi
Keamanan Skrip Roblox
Baca Paralel

IsLoaded

Hanya Baca
Tidak Direplikasi
Baca Paralel

Elemen ini akan benar ketika Sound telah dimuat dari server Roblox dan siap untuk dimainkan.

Di Roblox, file audio tidak disimpan di game itu sendiri tetapi dihosting di server Roblox dan dianggap oleh Sound.SoundId property. Ini berarti bahwa mereka perlu diunduh ke perangkat klien sebelum mereka dapat dimainkan. Ini dapat memakan waktu beberapa saat tergantung pada koneksi internet pengguna, panjang suara, dan jumlah objek lain

Pengembang dapat menggunakan property Sound.IsLoaded dan acara Sound.Loaded jika mereka ingin memverifikasi bahwa suara telah dimuat sebelum dimainkan.

Contoh Kode

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

Tersembunyi
Hanya Baca
Tidak Direplikasi
Baca Paralel

Properti baca-hanya ini akan mengembalikan true ketika Sound tidak bermain. Catat bahwa properti ini tidak hanya akan mengembalikan true setelah suara dihentikan menggunakan fungsi Sound:Pause() tetapi juga jika tidak pernah dimainkan menggunakan fungsi Sound:Stop().

Propiedad ini hanya akan benar ketika Sound.IsPlaying adalah benar.

Karena As IsPaused hanya dibaca, itu tidak dapat digunakan untuk menghentikan suara, Sound:Stop() dan Sound:Pause() seharusnya digunakan sebagai gantinya.

Contoh Kode

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

Tersembunyi
Hanya Baca
Tidak Direplikasi
Baca Paralel

Properti baca hanya ini akan mengembalikan benar saat Sound bermain.

Propiedad ini hanya dapat benar ketika Sound.IsPaused adalah benar.

Karena IsPlaying hanya dibaca, itu tidak dapat digunakan untuk memutar suara, Sound:Play() seharusnya digunakan sebagai gantinya.

Contoh Kode

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

Baca Paralel

Sebuah rentang yang menunjukkan awal dan akhir dari lompatan dalam Sound.PlaybackRegion Sound dalam detik.

Looped

Baca Paralel

Ini menentukan apakah atau tidak Sound berulang setelah selesai ketika bermain.

Suara berulang cocok untuk berbagai aplikasi, termasuk musik dan suara ambient latar belakang. Acara Sound.DidLoop dapat digunakan untuk melacak jumlah kali suara berulang.

Contoh Kode

Whilst looping is most appropriate for longer tracks such as music or ambient sounds, this sample demonstrates sound looping by creating the Roblox death sound in every character's head and looping it.

The Players.PlayerAdded and Player.CharacterAdded functions are used to detect new player characters. One a new character has been added a sound is created in their head. For demonstration purposes, the Sound.DidLoop function is used to show how the number of times a particular sound has looped can be tracked.

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)

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

Baca Paralel

Ketika benar, Sound akan dimainkan saat dihapus dari game.

Catat bahwa suara akan dimainkan ketika propietas Instance.Parent dari Sound atau salah satu dari leluhurnya ditetapkan ke nol. Ini berarti semua dari berikut akan menyebabkan suara dimainkan ketika PlayOnRemove benar. Catat, ini termasuk Instance:Destroy() sebagai fungsi hancur menetapkan orang

suara:Hancurkan suara.Orang tua = suara nol.Orang tua = nol

Contoh Kode

In this sample a sound is created in the workspace, PlayOnRemove is set to true and the sound is then destroyed.

As Sound.PlayOnRemove is true, the sound will play when it is removed.

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

Hanya Baca
Tidak Direplikasi
Baca Paralel

Sebuah angka antara 0 dan 1000 menunjukkan seberapa keras Sound bermain kembali.

Prop ini mencerminkan amplitud pemutarannya suara dalam kasus waktu yang dilihat. Oleh karena itu, untuk kebanyakan suara itu akan bervariasi secara konstan. Karena itu, dalam kebanyakan kasus suara dibaca oleh kode di jendela Roblox Studio sebagai 0, namun ketika dibaca oleh kode di bilah perintah atau Scripts itu akan mengembalikan nil

Contoh Kode

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

Baca Paralel

Jangkauan yang menunjukkan waktu mulai (min) dan berhenti (max) yang diinginkan dalam Sound.TimeLength , dalam detik.

  • Jika PlaybackRegion.Min > 0, suara mulai bermain dari waktu PlaybackRegion.Min.

  • Jika PlaybackRegion.Min < 0, suara mulai bermain dari 0.

  • Jika PlaybackRegion.Max > Sound.TimeLength , suara berhenti di Sound.TimeLength .

  • Jika PlaybackRegion.Max < Sound.TimeLength , suara berhenti pada waktu tepat itu.

  • Jika PlaybackRegion.Min == PlaybackRegion.Max, PlayBackRegion tidak aktif.

PlaybackRegionsEnabled

Baca Paralel

Jika benar , property ini memberi Anda akses ke Sound dan Sound.PlaybackRegion properti, yang dapat lebih akurat mengontrol playback-nya.

PlaybackSpeed

Tidak Direplikasi
Baca Paralel

Mengontrol kecepatan di mana Sound bermain. Semakin besar nilai, semakin cepat suara akan diputar kembali.

Misalnya, nilai 2 akan menyebabkan Sound bermain 2x lebih cepat, sementara nilai 0,5 akan menyebabkan bermain 2x lebih lambat. Saat PlaybackSpeed sama dengan 1, suara akan membutuhkan Sound.TimeLength (dalam detik) untuk menyelesaikan.

Catat bahwa meningkatkan PlaybackSpeed suara akan menyebabkan suara tersebut bermain dengan pitch yang lebih tinggi.

Contoh Kode

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

Tidak Direplikasi
Baca Paralel

Menunjukkan apakah Sound saat ini bermain. Ini dapat diaktifkan, dan属性 ini akan selalu bereplikasi.

Di editor studio, Sounds tidak dapat dimainkan karena waktu berhenti. Mengatur Sound.Playing ke benar dalam mode edit tidak melakukan apa-apa. Namun, Anda dapat mengubah audio dari dalam plugin dengan menetapkan Sounds sebagai nenek moyang dari <

Elemen ini seharusnya tidak bingung dengan Sound.IsPlaying yang merupakan elemen baca-hanya. Bermain dapat diatur ke benar atau salah untuk memulai atau menghentikan pemutaran suara.

Catat bahwa ketika Bermain di set ke false, property Sound.TimePosition dari suara tidak akan reset. Ini berarti ketika Bermain di set ke true lagi, audio akan melanjutkan dari posisi waktu yang diatur ketika dihentikan. Namun, jika fungsi Sound:Play() digunakan untuk mengambil lanjutkan suara, posisi waktu akan di

Contoh Kode

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

Baca Paralel

Jarak maksimum, dalam studs, pendengar klien dapat berasal dari Sound asal dan masih mendengarnya. Hanya berlaku untuk Sounds yang diwarisi ke Class.Part atau Class.Attachment (suara 3D).

Bagaimana RollOffMaxDistance mempengaruhi penyemburan suara (cara penyudutnya) adalah tergantung pada property Sound.RollOffMode. Saat RollOffMode di set untuk menggunakan model jarak terbalik (Inverse atau InverseTapered) maka RollOffMaxDistance tidak akan meng

Ketika RollOffMode di set ke model jarak tipe linier (Linear atau LinearSquared) suara akan berkurang antara Sound.EmitterSize dan MaxDistance (dengan volume pemutar mencapai nol di RollOffMaxDistance). Ini kurang realistis, tetapi dalam beberapa kasus memungkinkan penanganan penyemburan dengan lebih mudah.

RollOffMinDistance

Baca Paralel

Jarak minimum, dalam studs, di mana 3D Sound (anak langsung dari BasePart atau Attachment ) akan mulai menurun (menurun dalam volume).

Kedengarannya terkait dengan BasePart atau Attachment yang merupakan cendawan dari Workspace yang dianggap 3D suara dan volume bermain tergantung p

Cara Sound menyikat (memudar) setelah jarak antara pendengar dan suara melebihi batas RollOffMinDistance ditentukan oleh Mode RollOff.

RollOffMode

Baca Paralel

Properti ini menetapkan cara 3D Sounds menyatu (memudar) saat jarak antara pendengar dan orang tua suara meningkat. Ini dapat diatur ke salah satu nilai dari Enum.RollOffMode enum.

Kode berikut akan mengatur RollOffMode menjadi Linear:


sound.RollOffMode = Enum.RollOffMode.Linear

Mode yang berbeda

Opsi berikut tersedia:


<tbody>
<tr>
<td>Terbalik</td>
<td>Volume berkurang dari <code>Suara/RollOffMinDistance</code> dengan cara terbalik.</td>
</tr>
<tr>
<td>TerbalikTerkecil</td>
<td>Model hibrida. Mengikuti model Terbalik saat dekat dengan RollOffMinDistance dan model Linier Square saat dekat dengan Sound/RollOffMaxDistance.</td>
</tr>
<tr>
<td>Linier</td>
<td>Volume berkurang antara <code>RollOffMinDistance</code> dan <code>Sound/RollOffMaxDistance</code> dengan hubungan linier.</td>
</tr>
<tr>
<td>PutarLinear</td>
<td>Volume berkurang antara <code>RollOffMinDistance</code> dan <code>Sound/RollOffMaxDistance</code> dengan hubungan linier.</td>
</tr>
</tbody>
ModeDeskripsi

Inversi vs Jarak Linier

Oleh karena suara diatur untuk menggunakan penurunan jarak terbalik (enum.rolloffmode.逆) yang mengacungkan cara suara berkurang di dunia nyata. Di bawah penurunan jarak terbalik, suara akan mulai berkurang setelah jarak antara pendengar dan Sound's parent me

RollOffMaxDistance tidak akan mengalami penyempurnaan di bawah model terbalik tetapi akan menyebabkan suara terpotong sepenuhnya setelah jarak ini dicapai. Ini dapat sangat mencolok ketika menggunakan nilai rendah untuk jarak maksimal.

Jarak lurus menghilangkan bekerja secara berbeda. Di bawah jarak lurus menghilangkan, suara akan menghilang di antara RollOffMinDistance dan RollOffMaxDistance, menjadi diam setelah mencapai MaxDistance. RollOffMinDistance masih menunjukkan titik di mana suara akan mulai menghilang. Namun, volume yang

SoundGroup

Baca Paralel

Class.SoundGroup yang terhubung ke Sound ini. SoundGroup.Volume dan 1>Class.SoundGroup.SoundEffects1> yang diterapkan ke kelompok suara ini akan masuk ke suara. Sebuah suara hanya dapat berada di satu kelompok suara pada satu waktu.

Class.SoundGroup|SoundGroups digunakan untuk mengelola volume dan efek dari banyak Sounds sekaligus. Sebuah Sound ditambahkan ke sebuah 0> Class.SoundGroup0> dengan menetapkan properti SoundGroups3> dari suara.

Contoh Kode

This sample demonstrates how a SoundGroup can be used to change the volume of its associated Sounds and apply SoundEffects.

In this example a Sound is instanced in the Workspace and assigned to a new SoundGroup. The Sound is played and during playback the volume is changed via the SoundGroup and a SoundEffect is added.

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

ContentId
Baca Paralel

Properti ini adalah ID konten dari file suara yang sebuah Sound objek terkait. Setelah suara diunggah ke Roblox, ID konten dapat ditemukan dalam URL suara yang diunggah.

Penting untuk diingat bahwa URL tidak sama dengan ID konten. Ini akan bekerja ketika dipasang secara langsung ke property SoundId dari Sound di Roblox studio, karena Studio akan secara otomatis memperbaikinya, namun jika diatur dari Script maka ID konten yang benar akan perlu digunakan, menggunakan nomor dari URL. Mis


"https://www.roblox.com/catalog/9120386436" -- URL web (tidak akan berfungsi)
"http://www.roblox.com/asset/?id=9120386436" -- ID Konten (akan bekerja)
"rbxassetid://9120386436" -- Content ID (alternative version, will work)

Contoh Kode

This sample gives a simple example of how a Sound that is not parented to a Part or Attachment will play at a constant volume throughout the place. A sound is instanced and parented to the Workspace, and then played.

Sound in the Workspace

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

TimeLength

Hanya Baca
Tidak Direplikasi
Baca Paralel

Panjang Sound dalam detik. Jika Sound tidak dimuat, nilai ini akan menjadi 0.

Eigenschaft ini sering digunakan bersama dengan Sound.PlaybackSpeed untuk menyesuaikan kecepatan suara sehingga bertahan selama durasi tertentu (lihat contoh). Ketika Sound.PlaybackSpeed sama dengan 1, suara akan memakan waktu detik untuk menyelesaikan.

Contoh Kode

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

Tidak Direplikasi
Baca Paralel

Menunjukkan kemajuan dalam detik dari Sound . Dapat diubah untuk menyesuaikan posisi pemutarannya. Jika suara sedang dimainkan maka pemutar akan beralih ke posisi yang ditentukan. Jika tidak memainkan suara Sound akan memulai pemutaran di posisi yang ditentukan ketika suara berikutnya dimainkan.

Saat Sound dimainkan, TimePosition meningkat dengan kecepatan Sound.PlaybackSpeed per detik. Setelah TimePosition mencapai Sound.TimeLength , maka suara akan berhenti kecuali jika 1> Class.Sound

Catat bahwa menetapkan TimePosition ke nilai yang lebih besar dari panjang dalam jalur berulang tidak akan menyebabkan wrapping. Jika perilaku itu diinginkan, pengembang harus melakukan mengikuti.

jika newPosition >= sound.TimeLength maka newPosition = newPosition - sound.TimeLength end sound.TimePosition = newPosition

Mengatur TimePosition ke nilai kurang dari nol saat ini tidak mengubah waktu bermain, tetapi perilaku ini tidak boleh diandalkan.

Contoh Kode

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

Baca Paralel

Volume dari Sound dapat diatur antara 0 dan 10. Standar untuk 0,5

Catat bahwa jika Sound adalah anggota dari Class.SoundGroup nya volume pemutarannya (tetapi bukan Volume property) akan dipengaruhi oleh SoundGroup property dari Class.SoundGroup itu. Efek ini

Contoh Kode

This sample includes a demonstration of how a Sound's volume can be set using the Sound.Volume property.

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

Metode

Pause

void

Tetapkan Sound.Playing ke false. Ini menghentikan playback dari Sound jika suara sedang dimainkan. Berbeda dengan Sound:Stop() , itu tidak mengatur ulang 1> Class.Sound.TimePosition1> , yang berarti suara dapat diambil kembali

Efek dari berbagai fungsi Suara pada Sound.Playing dan Sound.TimePosition ditunjukkan di bawah ini.


<tbody>
<tr>
<td>Suara:Mainkan()</td>
<td>Benar</td>
<td>Nilai terakhir ditetapkan dalam Lua (default 0)</td>
</tr>
<tr>
<td>Suara:Berhenti()</td>
<td>Palsu</td>
<td>bahasa</td>
</tr>
<tr>
<td>Suara: Lanjutkan()</td>
<td>Benar</td>
<td>bahasa</td>
</tr>
<tr>
<td>Suara: Berhenti()</td>
<td>Palsu</td>
<td>0</td>
</tr>
</tbody>
FungsiSuara.BermainPosisi Waktu Suara

Memberikan nilai

void

Contoh Kode

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

void

Memainkan Sound . Tetapkan Sound.TimePosition ke nilai terakhir yang ditetapkan oleh Script (atau 0 jika belum atur), dan kemudian tetapkan 1> Class.Sound.Playing1> ke benar.

Pengaruh berbagai fungsi Sound pada Sound.Playing dan Sound.TimePosition di bawah ini ditunjukkan.


<tbody>
<tr>
<td>Suara:Mainkan()</td>
<td>Benar</td>
<td>Nilai terakhir ditetapkan dalam Lua (default 0)</td>
</tr>
<tr>
<td>Suara:Berhenti()</td>
<td>Palsu</td>
<td>bahasa</td>
</tr>
<tr>
<td>Suara: Lanjutkan()</td>
<td>Benar</td>
<td>bahasa</td>
</tr>
<tr>
<td>Suara: Berhenti()</td>
<td>Palsu</td>
<td>0</td>
</tr>
</tbody>
FungsiSuara.BermainPosisi Waktu Suara

Memberikan nilai

void

Contoh Kode

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

void

Mengambil kembali Sound . Tetapkan Sound.Playing ke benar. Tidak mengubah Sound.TimePosition dan dapat dengan demikian digunakan untuk mengambil kembali pemutaran suara yang dihentikan menggunakan 1> Class.Sound:Pause()1> .

Efek dari berbagai fungsi suara pada Sound.Playing dan Sound.TimePosition ditunjukkan di bawah ini.


<tbody>
<tr>
<td>Suara:Mainkan()</td>
<td>Benar</td>
<td>Nilai terakhir ditetapkan dalam Lua (default 0)</td>
</tr>
<tr>
<td>Suara:Berhenti()</td>
<td>Palsu</td>
<td>bahasa</td>
</tr>
<tr>
<td>Suara: Lanjutkan()</td>
<td>Benar</td>
<td>bahasa</td>
</tr>
<tr>
<td>Suara: Berhenti()</td>
<td>Palsu</td>
<td>0</td>
</tr>
</tbody>
FungsiSuara.BermainPosisi Waktu Suara

Memberikan nilai

void

Contoh Kode

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

void

Menghentikan Sound. Tetapkan Sound.Playing ke false lalu tetapkan Sound.TimePosition ke 0.

Efek dari berbagai fungsi suara pada Sound.Playing dan Sound.TimePosition ditunjukkan di bawah ini.


<tbody>
<tr>
<td>Suara:Mainkan()</td>
<td>Benar</td>
<td>Nilai terakhir ditetapkan dalam Lua (default 0)</td>
</tr>
<tr>
<td>Suara:Berhenti()</td>
<td>Palsu</td>
<td>bahasa</td>
</tr>
<tr>
<td>Suara: Lanjutkan()</td>
<td>Benar</td>
<td>bahasa</td>
</tr>
<tr>
<td>Suara: Berhenti()</td>
<td>Palsu</td>
<td>0</td>
</tr>
</tbody>
FungsiSuara.BermainPosisi Waktu Suara

Memberikan nilai

void

Contoh Kode

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

Acara

DidLoop

Acara yang menyetel setiap kali Sound loop. Mengembalikan soundId dan numOfTimesLooped, memberikan kontenID dari suara dan jumlah kali looped masing-masing.

Ketika Sound dihentikan, counter berulang kembali berarti acara DidLoop berikutnya akan mengembalikan 1 untuk numOfTimesLooped.

Parameter

soundId: string

Class.Sound.SoundId dari Sound yang berulang.

numOfTimesLooped: number

Jumlah kali Sound telah ber loop.


Contoh Kode

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

Diaktifkan saat Sound telah menyelesaikan pemutar dan berhenti. Perhatikan bahwa acara ini tidak akan diaktifkan untuk suara dengan Sound.Looped tetap set ke benar saat mereka terus bermain setelah mencapai akhirimereka.

Acara ini sering digunakan untuk menghancurkan suara saat telah menyelesaikan playback.


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

Acara ini hanya diaktifkan jika suara telah mencapai akhiri. Ini berarti bahwa ia juga tidak akan diaktifkan saat suara dihentikan sebelum pemutaran telah selesai, untuk penggunaan ini Sound.Stopped .

Parameter

soundId: string

Class.Sound.SoundId dari Sound yang telah berakhir.


Loaded

Acara Sound.Loaded diaktifkan saat Sound dimuat.

Catat bahwa acara ini hanya akan diaktifkan saat suara dimuat. Ini berarti jika diasuh untuk saat suara sudah dimuat itu tidak akan kembali. Oleh karena itu, disarankan untuk memeriksa Sound.IsLoaded sebelum terhubung ke acara ini.

Parameter

soundId: string

Class.Sound.SoundId dari Sound yang dimuat.


Contoh Kode

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

Dibakar setiap kali Sound dihentikan menggunakan Sound:Pause() .

Seperti dengan Sound.Played , Sound.Resumed dan Sound.Stopped hanya fungsi suara yang berwenang yang akan menyebabkan acara tersebut terjadi. Ini berarti bahwa Pause hanya akan menjalankan ketika 1> Class.Sound:Pause()1> dianggil.

Parameter

soundId: string

Class.Sound.SoundId dari Sound yang dihentikan.


Contoh Kode

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

Membakar setiap kali Sound dimainkan menggunakan fungsi Sound:Play() .

Seperti dengan Sound.Stopped , Sound.Paused dan Sound.Resumed hanya fungsi suara yang berwenang yang akan menyebabkan acara berapi-api. Ini berart

Parameter

soundId: string

Class.Sound.SoundId dari Sound yang diputar.


Contoh Kode

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

Dibakar saat Sound diambil kembali menggunakan Sound:Resume() .

Seperti dengan Sound.Played , Sound.Paused dan Sound.Stopped hanya fungsi suara yang berwenang yang akan menyebabkan acara berlangsung. Ini berarti bahwa Resume hanya akan menyetel ketika 1> Class.Sound:Resume()1> dipanggil.

Parameter

soundId: string

Class.Sound.SoundId dari Sound diambil kembali.


Contoh Kode

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

Dibakar saat Sound dihentikan karena fungsi Sound:Stop() .

Seperti dengan Sound.Played , Sound.Paused dan Sound.Resumed hanya fungsi suara yang berwenang yang akan menyebabkan acara berlangsung. Ini berarti bahwa Dibatalkan hanya akan berfungsi saat 1> Class.Sound:Stop()1> diangg

Parameter

soundId: string

Class.Sound.SoundId dari Sound yang berhenti.


Contoh Kode

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