Impara
Classe motore
Sound

*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.


Campioni di codice
Parte di Riproduzione Musicale
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
-- crea un suono
local sound = Instance.new("Sound", part)
sound.SoundId = "rbxassetid://9120386436"
sound.EmitterSize = 5 -- riduci la dimensione dell'emettitore (per un abbassamento del volume
-- più precoce)
sound.Looped = true
sound.Parent = part
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = part
-- attiva/disattiva la riproduzione del suono
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)

Riferimento API
Proprietà
AcousticSimulationEnabled
Lettura Parallela
Capacità: LegacySound
Sound.AcousticSimulationEnabled:boolean

AudioContent
Nascosto
Lettura Parallela
Capacità: LegacySound
Sound.AudioContent:Content

EmitterSize
Obsoleto

IsLoaded
Sola Lettura
Non Replicato
Lettura Parallela
Capacità: LegacySound
Sound.IsLoaded:boolean
Campioni di codice
Carica suono
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("Il suono è stato caricato!")

IsPaused
Nascosto
Sola Lettura
Non Replicato
Lettura Parallela
Capacità: LegacySound
Sound.IsPaused:boolean
Campioni di codice
Suono IsPlaying e SoundIsPaused
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
Nascosto
Sola Lettura
Non Replicato
Lettura Parallela
Capacità: LegacySound
Sound.IsPlaying:boolean
Campioni di codice
Suono IsPlaying e SoundIsPaused
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
Obsoleto

Looped
Lettura Parallela
Capacità: LegacySound
Sound.Looped:boolean
Campioni di codice
Esegui un Numero di Volte
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
-- disconnetti la connessione
connection:Disconnect()
-- ferma il suono
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)

LoopRegion
Lettura Parallela
Capacità: LegacySound
Sound.LoopRegion:NumberRange

MaxDistance
Obsoleto

MinDistance
Obsoleto

Pitch
Obsoleto

PlaybackLoudness
Sola Lettura
Non Replicato
Lettura Parallela
Capacità: LegacySound
Sound.PlaybackLoudness:number
Campioni di codice
Bar di Ampiezza del Volume
-- da posizionare in StarterPlayer > StarterPlayerScripts
local Players = game:GetService("Players")
-- aspetta il PlayerGui del giocatore locale
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")
-- crea uno ScreenGui
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = playerGui
-- crea un contenitore per la nostra 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 suono
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = screenGui
sound:Play()
-- definisci un massimo di ampiezza
local maxLoudness = 30
-- anima la barra di ampiezza
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
Lettura Parallela
Capacità: LegacySound
Sound.PlaybackRegion:NumberRange

PlaybackRegionsEnabled
Lettura Parallela
Capacità: LegacySound
Sound.PlaybackRegionsEnabled:boolean

PlaybackSpeed
Non Replicato
Lettura Parallela
Capacità: LegacySound
Sound.PlaybackSpeed:number
Campioni di codice
Velocità di Riproduzione del Suono
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
sound:Play()
task.wait(10)
sound.PlaybackSpeed = 3 -- 3x più veloce
task.wait(5)
sound.PlaybackSpeed = 0.5 -- 2x più lento
task.wait(5)
sound.PlaybackSpeed = 1 -- predefinito

Playing
Non Replicato
Lettura Parallela
Capacità: LegacySound
Sound.Playing:boolean
Campioni di codice
Riproduzione del suono
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
print("riproducendo suono")
sound.Playing = true
task.wait(10)
print("fermare suono")
sound.Playing = false
task.wait(5)
sound.Playing = true
local timePosition = sound.TimePosition
print("ripreso alla posizione temporale: " .. tostring(timePosition)) -- c. 10 secondi

PlayOnRemove
Lettura Parallela
Capacità: LegacySound
Sound.PlayOnRemove:boolean

RollOffMaxDistance
Lettura Parallela
Capacità: LegacySound
Sound.RollOffMaxDistance:number

RollOffMinDistance
Lettura Parallela
Capacità: LegacySound
Sound.RollOffMinDistance:number

RollOffMode
Lettura Parallela
Capacità: LegacySound
Sound.RollOffMode:Enum.RollOffMode

SoundGroup
Lettura Parallela
Capacità: LegacySound
Sound.SoundGroup:SoundGroup

SoundId
Lettura Parallela
Capacità: LegacySound
Sound.SoundId:ContentId

TimeLength
Sola Lettura
Non Replicato
Lettura Parallela
Capacità: LegacySound
Sound.TimeLength:number
Campioni di codice
Riproduci un Suono per una Durata Specifica
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
Non Replicato
Lettura Parallela
Capacità: LegacySound
Sound.TimePosition:number
Campioni di codice
Posizione Tempo del Suono
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
Lettura Parallela
Capacità: LegacySound
Sound.Volume:number

Metodi
Pause
Capacità: LegacySound
Sound:Pause():()
Restituzioni
()
Campioni di codice
Funzioni Audio
-- crea un suono
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- ascolta gli eventi
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

pause
Obsoleto

Play
Capacità: LegacySound
Sound:Play():()
Restituzioni
()
Campioni di codice
Funzioni Audio
-- crea un suono
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- ascolta gli eventi
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

play
Obsoleto

Resume
Capacità: LegacySound
Sound:Resume():()
Restituzioni
()
Campioni di codice
Funzioni Audio
-- crea un suono
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- ascolta gli eventi
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

Stop
Capacità: LegacySound
Sound:Stop():()
Restituzioni
()
Campioni di codice
Funzioni Audio
-- crea un suono
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- ascolta gli eventi
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

stop
Obsoleto

Eventi
DidLoop
Capacità: LegacySound
Sound.DidLoop(
soundId:string, numOfTimesLooped:number
Parametri
soundId:string
numOfTimesLooped:number
Campioni di codice
Esegui un Numero di Volte
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
-- disconnetti la connessione
connection:Disconnect()
-- ferma il suono
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)

Ended
Capacità: LegacySound
Sound.Ended(soundId:string):RBXScriptSignal
Parametri
soundId:string

Loaded
Capacità: LegacySound
Sound.Loaded(soundId:string):RBXScriptSignal
Parametri
soundId:string
Campioni di codice
Carica suono
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("Il suono è stato caricato!")

Paused
Capacità: LegacySound
Sound.Paused(soundId:string):RBXScriptSignal
Parametri
soundId:string
Campioni di codice
Funzioni Audio
-- crea un suono
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- ascolta gli eventi
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

Played
Capacità: LegacySound
Sound.Played(soundId:string):RBXScriptSignal
Parametri
soundId:string
Campioni di codice
Funzioni Audio
-- crea un suono
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- ascolta gli eventi
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

Resumed
Capacità: LegacySound
Sound.Resumed(soundId:string):RBXScriptSignal
Parametri
soundId:string
Campioni di codice
Funzioni Audio
-- crea un suono
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- ascolta gli eventi
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

Stopped
Capacità: LegacySound
Sound.Stopped(soundId:string):RBXScriptSignal
Parametri
soundId:string
Campioni di codice
Funzioni Audio
-- crea un suono
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- ascolta gli eventi
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

© 2026 Roblox Corporation. Roblox, il logo Roblox e Powering Imagination sono tra i nostri marchi registrati e non registrati negli Stati Uniti. e altri paesi.