Sound

Veraltete anzeigen

*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.

Musik Spielender Teil

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

Methoden

Events

Eigenschaften

AudioContent

Parallel lesen

ChannelCount

Schreibgeschützt
Nicht repliziert
Nicht durchsuchbar
Roblox-Skript-Sicherheit
Parallel lesen

IsLoaded

Schreibgeschützt
Nicht repliziert
Parallel lesen

Code-Beispiele

Geräusch laden

local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("Das Geräusch wurde geladen!")

IsMutedForCapture

Roblox-Skript-Sicherheit
Parallel lesen

IsPaused

Verborgen
Schreibgeschützt
Nicht repliziert
Parallel lesen

Code-Beispiele

Sound IstPlaying und SoundIstPaused

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, wahr

IsPlaying

Verborgen
Schreibgeschützt
Nicht repliziert
Parallel lesen

Code-Beispiele

Sound IstPlaying und SoundIstPaused

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, wahr

Looped

Parallel lesen

Code-Beispiele

Zahl mehrmals wiederholen

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

Parallel lesen

PlaybackLoudness

Schreibgeschützt
Nicht repliziert
Parallel lesen

Code-Beispiele

Lautstärke Amplitudenanzeige

-- 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)
end

PlaybackRegion

Parallel lesen

PlaybackRegionsEnabled

Parallel lesen

PlaybackSpeed

Nicht repliziert
Parallel lesen

Code-Beispiele

Sound Wiedergabegeschwindigkeit

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 -- Standard

Playing

Nicht repliziert
Parallel lesen

Code-Beispiele

Sound Wiedergabe

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 Sekunden

PlayOnRemove

Parallel lesen

RollOffMaxDistance

Parallel lesen

RollOffMinDistance

Parallel lesen

RollOffMode

Parallel lesen

SoundGroup

Parallel lesen

SoundId

ContentId
Parallel lesen

TimeLength

Schreibgeschützt
Nicht repliziert
Parallel lesen

Code-Beispiele

Spiele einen Sound für eine bestimmte Dauer

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

Nicht repliziert
Parallel lesen

Code-Beispiele

Ton Zeitposition

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

Parallel lesen

Methoden

Pause

()

Rückgaben

()

Code-Beispiele

Sound-Funktionen

-- 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

Play

()

Rückgaben

()

Code-Beispiele

Sound-Funktionen

-- 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

Resume

()

Rückgaben

()

Code-Beispiele

Sound-Funktionen

-- 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

Stop

()

Rückgaben

()

Code-Beispiele

Sound-Funktionen

-- 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

Events

DidLoop

Parameter

soundId: string
numOfTimesLooped: number

Code-Beispiele

Zahl mehrmals wiederholen

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)

Ended

Parameter

soundId: string

Loaded

Parameter

soundId: string

Code-Beispiele

Geräusch laden

local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("Das Geräusch wurde geladen!")

Paused

Parameter

soundId: string

Code-Beispiele

Sound-Funktionen

-- 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

Played

Parameter

soundId: string

Code-Beispiele

Sound-Funktionen

-- 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

Resumed

Parameter

soundId: string

Code-Beispiele

Sound-Funktionen

-- 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

Stopped

Parameter

soundId: string

Code-Beispiele

Sound-Funktionen

-- 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