Học
Lớp công cụ
Sound

*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.


Mẫu mã
Phần Nhạc Đang Phát
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
-- tạo một âm thanh
local sound = Instance.new("Sound", part)
sound.SoundId = "rbxassetid://9120386436"
sound.EmitterSize = 5 -- giảm kích thước phát ra (để giảm âm lượng sớm hơn)
sound.Looped = true
sound.Parent = part
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = part
-- chuyển đổi âm thanh đang phát / không phát
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)

Tài Liệu Tham Khảo API
Thuộc Tính
AcousticSimulationEnabled
Đọc Song Song
Các khả năng: LegacySound
Sound.AcousticSimulationEnabled:boolean

AudioContent
Ẩn
Đọc Song Song
Các khả năng: LegacySound
Sound.AudioContent:Content

EmitterSize
Đã Lỗi Thời

IsLoaded
Chỉ Đọc
Không Sao Chép
Đọc Song Song
Các khả năng: LegacySound
Sound.IsLoaded:boolean
Mẫu mã
Tải Âm Thanh
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("Âm thanh đã được tải!")

IsPaused
Ẩn
Chỉ Đọc
Không Sao Chép
Đọc Song Song
Các khả năng: LegacySound
Sound.IsPaused:boolean
Mẫu mã
Âm thanh ĐangChạy và Âm thanh ĐangTạmDừng
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) -- đúng, sai
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- sai, đúng
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- đúng, sai
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- sai, đúng

IsPlaying
Ẩn
Chỉ Đọc
Không Sao Chép
Đọc Song Song
Các khả năng: LegacySound
Sound.IsPlaying:boolean
Mẫu mã
Âm thanh ĐangChạy và Âm thanh ĐangTạmDừng
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) -- đúng, sai
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- sai, đúng
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- đúng, sai
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- sai, đúng

isPlaying
Đã Lỗi Thời

Looped
Đọc Song Song
Các khả năng: LegacySound
Sound.Looped:boolean
Mẫu mã
Lặp lại một số lần
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
-- ngắt kết nối
connection:Disconnect()
-- dừng âm thanh
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)

LoopRegion
Đọc Song Song
Các khả năng: LegacySound
Sound.LoopRegion:NumberRange

MaxDistance
Đã Lỗi Thời

MinDistance
Đã Lỗi Thời

Pitch
Đã Lỗi Thời

PlaybackLoudness
Chỉ Đọc
Không Sao Chép
Đọc Song Song
Các khả năng: LegacySound
Sound.PlaybackLoudness:number
Mẫu mã
Thanh Biên Độ Âm Lượng
-- nên được đặt trong StarterPlayer > StarterPlayerScripts
local Players = game:GetService("Players")
-- chờ đợi người chơi địa phương PlayerGui
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")
-- tạo một ScreenGui
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = playerGui
-- tạo một khung chứa cho thanh của chúng ta
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
-- tạo một thanh
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
-- tạo một âm thanh
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = screenGui
sound:Play()
-- xác định một độ lớn tối đa
local maxLoudness = 30
-- hoạt hình thanh biên độ
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
Đọc Song Song
Các khả năng: LegacySound
Sound.PlaybackRegion:NumberRange

PlaybackRegionsEnabled
Đọc Song Song
Các khả năng: LegacySound
Sound.PlaybackRegionsEnabled:boolean

PlaybackSpeed
Không Sao Chép
Đọc Song Song
Các khả năng: LegacySound
Sound.PlaybackSpeed:number
Mẫu mã
Tốc độ phát âm thanh
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
sound:Play()
task.wait(10)
sound.PlaybackSpeed = 3 -- nhanh hơn 3 lần
task.wait(5)
sound.PlaybackSpeed = 0.5 -- chậm hơn 2 lần
task.wait(5)
sound.PlaybackSpeed = 1 -- mặc định

Playing
Không Sao Chép
Đọc Song Song
Các khả năng: LegacySound
Sound.Playing:boolean
Mẫu mã
Phát Âm
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
print("đang phát âm thanh")
sound.Playing = true
task.wait(10)
print("dừng âm thanh")
sound.Playing = false
task.wait(5)
sound.Playing = true
local timePosition = sound.TimePosition
print("tiếp tục tại vị trí thời gian: " .. tostring(timePosition)) -- c. 10 giây

PlayOnRemove
Đọc Song Song
Các khả năng: LegacySound
Sound.PlayOnRemove:boolean

RollOffMaxDistance
Đọc Song Song
Các khả năng: LegacySound
Sound.RollOffMaxDistance:number

RollOffMinDistance
Đọc Song Song
Các khả năng: LegacySound
Sound.RollOffMinDistance:number

RollOffMode
Đọc Song Song
Các khả năng: LegacySound
Sound.RollOffMode:Enum.RollOffMode

SoundGroup
Đọc Song Song
Các khả năng: LegacySound
Sound.SoundGroup:SoundGroup

