コードサンプル
音楽再生部分
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)
概要
プロパティ
イベント
プロパティ
ChannelCount
IsLoaded
コードサンプル
Load Sound
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("The sound has loaded!")
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) -- 偽、真
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
コードサンプル
サウンドが再生中とサウンドが一時停止中
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) -- 偽、真
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
Looped
コードサンプル
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
PlaybackLoudness
コードサンプル
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
PlaybackRegionsEnabled
PlaybackSpeed
コードサンプル
サウンド再生速度
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 -- default
Playing
コードサンプル
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
RollOffMinDistance
RollOffMode
SoundGroup
SoundId
ContentId
TimeLength
コードサンプル
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
コードサンプル
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
方法
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 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) -- 真の 0 を再生
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- 一時停止偽 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- 真の 10 を再生
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- 偽の 0 を停止
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
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 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) -- 真の 0 を再生
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- 一時停止偽 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- 真の 10 を再生
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- 偽の 0 を停止
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
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 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) -- 真の 0 を再生
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- 一時停止偽 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- 真の 10 を再生
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- 偽の 0 を停止
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
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 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) -- 真の 0 を再生
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- 一時停止偽 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- 真の 10 を再生
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- 偽の 0 を停止
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
イベント
DidLoop
パラメータ
コードサンプル
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)
Loaded
パラメータ
コードサンプル
Load Sound
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("The sound has loaded!")
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 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) -- 真の 0 を再生
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- 一時停止偽 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- 真の 10 を再生
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- 偽の 0 を停止
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
Played
パラメータ
コードサンプル
サウンド機能
-- サウンドを作成
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 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) -- 真の 0 を再生
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- 一時停止偽 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- 真の 10 を再生
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- 偽の 0 を停止
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
Resumed
パラメータ
コードサンプル
サウンド機能
-- サウンドを作成
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 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) -- 真の 0 を再生
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- 一時停止偽 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- 真の 10 を再生
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- 偽の 0 を停止
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
Stopped
パラメータ
コードサンプル
サウンド機能
-- サウンドを作成
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 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) -- 真の 0 を再生
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- 一時停止偽 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- 真の 10 を再生
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- 偽の 0 を停止
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0