學習
引擎類別
Sound

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡


範例程式碼
音樂播放部件
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
-- 創建一個聲音
local sound = Instance.new("Sound", part)
sound.SoundId = "rbxassetid://9120386436"
sound.EmitterSize = 5 -- 減小發射器大小(以便更早音量降低)
sound.Looped = true
sound.Parent = part
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = part
-- 切換聲音播放 / 不播放
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)

API 參考
屬性
AudioContent
隱藏
平行讀取
功能: LegacySound
Sound.AudioContent:Content

EmitterSize
已棄用

IsLoaded
唯讀
未複製
平行讀取
功能: LegacySound
Sound.IsLoaded:boolean
範例程式碼
載入音效
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("音效已載入!")

IsPaused
隱藏
唯讀
未複製
平行讀取
功能: LegacySound
Sound.IsPaused:boolean
範例程式碼
聲音正在播放和聲音已暫停
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
隱藏
唯讀
未複製
平行讀取
功能: LegacySound
Sound.IsPlaying:boolean
範例程式碼
聲音正在播放和聲音已暫停
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
已棄用

Looped
平行讀取
功能: LegacySound
Sound.Looped:boolean
範例程式碼
循環指定次數
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
-- 斷開連接
connection:Disconnect()
-- 停止聲音
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)

LoopRegion
平行讀取
功能: LegacySound
Sound.LoopRegion:NumberRange

MaxDistance
已棄用

MinDistance
已棄用

Pitch
已棄用

PlaybackLoudness
唯讀
未複製
平行讀取
功能: LegacySound
Sound.PlaybackLoudness:number
範例程式碼
音量振幅條
-- 放置在 StarterPlayer > StarterPlayerScripts 中
local Players = game:GetService("Players")
-- 等待本地玩家 PlayerGui
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")
-- 創建一個 ScreenGui
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = playerGui
-- 創建一個持有我們條的框架
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
-- 創建一個條
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
-- 創建一個聲音
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = screenGui
sound:Play()
-- 定義最大音量
local maxLoudness = 30
-- 動畫振幅條
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
平行讀取
功能: LegacySound
Sound.PlaybackRegion:NumberRange

PlaybackRegionsEnabled
平行讀取
功能: LegacySound
Sound.PlaybackRegionsEnabled:boolean

PlaybackSpeed
未複製
平行讀取
功能: LegacySound
Sound.PlaybackSpeed:number
範例程式碼
聲音播放速度
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
sound:Play()
task.wait(10)
sound.PlaybackSpeed = 3 -- 3倍快
task.wait(5)
sound.PlaybackSpeed = 0.5 -- 2倍慢
task.wait(5)
sound.PlaybackSpeed = 1 -- 默認

Playing
未複製
平行讀取
功能: LegacySound
Sound.Playing:boolean
範例程式碼
聲音播放
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
print("播放聲音")
sound.Playing = true
task.wait(10)
print("停止聲音")
sound.Playing = false
task.wait(5)
sound.Playing = true
local timePosition = sound.TimePosition
print("在時間位置恢復: " .. tostring(timePosition)) -- 約 10 秒

PlayOnRemove
平行讀取
功能: LegacySound
Sound.PlayOnRemove:boolean

RollOffMaxDistance
平行讀取
功能: LegacySound
Sound.RollOffMaxDistance:number

RollOffMinDistance
平行讀取
功能: LegacySound
Sound.RollOffMinDistance:number

RollOffMode
平行讀取
功能: LegacySound
Sound.RollOffMode:Enum.RollOffMode

SoundGroup
平行讀取
功能: LegacySound
Sound.SoundGroup:SoundGroup

SoundId
平行讀取
功能: LegacySound
Sound.SoundId:ContentId

TimeLength
唯讀
未複製
平行讀取
功能: LegacySound
Sound.TimeLength:number
範例程式碼
為特定持續時間播放聲音
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
未複製
平行讀取
功能: LegacySound
Sound.TimePosition:number
範例程式碼
聲音時間位置
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
時間位置
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
平行讀取
功能: LegacySound
Sound.Volume:number

