Classe motore
Sound
*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.
Sommario
Proprietà
SoundId:ContentId |
Eventi
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 |
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à
EmitterSize
IsLoaded
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
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, trueIsPlaying
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, trueisPlaying
Looped
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)MaxDistance
MinDistance
Pitch
PlaybackLoudness
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)
endPlaybackSpeed
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 -- predefinitoPlaying
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 secondiSoundId
Sound.SoundId:ContentId
TimeLength
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
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 = 0Metodi
Pause
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 0pause
Play
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 0play
Resume
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 0Stop
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 0stop
Eventi
DidLoop
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)Loaded
Parametri
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
Parametri
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 0Played
Parametri
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 0Resumed
Parametri
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 0Stopped
Parametri
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