Clase de motor
Sound
*Este contenido se traduce usando la IA (Beta) y puede contener errores. Para ver esta página en inglés, haz clic en aquí.
Resumen
Propiedades
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 |
Muestras de código
Parte de Música Reproduciendo
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
-- crear un sonido
local sound = Instance.new("Sound", part)
sound.SoundId = "rbxassetid://9120386436"
sound.EmitterSize = 5 -- disminuir el tamaño del emisor (para una caída de volumen más temprana)
sound.Looped = true
sound.Parent = part
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = part
-- alternar la reproducción del sonido / no reproducido
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)Referencia API
Propiedades
EmitterSize
IsLoaded
Muestras de código
Cargar Sonido
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("¡El sonido se ha cargado!")IsPaused
Muestras de código
Sonido SeEstáReproduciendo y SonidoEstá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) -- verdadero, falso
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- falso, verdadero
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- verdadero, falso
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- falso, verdaderoIsPlaying
Muestras de código
Sonido SeEstáReproduciendo y SonidoEstá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) -- verdadero, falso
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- falso, verdadero
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- verdadero, falso
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- falso, verdaderoisPlaying
Looped
Muestras de código
Repetir un Número de Veces
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 la conexión
connection:Disconnect()
-- detener el sonido
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)MaxDistance
MinDistance
Pitch
PlaybackLoudness
Muestras de código
Barra de Amplitud de Volumen
-- para ser colocado en StarterPlayer > StarterPlayerScripts
local Players = game:GetService("Players")
-- espera al PlayerGui del jugador local
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")
-- crea un ScreenGui
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = playerGui
-- crea un contenedor para nuestra 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
-- crea una 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
-- crea un sonido
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = screenGui
sound:Play()
-- define una máxima volumen
local maxLoudness = 30
-- anima la barra de amplitud
while true do
local amplitude = math.clamp(sound.PlaybackLoudness / maxLoudness, 0, 1)
bar.Size = UDim2.new(amplitude, 0, 1, 0)
wait(0.05)
endPlaybackSpeed
Muestras de código
Velocidad de Reproducción de Sonido
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
sound:Play()
task.wait(10)
sound.PlaybackSpeed = 3 -- 3x más rápido
task.wait(5)
sound.PlaybackSpeed = 0.5 -- 2x más lento
task.wait(5)
sound.PlaybackSpeed = 1 -- predeterminadoPlaying
Muestras de código
Reproduciendo Sonido
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
print("reproduciendo sonido")
sound.Playing = true
task.wait(10)
print("deteniendo sonido")
sound.Playing = false
task.wait(5)
sound.Playing = true
local timePosition = sound.TimePosition
print("reanudar en la posición de tiempo: " .. tostring(timePosition)) -- c. 10 segundosSoundId
Sound.SoundId:ContentId
TimeLength
Muestras de código
Reproducir un sonido durante una duración 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
Muestras de código
Tiempo de Sonido
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():()
Devuelve
()
Muestras de código
Funciones de Sonido
-- crear un sonido
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- escuchar 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("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0pause
Play
Sound:Play():()
Devuelve
()
Muestras de código
Funciones de Sonido
-- crear un sonido
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- escuchar 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("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0play
Resume
Sound:Resume():()
Devuelve
()
Muestras de código
Funciones de Sonido
-- crear un sonido
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- escuchar 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("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0Stop
Sound:Stop():()
Devuelve
()
Muestras de código
Funciones de Sonido
-- crear un sonido
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- escuchar 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("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0stop
Eventos
DidLoop
Muestras de código
Repetir un Número de Veces
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 la conexión
connection:Disconnect()
-- detener el sonido
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)Loaded
Parámetros
Muestras de código
Cargar Sonido
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("¡El sonido se ha cargado!")Paused
Parámetros
Muestras de código
Funciones de Sonido
-- crear un sonido
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- escuchar 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("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0Played
Parámetros
Muestras de código
Funciones de Sonido
-- crear un sonido
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- escuchar 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("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0Resumed
Parámetros
Muestras de código
Funciones de Sonido
-- crear un sonido
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- escuchar 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("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0Stopped
Parámetros
Muestras de código
Funciones de Sonido
-- crear un sonido
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- escuchar 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("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0