方法
Pause
功能: LegacySound
Sound:Pause():()
返回
()
範例程式碼
聲音函數
-- 創建一個聲音
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- 監聽事件
sound.Played:Connect(function(_soundId)
print("聲音播放事件")
end)
sound.Paused:Connect(function(_soundId)
print("聲音暫停事件")
end)
sound.Resumed:Connect(function(_soundId)
print("聲音恢復事件")
end)
sound.Stopped:Connect(function(_soundId)
print("聲音停止事件")
end)
sound:Play()
print("播放", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("暫停", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("播放", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("停止", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("播放", sound.Playing, sound.TimePosition) -- play true 0

pause
已棄用

Play
功能: LegacySound
Sound:Play():()
返回
()
範例程式碼
聲音函數
-- 創建一個聲音
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- 監聽事件
sound.Played:Connect(function(_soundId)
print("聲音播放事件")
end)
sound.Paused:Connect(function(_soundId)
print("聲音暫停事件")
end)
sound.Resumed:Connect(function(_soundId)
print("聲音恢復事件")
end)
sound.Stopped:Connect(function(_soundId)
print("聲音停止事件")
end)
sound:Play()
print("播放", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("暫停", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("播放", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("停止", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("播放", sound.Playing, sound.TimePosition) -- play true 0

play
已棄用

Resume
功能: LegacySound
Sound:Resume():()
返回
()
範例程式碼
聲音函數
-- 創建一個聲音
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- 監聽事件
sound.Played:Connect(function(_soundId)
print("聲音播放事件")
end)
sound.Paused:Connect(function(_soundId)
print("聲音暫停事件")
end)
sound.Resumed:Connect(function(_soundId)
print("聲音恢復事件")
end)
sound.Stopped:Connect(function(_soundId)
print("聲音停止事件")
end)
sound:Play()
print("播放", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("暫停", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("播放", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("停止", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("播放", sound.Playing, sound.TimePosition) -- play true 0

Stop
功能: LegacySound
Sound:Stop():()
返回
()
範例程式碼
聲音函數
-- 創建一個聲音
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- 監聽事件
sound.Played:Connect(function(_soundId)
print("聲音播放事件")
end)
sound.Paused:Connect(function(_soundId)
print("聲音暫停事件")
end)
sound.Resumed:Connect(function(_soundId)
print("聲音恢復事件")
end)
sound.Stopped:Connect(function(_soundId)
print("聲音停止事件")
end)
sound:Play()
print("播放", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("暫停", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("播放", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("停止", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("播放", sound.Playing, sound.TimePosition) -- play true 0

stop
已棄用

活動
DidLoop
功能: LegacySound
Sound.DidLoop(
soundId:string, numOfTimesLooped:number
參數
soundId:string
numOfTimesLooped:number
範例程式碼
循環指定次數
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
-- 斷開連接
connection:Disconnect()
-- 停止聲音
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)

Ended
功能: LegacySound
Sound.Ended(soundId:string):RBXScriptSignal
參數
soundId:string

Loaded
功能: LegacySound
Sound.Loaded(soundId:string):RBXScriptSignal
參數
soundId:string
範例程式碼
載入音效
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("音效已載入!")

Paused
功能: LegacySound
Sound.Paused(soundId:string):RBXScriptSignal
參數
soundId:string
範例程式碼
聲音函數
-- 創建一個聲音
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- 監聽事件
sound.Played:Connect(function(_soundId)
print("聲音播放事件")
end)
sound.Paused:Connect(function(_soundId)
print("聲音暫停事件")
end)
sound.Resumed:Connect(function(_soundId)
print("聲音恢復事件")
end)
sound.Stopped:Connect(function(_soundId)
print("聲音停止事件")
end)
sound:Play()
print("播放", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("暫停", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("播放", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("停止", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("播放", sound.Playing, sound.TimePosition) -- play true 0

Played
功能: LegacySound
Sound.Played(soundId:string):RBXScriptSignal
參數
soundId:string
範例程式碼
聲音函數
-- 創建一個聲音
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- 監聽事件
sound.Played:Connect(function(_soundId)
print("聲音播放事件")
end)
sound.Paused:Connect(function(_soundId)
print("聲音暫停事件")
end)
sound.Resumed:Connect(function(_soundId)
print("聲音恢復事件")
end)
sound.Stopped:Connect(function(_soundId)
print("聲音停止事件")
end)
sound:Play()
print("播放", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("暫停", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("播放", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("停止", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("播放", sound.Playing, sound.TimePosition) -- play true 0

Resumed
功能: LegacySound
Sound.Resumed(soundId:string):RBXScriptSignal
參數
soundId:string
範例程式碼
聲音函數
-- 創建一個聲音
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- 監聽事件
sound.Played:Connect(function(_soundId)
print("聲音播放事件")
end)
sound.Paused:Connect(function(_soundId)
print("聲音暫停事件")
end)
sound.Resumed:Connect(function(_soundId)
print("聲音恢復事件")
end)
sound.Stopped:Connect(function(_soundId)
print("聲音停止事件")
end)
sound:Play()
print("播放", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("暫停", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("播放", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("停止", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("播放", sound.Playing, sound.TimePosition) -- play true 0

Stopped
功能: LegacySound
Sound.Stopped(soundId:string):RBXScriptSignal
參數
soundId:string
範例程式碼
聲音函數
-- 創建一個聲音
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- 監聽事件
sound.Played:Connect(function(_soundId)
print("聲音播放事件")
end)
sound.Paused:Connect(function(_soundId)
print("聲音暫停事件")
end)
sound.Resumed:Connect(function(_soundId)
print("聲音恢復事件")
end)
sound.Stopped:Connect(function(_soundId)
print("聲音停止事件")
end)
sound:Play()
print("播放", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("暫停", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("播放", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("停止", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("播放", sound.Playing, sound.TimePosition) -- play true 0

©2026 Roblox Corporation、Roblox、Roblox 標誌及 Powering Imagination 是我們在美國及其他國家地區的部分註冊與未註冊商標。