SoundId
Đọc Song Song
Các khả năng: LegacySound
Sound.SoundId:ContentId

TimeLength
Chỉ Đọc
Không Sao Chép
Đọc Song Song
Các khả năng: LegacySound
Sound.TimeLength:number
Mẫu mã
Phát âm thanh trong một khoảng thời gian cụ thể
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
Không Sao Chép
Đọc Song Song
Các khả năng: LegacySound
Sound.TimePosition:number
Mẫu mã
Thời gian âm thanh
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
Vị Trí Thời Gian
local Workspace = game:GetService("Workspace")
local sound = Instance.new("Sound")
sound.Parent = Workspace
local newPosition = 1.5
if newPosition >= sound.TimeLength then
newPosition = newPosition - sound.TimeLength
end
sound.TimePosition = newPosition

Volume
Đọc Song Song
Các khả năng: LegacySound
Sound.Volume:number

Phương Pháp
Pause
Các khả năng: LegacySound
Sound:Pause():()
Lợi Nhuận
()
Mẫu mã
Chức Năng Âm Thanh
-- tạo một âm thanh
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- lắng nghe các sự kiện
sound.Played:Connect(function(_soundId)
print("Sự kiện Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Sự kiện Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Sự kiện Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Sự kiện Sound.Stopped")
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

pause
Đã Lỗi Thời

Play
Các khả năng: LegacySound
Sound:Play():()
Lợi Nhuận
()
Mẫu mã
Chức Năng Âm Thanh
-- tạo một âm thanh
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- lắng nghe các sự kiện
sound.Played:Connect(function(_soundId)
print("Sự kiện Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Sự kiện Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Sự kiện Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Sự kiện Sound.Stopped")
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
Đã Lỗi Thời

Resume
Các khả năng: LegacySound
Sound:Resume():()
Lợi Nhuận
()
Mẫu mã
Chức Năng Âm Thanh
-- tạo một âm thanh
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- lắng nghe các sự kiện
sound.Played:Connect(function(_soundId)
print("Sự kiện Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Sự kiện Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Sự kiện Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Sự kiện Sound.Stopped")
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
Các khả năng: LegacySound
Sound:Stop():()
Lợi Nhuận
()
Mẫu mã
Chức Năng Âm Thanh
-- tạo một âm thanh
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- lắng nghe các sự kiện
sound.Played:Connect(function(_soundId)
print("Sự kiện Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Sự kiện Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Sự kiện Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Sự kiện Sound.Stopped")
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
Đã Lỗi Thời

Sự Kiện
DidLoop
Các khả năng: LegacySound
Sound.DidLoop(
soundId:string, numOfTimesLooped:number
Tham Số
soundId:string
numOfTimesLooped:number
Mẫu mã
Lặp lại một số lần
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
-- ngắt kết nối
connection:Disconnect()
-- dừng âm thanh
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)

Ended
Các khả năng: LegacySound
Sound.Ended(soundId:string):RBXScriptSignal
Tham Số
soundId:string

Loaded
Các khả năng: LegacySound
Sound.Loaded(soundId:string):RBXScriptSignal
Tham Số
soundId:string
Mẫu mã
Tải Âm Thanh
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("Âm thanh đã được tải!")

Paused
Các khả năng: LegacySound
Sound.Paused(soundId:string):RBXScriptSignal
Tham Số
soundId:string
Mẫu mã
Chức Năng Âm Thanh
-- tạo một âm thanh
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- lắng nghe các sự kiện
sound.Played:Connect(function(_soundId)
print("Sự kiện Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Sự kiện Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Sự kiện Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Sự kiện Sound.Stopped")
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
Các khả năng: LegacySound
Sound.Played(soundId:string):RBXScriptSignal
Tham Số
soundId:string
Mẫu mã
Chức Năng Âm Thanh
-- tạo một âm thanh
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- lắng nghe các sự kiện
sound.Played:Connect(function(_soundId)
print("Sự kiện Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Sự kiện Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Sự kiện Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Sự kiện Sound.Stopped")
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
Các khả năng: LegacySound
Sound.Resumed(soundId:string):RBXScriptSignal
Tham Số
soundId:string
Mẫu mã
Chức Năng Âm Thanh
-- tạo một âm thanh
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- lắng nghe các sự kiện
sound.Played:Connect(function(_soundId)
print("Sự kiện Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Sự kiện Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Sự kiện Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Sự kiện Sound.Stopped")
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
Các khả năng: LegacySound
Sound.Stopped(soundId:string):RBXScriptSignal
Tham Số
soundId:string
Mẫu mã
Chức Năng Âm Thanh
-- tạo một âm thanh
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- lắng nghe các sự kiện
sound.Played:Connect(function(_soundId)
print("Sự kiện Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Sự kiện Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Sự kiện Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Sự kiện Sound.Stopped")
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

©2026 Roblox Corporation. Roblox, logo Roblox và Powering Imagination là các nhãn hiệu đã đăng ký và chưa đăng ký của chúng tôi tại Hoa Kỳ và các quốc gia khác.