เรียนรู้
Engine Class
Sound

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่


สรุป
วิธีการ
Pause():()
pause():()
เลิกใช้แล้ว
Play():()
play():()
เลิกใช้แล้ว
Resume():()
Stop():()
stop():()
เลิกใช้แล้ว
สมาชิกที่ได้รับทอด
ตัวอย่างโค้ด
ส่วนเพลงเล่น
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
คุณสมบัติ
AcousticSimulationEnabled
อ่านพร้อมๆ กัน
ความสามารถ: LegacySound
Sound.AcousticSimulationEnabled:boolean

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

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("เหตุการณ์ Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("เหตุการณ์ Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("เหตุการณ์ Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("เหตุการณ์ Sound.Stopped")
end)
sound:Play()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น true 0
task.wait(10)
sound:Pause()
print("หยุดชั่วคราว", sound.Playing, sound.TimePosition) -- หยุดชั่วคราว false 10
task.wait(3)
sound:Resume()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น true 10
task.wait(3)
sound:Stop()
print("หยุด", sound.Playing, sound.TimePosition) -- หยุด false 0
task.wait(3)
sound:Play()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น 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("เหตุการณ์ Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("เหตุการณ์ Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("เหตุการณ์ Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("เหตุการณ์ Sound.Stopped")
end)
sound:Play()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น true 0
task.wait(10)
sound:Pause()
print("หยุดชั่วคราว", sound.Playing, sound.TimePosition) -- หยุดชั่วคราว false 10
task.wait(3)
sound:Resume()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น true 10
task.wait(3)
sound:Stop()
print("หยุด", sound.Playing, sound.TimePosition) -- หยุด false 0
task.wait(3)
sound:Play()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น 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("เหตุการณ์ Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("เหตุการณ์ Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("เหตุการณ์ Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("เหตุการณ์ Sound.Stopped")
end)
sound:Play()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น true 0
task.wait(10)
sound:Pause()
print("หยุดชั่วคราว", sound.Playing, sound.TimePosition) -- หยุดชั่วคราว false 10
task.wait(3)
sound:Resume()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น true 10
task.wait(3)
sound:Stop()
print("หยุด", sound.Playing, sound.TimePosition) -- หยุด false 0
task.wait(3)
sound:Play()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น 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("เหตุการณ์ Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("เหตุการณ์ Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("เหตุการณ์ Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("เหตุการณ์ Sound.Stopped")
end)
sound:Play()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น true 0
task.wait(10)
sound:Pause()
print("หยุดชั่วคราว", sound.Playing, sound.TimePosition) -- หยุดชั่วคราว false 10
task.wait(3)
sound:Resume()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น true 10
task.wait(3)
sound:Stop()
print("หยุด", sound.Playing, sound.TimePosition) -- หยุด false 0
task.wait(3)
sound:Play()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น 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("เหตุการณ์ Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("เหตุการณ์ Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("เหตุการณ์ Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("เหตุการณ์ Sound.Stopped")
end)
sound:Play()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น true 0
task.wait(10)
sound:Pause()
print("หยุดชั่วคราว", sound.Playing, sound.TimePosition) -- หยุดชั่วคราว false 10
task.wait(3)
sound:Resume()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น true 10
task.wait(3)
sound:Stop()
print("หยุด", sound.Playing, sound.TimePosition) -- หยุด false 0
task.wait(3)
sound:Play()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น 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("เหตุการณ์ Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("เหตุการณ์ Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("เหตุการณ์ Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("เหตุการณ์ Sound.Stopped")
end)
sound:Play()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น true 0
task.wait(10)
sound:Pause()
print("หยุดชั่วคราว", sound.Playing, sound.TimePosition) -- หยุดชั่วคราว false 10
task.wait(3)
sound:Resume()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น true 10
task.wait(3)
sound:Stop()
print("หยุด", sound.Playing, sound.TimePosition) -- หยุด false 0
task.wait(3)
sound:Play()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น 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("เหตุการณ์ Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("เหตุการณ์ Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("เหตุการณ์ Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("เหตุการณ์ Sound.Stopped")
end)
sound:Play()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น true 0
task.wait(10)
sound:Pause()
print("หยุดชั่วคราว", sound.Playing, sound.TimePosition) -- หยุดชั่วคราว false 10
task.wait(3)
sound:Resume()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น true 10
task.wait(3)
sound:Stop()
print("หยุด", sound.Playing, sound.TimePosition) -- หยุด false 0
task.wait(3)
sound:Play()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น 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("เหตุการณ์ Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("เหตุการณ์ Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("เหตุการณ์ Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("เหตุการณ์ Sound.Stopped")
end)
sound:Play()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น true 0
task.wait(10)
sound:Pause()
print("หยุดชั่วคราว", sound.Playing, sound.TimePosition) -- หยุดชั่วคราว false 10
task.wait(3)
sound:Resume()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น true 10
task.wait(3)
sound:Stop()
print("หยุด", sound.Playing, sound.TimePosition) -- หยุด false 0
task.wait(3)
sound:Play()
print("เล่น", sound.Playing, sound.TimePosition) -- เล่น true 0

©2026 Roblox Corporation. Roblox, โลโก้ Roblox และ Powering Imagination เป็นส่วนหนึ่งของเครื่องหมายการค้าที่จดทะเบียน และไม่ได้จดทะเบียนของเราในสหรัฐฯ และประเทศอื่นๆ