Sound
*Dieser Inhalt wurde mit KI (Beta) übersetzt und kann Fehler enthalten. Um diese Seite auf Englisch zu sehen, klicke hier.
Sound ist ein Objekt, das Geräusche ausgibt. Wenn es in einem BasePart oder einem Attachment platziert wird, gibt dieses Objekt sein Geräusch von der Position des BasePart.Position oder der Attachment.WorldPosition des Anhangs aus. Bei dieser Platzierung weist ein Sound das Dopplereffekt auf, was bedeutet, dass seine Frequenz und Tonhöhe mit der relativen Bewegung des Anhangs oder Teils, an dem es befestigt ist, variieren. Darüber hinaus wird das Volumen durch den Abstand zwischen dem Soundlistener des Clients (standardmäßig die Position der Camera) und der Position des Elternteils des Sounds bestimmt. Für weitere Informationen siehe RollOffMode.
Ein Geräusch wird als "global" betrachtet, wenn es nicht an einen BasePart oder ein Attachment angehängt ist. In diesem Fall wird das Geräusch mit dem gleichen Volumen im gesamten Ort abgespielt.
Code-Beispiele
Der Code in diesem Beispiel zeigt, wie ein Sound, der an einen Teil oder Attachment angehängt ist, lokal abgespielt wird und die Lautstärke abnimmt, je weiter die Kamera des Spielers vom Teil entfernt ist.
Ein Teil wird instanziiert, und ein Sound wird instanziiert und an den Teil gangen. Ein Klickdetektor ist auf dem Teil eingerichtet, der prüfen wird, ob der Sound abgespielt wird, unter Verwendung von Sound.IsPlaying und spielt either den Sound oder stoppt ihn entsprechend.
local part = Instance.new("Part")
part.Anchored = true
part.Position = Vector3.new(0, 3, 0)
part.BrickColor = BrickColor.new("Hellrot")
part.Name = "Musikbox"
part.Parent = workspace
-- ein Geräusch erstellen
local sound = Instance.new("Sound", part)
sound.SoundId = "rbxassetid://9120386436"
sound.EmitterSize = 5 -- verringere die Emittergröße (für frühere Lautstärkeabnahme)
sound.Looped = true
sound.Parent = part
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = part
-- das Abspielen des Sounds ein- / ausschalten
clickDetector.MouseClick:Connect(function()
if not sound.IsPlaying then
part.BrickColor = BrickColor.new("Hellgrün")
sound:Play()
else
part.BrickColor = BrickColor.new("Hellrot")
sound:Stop()
end
end)Zusammenfassung
Eigenschaften
Diese Eigenschaft ist true, wenn der Sound von Roblox Servern geladen wurde und bereit ist, gespielt zu werden.
Schreibgeschützte Eigenschaft, die true zurückgibt, wenn der Sound nicht spielt.
Schreibgeschützte Eigenschaft, die true zurückgibt, wenn der Sound spielt.
Legt fest, ob der Sound wiederholt werden soll, nachdem er fertig ist mit dem Spielen.
Ein Bereich, der einen gewünschten Schleifenstart und Schleifenende innerhalb der PlaybackRegion, in Sekunden, darstellt.
Eine Zahl zwischen 0 und 1000, die angibt, wie laut der Sound aktuell wiedergegeben wird.
Ein Bereich, der einen gewünschten Start- und Endzeitpunkt innerhalb der TimeLength, in Sekunden, darstellt.
Wenn true, gewährt diese Eigenschaft Ihrem Sound Zugriff auf die PlaybackRegion und LoopRegion Eigenschaften, die die Wiedergabe genauer steuern können.
Bestimmt die Geschwindigkeit, mit der ein Sound abgespielt wird, wobei höhere Werte den Sound schneller und mit höherem Ton wiedergeben lassen.
Gibt an, ob der Sound gerade spielt.
Wenn true, wird der Sound abgespielt, wenn er aus der Erfahrung entfernt wird.
Die maximale Entfernung in Studs, die ein Clients Hörer vom Ursprung des Sounds erreichen kann und ihn dennoch hören kann. Gilt nur für Sounds , die an ein BasePart oder Attachment angehängt sind.
Die minimale Entfernung in Studs, bei der ein Sound, das an ein BasePart oder Attachment angehängt ist, zu dämpfen beginnt (Verringerung der Lautstärke).
Steuert, wie die Lautstärke eines Sound, das an ein BasePart oder Attachment angehängt ist, sich dämpft (ausfällt), während sich die Entfernung zwischen dem Hörer und dem Elternteil ändert.
Die SoundGroup, die mit diesem Sound verknüpft ist.
Content-ID der Sounddatei, die mit dem Sound verknüpft werden soll.
Die Länge des Sound in Sekunden.
Der Fortschritt des Sound in Sekunden. Kann geändert werden, um die Wiedergabeposition des Sound sowohl vor als auch während der Wiedergabe zu ändern.
Die Lautstärke des Sound.
Methoden
Eigenschaften
AudioContent
ChannelCount
IsLoaded
Code-Beispiele
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("Das Geräusch wurde geladen!")IsMutedForCapture
IsPaused
Code-Beispiele
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) -- wahr, falsch
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- falsch, wahr
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- wahr, falsch
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- falsch, wahrIsPlaying
Code-Beispiele
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) -- wahr, falsch
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- falsch, wahr
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- wahr, falsch
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- falsch, wahrLooped
Code-Beispiele
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
-- die Verbindung trennen
connection:Disconnect()
-- den Sound stoppen
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)LoopRegion
PlaybackLoudness
Code-Beispiele
-- muss in StarterPlayer > StarterPlayerScripts platziert werden
local Players = game:GetService("Players")
-- auf den lokalen Spieler PlayerGui warten
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")
-- ein ScreenGui erstellen
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = playerGui
-- einen Halter für unsere Anzeige erstellen
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
-- eine Anzeige erstellen
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
-- einen Ton erstellen
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = screenGui
sound:Play()
-- eine maximale Lautstärke definieren
local maxLoudness = 30
-- die Amplitudenanzeige animieren
while true do
local amplitude = math.clamp(sound.PlaybackLoudness / maxLoudness, 0, 1)
bar.Size = UDim2.new(amplitude, 0, 1, 0)
wait(0.05)
endPlaybackRegion
PlaybackRegionsEnabled
PlaybackSpeed
Code-Beispiele
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
sound:Play()
task.wait(10)
sound.PlaybackSpeed = 3 -- 3x schneller
task.wait(5)
sound.PlaybackSpeed = 0.5 -- 2x langsamer
task.wait(5)
sound.PlaybackSpeed = 1 -- StandardPlaying
Code-Beispiele
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
print("Spiele Sound")
sound.Playing = true
task.wait(10)
print("Stoppe Sound")
sound.Playing = false
task.wait(5)
sound.Playing = true
local timePosition = sound.TimePosition
print("Fortgesetzt an der Zeitposition: " .. tostring(timePosition)) -- ca. 10 SekundenPlayOnRemove
RollOffMaxDistance
RollOffMinDistance
RollOffMode
SoundGroup
SoundId
TimeLength
Code-Beispiele
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
Code-Beispiele
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 = 0Volume
Methoden
Pause
Rückgaben
Code-Beispiele
-- Erstelle einen Sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- Höre auf Ereignisse
sound.Played:Connect(function(_soundId)
print("Sound.Played-Ereignis")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused-Ereignis")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed-Ereignis")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped-Ereignis")
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
Rückgaben
Code-Beispiele
-- Erstelle einen Sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- Höre auf Ereignisse
sound.Played:Connect(function(_soundId)
print("Sound.Played-Ereignis")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused-Ereignis")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed-Ereignis")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped-Ereignis")
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 0Resume
Rückgaben
Code-Beispiele
-- Erstelle einen Sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- Höre auf Ereignisse
sound.Played:Connect(function(_soundId)
print("Sound.Played-Ereignis")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused-Ereignis")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed-Ereignis")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped-Ereignis")
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
Rückgaben
Code-Beispiele
-- Erstelle einen Sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- Höre auf Ereignisse
sound.Played:Connect(function(_soundId)
print("Sound.Played-Ereignis")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused-Ereignis")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed-Ereignis")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped-Ereignis")
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 0Events
DidLoop
Parameter
Code-Beispiele
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
-- die Verbindung trennen
connection:Disconnect()
-- den Sound stoppen
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)Loaded
Parameter
Code-Beispiele
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("Das Geräusch wurde geladen!")Paused
Parameter
Code-Beispiele
-- Erstelle einen Sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- Höre auf Ereignisse
sound.Played:Connect(function(_soundId)
print("Sound.Played-Ereignis")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused-Ereignis")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed-Ereignis")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped-Ereignis")
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
Parameter
Code-Beispiele
-- Erstelle einen Sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- Höre auf Ereignisse
sound.Played:Connect(function(_soundId)
print("Sound.Played-Ereignis")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused-Ereignis")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed-Ereignis")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped-Ereignis")
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
Parameter
Code-Beispiele
-- Erstelle einen Sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- Höre auf Ereignisse
sound.Played:Connect(function(_soundId)
print("Sound.Played-Ereignis")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused-Ereignis")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed-Ereignis")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped-Ereignis")
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
Parameter
Code-Beispiele
-- Erstelle einen Sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- Höre auf Ereignisse
sound.Played:Connect(function(_soundId)
print("Sound.Played-Ereignis")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused-Ereignis")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed-Ereignis")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped-Ereignis")
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