local Holder = workspace["MusicHolder"]
local Gui = script.Parent
local Components = {}
local CurrentSong, SongStatus = nil, "Playing"
local SoundEndRender, TimeLeft
local TotalTime
local LastTick, LastLeft
local elapsed
for a, b in pairs(Gui.Frame:GetChildren()) do
Components[b.Name] = b
end
local MusicTitleTransparency = {}
local function MusicTitleFade()
MusicTitleTransparency["TitleIn"] = game.TweenService:Create(Components.Title, TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {TextTransparency = 0})
MusicTitleTransparency["TitleOut"] = game.TweenService:Create(Components.Title, TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {TextTransparency = 1})
MusicTitleTransparency["TitleIn"].Completed:Connect(function()
task.wait(3)
if MusicTitleTransparency["TitleOut"] then
MusicTitleTransparency["TitleOut"]:Play()
end
end)
MusicTitleTransparency["TitleIn"]:Play()
end
local function AssignNumbers()
for i, v in pairs(Holder:GetChildren()) do
local Number = Instance.new("IntValue")
Number.Name = "Number"
Number.Value = i
Number.Parent = v
local Volume = Instance.new("NumberValue")
Volume.Name = "Volume"
Volume.Value = v.Volume
Volume.Parent = v
end
end AssignNumbers()
local function ReturnRandomSong()
return Holder:GetChildren()[math.random(1, #Holder:GetChildren())]
end
local function ReturnNext()
local Current = CurrentSong:FindFirstChild("Number").Value
local Next = (Current + 1)
if Next > #Holder:GetChildren() then Next = 1 end
for _, b in pairs(Holder:GetChildren()) do
if b:FindFirstChild("Number").Value == Next then return b end
end
end
local function ButtonColourTween(Button)
Button.ImageColor3 = Color3.fromRGB(255, 255, 255)
game.TweenService:Create(Button, TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, true), {ImageColor3 = Color3.fromRGB(34, 255, 122)}):Play()
end
local function Effect()
if MusicTitleTransparency["TitleIn"] then
MusicTitleTransparency["TitleIn"]:Cancel()
MusicTitleTransparency["TitleIn"] = nil
end
if MusicTitleTransparency["TitleOut"] then
MusicTitleTransparency["TitleOut"]:Cancel()
MusicTitleTransparency["TitleOut"] = nil
end
Components.Title.TextTransparency = 1
delay(1, MusicTitleFade)
end
local function ReturnPrevious()
local Current = CurrentSong:FindFirstChild("Number").Value
local Prev = (Current - 1)
if Prev < 1 then Prev = #Holder:GetChildren() end
for _, b in pairs(Holder:GetChildren()) do
if b:FindFirstChild("Number").Value == Prev then return b end
end
end
local function StopCurrentSong(Paused, Unknown)
local CS = Unknown or CurrentSong
if not CS then return end
local Tween = game.TweenService:Create(CS, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Volume = 0})
Tween.Completed:Connect(function()
if Paused then CS:Pause() else CS:Stop() end
end)
Tween:Play()
end
local function PlaySong(Sound)
if SoundEndRender then SoundEndRender:Disconnect() end
local function Yield()
local Elapsed, Loaded = 0, false
while Elapsed < 8 do
Elapsed += 1
if Sound.TimeLength > 0.1 then Loaded = true break end
task.wait(1)
end
if not Loaded then warn(Sound.Name .. " ID Unable to Load: Music Player Error"); return PlaySong(ReturnNext()) end
end Yield()
code
SoundEndRender = game["Run Service"].RenderStepped:Connect(function()
if (os.clock() - LastTick) >= 1 and SongStatus == "Playing" then
for _, o in pairs(Holder:GetChildren()) do
if o.Playing and o ~= Sound then StopCurrentSong(false, o) break end
end
elapsed += 1
TimeLeft = (TotalTime - math.floor(Sound.TimePosition + 0.5))
Components.MusicName.Text = Sound.Name .. " [" .. string.format("%i:%02i", elapsed / 60, elapsed % 60) .. "]"
if TimeLeft <= 0 or (TimeLeft > LastLeft) then
SoundEndRender:Disconnect()
CurrentSong = ReturnNext()
PlaySong(CurrentSong)
else
TimeLeft -= 1
end
LastLeft = TimeLeft
local FillX = ((math.floor(Sound.TimePosition + 0.5)) * 100) / TotalTime
FillX = (FillX / 100) * 1
game.TweenService:Create(Components.Progress.Fill, TweenInfo.new(0.1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Size = UDim2.fromScale(FillX, Components.Progress.Fill.Size.Y.Scale)}):Play()
LastTick = os.clock()
end
end)
if SongStatus == "Paused" then
Components.Title.Text = "Currently Resumed"
Sound:Resume()
else
TotalTime = math.floor(Sound.TimeLength + 0.5)
LastTick, LastLeft = 0, TotalTime
elapsed = 0
Components.MusicName.Text = Sound.Name .. " [0:00]"
Components.Title.Text = "Currently Playing"
Sound:Play()
end
Sound.Volume = 0
game.TweenService:Create(Sound, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Volume = Sound:FindFirstChild("Volume").Value}):Play()
Effect()
Gerne, hier ist die Kombination der beiden Skripte.
Ich habe das erste Skript so umgeschrieben, dass es die Song-IDs aus der songs-Tabelle des zweiten Skripts verwendet, anstatt nach Sound-Objekten in einem "MusicHolder"-Ordner im Workspace zu suchen.
Was wurde geändert?
Song-Quelle: Das Skript verwendet jetzt die songs-Tabelle als Quelle für die Musik.
Kein Workspace-Ordner nötig: Du musst keinen "MusicHolder"-Ordner mehr im workspace haben. Das Skript erstellt die Sound-Objekte jetzt dynamisch.
Dynamisches Laden: Eine neue Funktion LoadSongs wurde erstellt. Sie durchläuft die songs-Tabelle, erstellt für jede ID ein Sound-Objekt und versucht, den echten Songtitel vom Roblox-Marktplatz abzurufen.
Vereinfachte Logik: Die Funktionen zum Abrufen des nächsten/vorherigen Songs wurden angepasst, um mit der neuen Tabellenstruktur zu arbeiten, was den Code sauberer und effizienter macht.
Vereinfachte Lautstärke: Die Lautstärke wird jetzt auf einen Standardwert gesetzt, anstatt sie von einem NumberValue-Objekt zu lesen.
Kombinierter und verbesserter Code
lua
-- Roblox Services
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local MarketplaceService = game:GetService("MarketplaceService")
-- GUI- und Skript-Referenzen
local Gui = script.Parent
local Components = {}
for _, b in pairs(Gui.Frame:GetChildren()) do
Components[b.Name] = b
end
-- Die Liste der Songs, die abgespielt werden sollen
local songs = {
"rbxassetid://9043887091",
"rbxassetid://1845341094",
"rbxassetid://9044565954",
"rbxassetid://9046862941",
"rbxassetid://9046863579",
"rbxassetid://9046864489",
"rbxassetid://1839363112",
"rbxassetid://1840901237",
"rbxassetid://1847417433",
"rbxassetid://1847245265",
"rbxassetid://1840712882",
"rbxassetid://1847358730",
}
-- Variablen für den Musik-Player-Status
local MusicHolder = {} -- Diese Tabelle wird die erstellten Sound-Objekte enthalten
local CurrentSong, SongStatus = nil, "Playing"
local SoundEndRender, TimeLeft
local TotalTime
local LastTick, LastLeft
local elapsed
local DEFAULT_VOLUME = 0.5 -- Standard-Lautstärke für alle Songs
-- Funktion zum Laden der Songs aus der Tabelle
local function LoadSongs()
-- Ein Ordner, um die erstellten Sounds zu speichern und den Workspace sauber zu halten
local soundContainer = Instance.new("Folder")
soundContainer.Name = "MusicPlayerSounds"
soundContainer.Parent = script
for i, songId in ipairs(songs) do
local sound = Instance.new("Sound")
sound.SoundId = songId
sound.Volume = DEFAULT_VOLUME
sound.Parent = soundContainer
-- Versuche, den echten Song-Namen vom Marktplatz zu erhalten
local success, info = pcall(function()
return MarketplaceService:GetProductInfo(tonumber(songId:match("%d+")))
end)
if success and info then
sound.Name = info.Name
else
sound.Name = "Unbekannter Song " .. i
warn("Konnte Informationen für Song-ID nicht laden: " .. songId)
end
table.insert(MusicHolder, sound)
end
end
-- Funktionen für die Titel-Animation
local MusicTitleTransparency = {}
local function MusicTitleFade()
MusicTitleTransparency["TitleIn"] = TweenService:Create(Components.Title, TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {TextTransparency = 0})
MusicTitleTransparency["TitleOut"] = TweenService:Create(Components.Title, TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {TextTransparency = 1})
MusicTitleTransparency["TitleIn"].Completed:Connect(function()
task.wait(3)
if MusicTitleTransparency["TitleOut"] then
MusicTitleTransparency["TitleOut"]:Play()
end
end)
MusicTitleTransparency["TitleIn"]:Play()
end
local function Effect()
if MusicTitleTransparency["TitleIn"] then MusicTitleTransparency["TitleIn"]:Cancel() end
if MusicTitleTransparency["TitleOut"] then MusicTitleTransparency["TitleOut"]:Cancel() end
Components.Title.TextTransparency = 1
delay(1, MusicTitleFade)
end
-- Funktionen zur Song-Navigation
local function ReturnRandomSong()
if #MusicHolder == 0 then return nil end
return MusicHolder[math.random(1, #MusicHolder)]
end
local function ReturnNext()
if not CurrentSong then return ReturnRandomSong() end
local currentIndex = table.find(MusicHolder, CurrentSong)
if not currentIndex then return ReturnRandomSong() end
local nextIndex = currentIndex + 1
if nextIndex > #MusicHolder then
nextIndex = 1
end
return MusicHolder[nextIndex]
end
local function ReturnPrevious()
if not CurrentSong then return ReturnRandomSong() end
local currentIndex = table.find(MusicHolder, CurrentSong)
if not currentIndex then return ReturnRandomSong() end
local prevIndex = currentIndex - 1
if prevIndex < 1 then
prevIndex = #MusicHolder
end
return MusicHolder[prevIndex]
end
-- Funktion, um einen Knopf-Effekt zu erzeugen
local function ButtonColourTween(Button)
Button.ImageColor3 = Color3.fromRGB(255, 255, 255)
TweenService:Create(Button, TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, true), {ImageColor3 = Color3.fromRGB(34, 255, 122)}):Play()
end
-- Kernfunktionen des Players
local function StopCurrentSong(Paused, Unknown)
local CS = Unknown or CurrentSong
if not CS then return end
local Tween = TweenService:Create(CS, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Volume = 0})
Tween.Completed:Connect(function()
if Paused then CS:Pause() else CS:Stop() end
end)
Tween:Play()
end
local function PlaySong(Sound)
if not Sound then warn("Kein Song zum Abspielen gefunden."); return end
if SoundEndRender then SoundEndRender:Disconnect() end
-- Warten, bis der Song geladen ist (maximal 8 Sekunden)
local Loaded = false
for i = 1, 8 do
if Sound.TimeLength > 0.1 then
Loaded = true
break
end
task.wait(1)
end
if not Loaded then
warn(Sound.Name .. " konnte nicht geladen werden. Überspringe zum nächsten Song.")
PlaySong(ReturnNext())
return
end
-- Stoppe alle anderen Songs, die möglicherweise noch spielen
for _, otherSound in ipairs(MusicHolder) do
if otherSound.Playing and otherSound ~= Sound then
StopCurrentSong(false, otherSound)
end
end
-- Render-Schleife für die Zeit- und Fortschrittsanzeige
SoundEndRender = RunService.RenderStepped:Connect(function()
if (os.clock() - LastTick) >= 1 and SongStatus == "Playing" then
elapsed += 1
TimeLeft = (TotalTime - math.floor(Sound.TimePosition + 0.5))
Components.MusicName.Text = Sound.Name .. " [" .. string.format("%i:%02i", elapsed / 60, elapsed % 60) .. "]"
-- Prüfen, ob der Song zu Ende ist oder gesprungen ist
if TimeLeft <= 0 or (TimeLeft > LastLeft) then
SoundEndRender:Disconnect()
CurrentSong = ReturnNext()
PlaySong(CurrentSong)
end
LastLeft = TimeLeft
-- Fortschrittsbalken aktualisieren
local FillX = (Sound.TimePosition / TotalTime)
TweenService:Create(Components.Progress.Fill, TweenInfo.new(0.1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Size = UDim2.fromScale(FillX, 1)}):Play()
LastTick = os.clock()
end
end)
if SongStatus == "Paused" then
Components.Title.Text = "Wiedergabe fortgesetzt"
Sound:Resume()
else
TotalTime = math.floor(Sound.TimeLength + 0.5)
LastTick, LastLeft = 0, TotalTime
elapsed = 0
Components.MusicName.Text = Sound.Name .. " [0:00]"
Components.Title.Text = "Jetzt spielt"
Sound:Play()
end
SongStatus = "Playing"
Sound.Volume = 0
TweenService:Create(Sound, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Volume = DEFAULT_VOLUME}):Play()
Effect()
end
-- Knopf-Verbindungen
local Debounce = false
Components.Control.Pause.Activated:Connect(function()
if Debounce then return end
Debounce = true
ButtonColourTween(Components.Control.Pause.ImageLabel)
if SongStatus == "Paused" then
PlaySong(CurrentSong)
Components.Control.Pause.ImageLabel.Image = "rbxassetid://13980756617" -- Pause-Icon
else
Components.Title.Text = "Pausiert"
Effect()
SongStatus = "Paused"
StopCurrentSong(true)
Components.Control.Pause.ImageLabel.Image = "rbxassetid://5048853382" -- Play-Icon
end
task.wait(0.4) Debounce = false
end)
Components.Control.Next.Activated:Connect(function()
if Debounce then return end
Debounce = true
ButtonColourTween(Components.Control.Next.ImageLabel)
StopCurrentSong(false)
Components.Control.Pause.ImageLabel.Image = "rbxassetid://13980756617" -- Sicherstellen, dass das Pause-Icon angezeigt wird
CurrentSong = ReturnNext()
PlaySong(CurrentSong)
task.wait(0.4) Debounce = false
end)
Components.Control.Previous.Activated:Connect(function()
if Debounce then return end
Debounce = true
ButtonColourTween(Components.Control.Previous.ImageLabel)
StopCurrentSong(false)
Components.Control.Pause.ImageLabel.Image = "rbxassetid://13980756617" -- Sicherstellen, dass das Pause-Icon angezeigt wird
CurrentSong = ReturnPrevious()
PlaySong(CurrentSong)
task.wait(0.4) Debounce = false
end)
-- Starte den Player
LoadSongs()
CurrentSong = ReturnRandomSong()
PlaySong(CurrentSong)