요약
속성
SoundId:ContentId |
이벤트
DidLoop(soundId: string,numOfTimesLooped: number):RBXScriptSignal |
Ended(soundId: string):RBXScriptSignal |
Loaded(soundId: string):RBXScriptSignal |
Paused(soundId: string):RBXScriptSignal |
Played(soundId: string):RBXScriptSignal |
Resumed(soundId: string):RBXScriptSignal |
Stopped(soundId: string):RBXScriptSignal |
상속된 멤버
코드 샘플
음악 재생 파트
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 참조
속성
EmitterSize
IsLoaded
코드 샘플
사운드 로드
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("사운드가 로드되었습니다!")IsPaused
코드 샘플
사운드가 재생 중이고 사운드가 일시 중지됨
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, trueIsPlaying
코드 샘플
사운드가 재생 중이고 사운드가 일시 중지됨
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, trueisPlaying
Looped
코드 샘플
주어진 횟수만큼 반복하기
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)MaxDistance
MinDistance
Pitch
PlaybackLoudness
코드 샘플
볼륨 진폭 표시기
-- 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)
endPlaybackSpeed
코드 샘플
사운드 재생 속도
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
코드 샘플
사운드 재생
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초SoundId
Sound.SoundId:ContentId
TimeLength
코드 샘플
특정 시간 동안 사운드 재생하기
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
코드 샘플
사운드 시간 위치
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메서드
Pause
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 0pause
Play
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 0play
Resume
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 0Stop
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 0stop
이벤트
DidLoop
코드 샘플
주어진 횟수만큼 반복하기
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)Loaded
매개 변수
코드 샘플
사운드 로드
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("사운드가 로드되었습니다!")Paused
매개 변수
코드 샘플
사운드 함수
-- 소리 생성
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 0Played
매개 변수
코드 샘플
사운드 함수
-- 소리 생성
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 0Resumed
매개 변수
코드 샘플
사운드 함수
-- 소리 생성
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 0Stopped
매개 변수
코드 샘플
사운드 함수
-- 소리 생성
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