Classe do mecanismo
Sound
*Este conteúdo é traduzido por IA (Beta) e pode conter erros. Para ver a página em inglês, clique aqui.
Resumo
Propriedades
SoundId:ContentId |
Eventos
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 |
Amostras de código
Parte de Música Reproduzindo
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
-- criar um som
local sound = Instance.new("Sound", part)
sound.SoundId = "rbxassetid://9120386436"
sound.EmitterSize = 5 -- diminuir o tamanho do emissor (para queda de volume mais cedo)
sound.Looped = true
sound.Parent = part
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = part
-- alternar entre som tocando / não tocando
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)Referência API
Propriedades
EmitterSize
IsLoaded
Amostras de código
Carregar Som
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("O som foi carregado!")IsPaused
Amostras de código
Som EstáTocando e SomEstáPausado
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) -- verdadeiro, falso
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- falso, verdadeiro
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- verdadeiro, falso
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- falso, verdadeiroIsPlaying
Amostras de código
Som EstáTocando e SomEstáPausado
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) -- verdadeiro, falso
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- falso, verdadeiro
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- verdadeiro, falso
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- falso, verdadeiroisPlaying
Looped
Amostras de código
Repetir um Número de Vezes
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
-- desconectar a conexão
connection:Disconnect()
-- parar o som
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)MaxDistance
MinDistance
Pitch
PlaybackLoudness
Amostras de código
Barra de Amplitude de Volume
-- a ser colocado em StarterPlayer > StarterPlayerScripts
local Players = game:GetService("Players")
-- aguarde o PlayerGui do jogador local
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")
-- crie um ScreenGui
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = playerGui
-- crie um suporte para nossa barra
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
-- crie uma barra
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
-- crie um som
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = screenGui
sound:Play()
-- defina um volume máximo
local maxLoudness = 30
-- anime a barra de amplitude
while true do
local amplitude = math.clamp(sound.PlaybackLoudness / maxLoudness, 0, 1)
bar.Size = UDim2.new(amplitude, 0, 1, 0)
wait(0.05)
endPlaybackSpeed
Amostras de código
Velocidade de Reproduçăo do Som
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
sound:Play()
task.wait(10)
sound.PlaybackSpeed = 3 -- 3x mais rápido
task.wait(5)
sound.PlaybackSpeed = 0.5 -- 2x mais lento
task.wait(5)
sound.PlaybackSpeed = 1 -- padrãoPlaying
Amostras de código
Reproduzindo Som
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
print("reproduzindo som")
sound.Playing = true
task.wait(10)
print("parando som")
sound.Playing = false
task.wait(5)
sound.Playing = true
local timePosition = sound.TimePosition
print("retomado na posição de tempo: " .. tostring(timePosition)) -- c. 10 segundosSoundId
Sound.SoundId:ContentId
TimeLength
Amostras de código
Tocar um Som por uma Duração Específica
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
Amostras de código
Posição do Tempo do Som
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 = 0Métodos
Pause
Sound:Pause():()
Devolução
()
Amostras de código
Funções de Som
-- criar um som
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- ouvir eventos
sound.Played:Connect(function(_soundId)
print("Evento Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Evento Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Evento Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Evento Sound.Stopped")
end)
sound:Play()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 0
task.wait(10)
sound:Pause()
print("pausar", sound.Playing, sound.TimePosition) -- pausar false 10
task.wait(3)
sound:Resume()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 10
task.wait(3)
sound:Stop()
print("parar", sound.Playing, sound.TimePosition) -- parar false 0
task.wait(3)
sound:Play()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 0pause
Play
Sound:Play():()
Devolução
()
Amostras de código
Funções de Som
-- criar um som
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- ouvir eventos
sound.Played:Connect(function(_soundId)
print("Evento Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Evento Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Evento Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Evento Sound.Stopped")
end)
sound:Play()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 0
task.wait(10)
sound:Pause()
print("pausar", sound.Playing, sound.TimePosition) -- pausar false 10
task.wait(3)
sound:Resume()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 10
task.wait(3)
sound:Stop()
print("parar", sound.Playing, sound.TimePosition) -- parar false 0
task.wait(3)
sound:Play()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 0play
Resume
Sound:Resume():()
Devolução
()
Amostras de código
Funções de Som
-- criar um som
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- ouvir eventos
sound.Played:Connect(function(_soundId)
print("Evento Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Evento Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Evento Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Evento Sound.Stopped")
end)
sound:Play()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 0
task.wait(10)
sound:Pause()
print("pausar", sound.Playing, sound.TimePosition) -- pausar false 10
task.wait(3)
sound:Resume()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 10
task.wait(3)
sound:Stop()
print("parar", sound.Playing, sound.TimePosition) -- parar false 0
task.wait(3)
sound:Play()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 0Stop
Sound:Stop():()
Devolução
()
Amostras de código
Funções de Som
-- criar um som
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- ouvir eventos
sound.Played:Connect(function(_soundId)
print("Evento Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Evento Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Evento Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Evento Sound.Stopped")
end)
sound:Play()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 0
task.wait(10)
sound:Pause()
print("pausar", sound.Playing, sound.TimePosition) -- pausar false 10
task.wait(3)
sound:Resume()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 10
task.wait(3)
sound:Stop()
print("parar", sound.Playing, sound.TimePosition) -- parar false 0
task.wait(3)
sound:Play()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 0stop
Eventos
DidLoop
Amostras de código
Repetir um Número de Vezes
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
-- desconectar a conexão
connection:Disconnect()
-- parar o som
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)Loaded
Parâmetros
Amostras de código
Carregar Som
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("O som foi carregado!")Paused
Parâmetros
Amostras de código
Funções de Som
-- criar um som
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- ouvir eventos
sound.Played:Connect(function(_soundId)
print("Evento Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Evento Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Evento Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Evento Sound.Stopped")
end)
sound:Play()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 0
task.wait(10)
sound:Pause()
print("pausar", sound.Playing, sound.TimePosition) -- pausar false 10
task.wait(3)
sound:Resume()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 10
task.wait(3)
sound:Stop()
print("parar", sound.Playing, sound.TimePosition) -- parar false 0
task.wait(3)
sound:Play()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 0Played
Parâmetros
Amostras de código
Funções de Som
-- criar um som
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- ouvir eventos
sound.Played:Connect(function(_soundId)
print("Evento Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Evento Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Evento Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Evento Sound.Stopped")
end)
sound:Play()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 0
task.wait(10)
sound:Pause()
print("pausar", sound.Playing, sound.TimePosition) -- pausar false 10
task.wait(3)
sound:Resume()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 10
task.wait(3)
sound:Stop()
print("parar", sound.Playing, sound.TimePosition) -- parar false 0
task.wait(3)
sound:Play()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 0Resumed
Parâmetros
Amostras de código
Funções de Som
-- criar um som
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- ouvir eventos
sound.Played:Connect(function(_soundId)
print("Evento Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Evento Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Evento Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Evento Sound.Stopped")
end)
sound:Play()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 0
task.wait(10)
sound:Pause()
print("pausar", sound.Playing, sound.TimePosition) -- pausar false 10
task.wait(3)
sound:Resume()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 10
task.wait(3)
sound:Stop()
print("parar", sound.Playing, sound.TimePosition) -- parar false 0
task.wait(3)
sound:Play()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 0Stopped
Parâmetros
Amostras de código
Funções de Som
-- criar um som
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- ouvir eventos
sound.Played:Connect(function(_soundId)
print("Evento Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Evento Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Evento Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Evento Sound.Stopped")
end)
sound:Play()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 0
task.wait(10)
sound:Pause()
print("pausar", sound.Playing, sound.TimePosition) -- pausar false 10
task.wait(3)
sound:Resume()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 10
task.wait(3)
sound:Stop()
print("parar", sound.Playing, sound.TimePosition) -- parar false 0
task.wait(3)
sound:Play()
print("tocar", sound.Playing, sound.TimePosition) -- tocar true 0