Help my songs wont loop

Help my songs work and it goes by the list, but once it’s the last song the script breaks, it plays the first song then stops and then it just wont play that first song, but plays the second song, I want it to loop after the last song to play the first song, then every other song once again, to the last one, and then the first one once again after the last one forever

Here’s the script:

local currentTrack = game.Workspace.Songs.CurrentTrack

while (true) do
	wait(0.1)
	l__Songs__1.SubZero.Playing = true
	currentTrack.Value = "Sub Zero - Lil Uzi Vert"
	wait(120.084)
	
	l__Songs__1.SubZero.Playing = false
	l__Songs__1.SlowMotion.Playing = true
	currentTrack.Value = "Slow Motion - Trey Songz"
	wait(120.216)
	
	l__Songs__1.SlowMotion.Playing = false
	l__Songs__1.JustForTheNight.Playing = true
	currentTrack.Value = "Just For The Night - Chris Brown, Migos"
	wait(80.169)
	
	l__Songs__1.JustForTheNight.Playing = false
	l__Songs__1.JocelynFlores.Playing = true
	currentTrack.Value = "Jocelyn Flores - XXXtentaction"
	wait(117.472)
	
	l__Songs__1.JocelynFlores.Playing = false
	l__Songs__1.Hotel.Playing = true
	currentTrack.Value = "Hotel - Kid Ink"
	wait(327.520)
	
	l__Songs__1.Hotel.Playing = false
	l__Songs__1.Homies.Playing = true
	currentTrack.Value = "Arizona Zervas - Homies"
	wait(220.368)
	
	l__Songs__1.Homies.Playing = false
	l__Songs__1.Ilikemebetter.Playing = true
	currentTrack.Value = "I like me better - Lauv "
	wait(105.351)
	
	l__Songs__1.Ilikemebetter.Playing = false
	l__Songs__1.LensV2.Playing = true
	currentTrack.Value = "Lens V2 - Travis Scott"
	wait(379.637)
	
	l__Songs__1.LensV2.Playing = false
	l__Songs__1.coolerthanme.Playing = true
	currentTrack.Value = "Cooler than me - Mike Posner"
	wait(116.453);
	
	l__Songs__1.coolerthanme.Playing = false
	l__Songs__1.cityofroses.Playing = true
	currentTrack.Value = "City of Roses - Tyus"
	wait(120.032)
	l__Songs__1.cityofroses.Playing = false
	l__Songs__1.blur.Playing = true
	currentTrack.Value = "Blur - Stellar"
	wait(131.526)
	
	l__Songs__1.blur.Playing = false
	l__Songs__1.goat.Playing = true
	currentTrack.Value = "G.O.A.T"
	wait(50.05)
	
	l__Songs__1.goat.Playing = false
	l__Songs__1.bereal.Playing = true
	currentTrack.Value = "Be Real - Kid Ink (Remix)"
	wait(170.352)
	
	l__Songs__1.bereal.Playing = false
	l__Songs__1.onelasttime.Playing = true
	currentTrack.Value = "One Last Time - Ariana Grande"
	wait(247.056)
	
	l__Songs__1.onelasttime.Playing = false
	l__Songs__1.getonyourknees.Playing = true
	currentTrack.Value = "Get on your knees - Ariana Grande"
	wait(119.901)
	l__Songs__1.getonyourknees.Playing = false
	
end

I have a folder in the workspace called songs, and that’s where the script where I’ve just mentioned is located in that folder, and I don’t want it local cause I want everyone in that server to hear that song that is playing in the server, and whenever a person leaves, or joins they hear that same song everyone else is listening to.

2 Likes

Well, with a while true loop you do not need the parentheses on the true, see if that fixes your issue

If it doesn’t, please send an image of the output

3 Likes

It might be because you’re setting the .Playing property to true, instead of using the :Play() method. And instead of calling wait() for the duration of the song, you can use the Ended event.

while true do -- parenthesis around the "true" are unnecessary, and generally discouraged in Lua (although you can still keep them if you want)
	currentTrack.Value = "Sub Zero - Lil Uzi Vert"
	l__Songs__1.SubZero:Play()
	l__Songs__1.SubZero.Ended:Wait()
	task.wait(0.1)

	-- rest of the songs
end

I’m also just wondering why you’ve decided to name the l__Songs__1 variable so weirdly, any reasoning?

3 Likes

Idk sounds cool lol, and yeah…

4 Likes

Remember to mark a post as Solution if it solved your problem.

3 Likes

The reason this may be happening is because you’re using Sound.Playing instead of Sound:Play(), the function that’s meant to ‘start’ your audio track. You may also want to use the Sound.Ended event when the audio track ends, instead of waiting it’s TimeLength.
I would also suggest changing how your loop works to prevent errors that may arise.
Example:

local currentTrack = game.Workspace.Songs.CurrentTrack
local Tracks =
{
    {Name = "Sub Zero - Lil Uzi Vert", Song = l__Songs__l:WaitForChild("SubZero")};
    {Name = "Slow Motion - Trey Songz", Song = l__Songs__l:WaitForChild("SlowMotion")};
    {Name = "Just For The Night - Chris Brown, Migos", Song = l__Songs__l:WaitForChild("JustForTheNight")
    ...
}
while true do
    for _, Track in ipairs(Tracks) do
        local Song = currentTrack.Song
        currentTrack.Value = Track.Name
        -- Play the song, then wait until it ends
        Song:Play()
        Song.Ended:Wait()
    end
    wait(.1)
end

I would suggest changing around how it works and even optimizing it (since it’s a rough example, there’s plenty of optimizations you can make).

1 Like

Can you go to my other post too I need help with animations the GUI, but thank you I’ll try this, and I wanna use wait cause I don’t like sound.ended some audios include extra stuff.

1 Like
local MusicTracks = {} -- // Tracks go here

coroutine.wrap(function()
	while true do
		local RandomIndex = math.random(1, #MusicTracks)
		local MusicTrack = MusicTracks[RandomIndex]
		
		Music.TimePosition = 0
		Music.SoundId = "rbxassetid://" .. MusicTrack
		Music:Play()

		Music.Ended:Wait()
	end
end)()

This is my approach to how I usually do music players. If you want the music to play in an specific order then go through each track one by one, like this.

local MusicTracks = {} -- // Tracks go here

coroutine.wrap(function()
    local LastIndex = 0

    while true do
        if LastIndex == #MusicTracks then
            LastIndex = 0
        end

        LastIndex += 1

        local MusicTrack = MusicTracks[LastIndex]	
        Music.TimePosition = 0
        Music.SoundId = "rbxassetid://" .. MusicTrack
        Music:Play()

        Music.Ended:Wait()
    end
end)()
2 Likes

but I want everything listed so I’d rather do wait like 200 seconds or 199 cause I can shorten the song, I’d like it randomize that’s cool but- I want it to wait a certain time, and not music.ended

1 Like

I see, well this it’s still pretty easy to code in that feature. (along with the song names)
(quick tip - You can have tables inside of tables)

example 1 - randomized tracks

local Music = Instance.new("Sound")
Music.Name = "ServerMusic"
Music.Parent = workspace


--[[
	Set the track's wait time to nil if you want the full track to play
]]


local MusicTracks = {
	[1] = {
		Name = "Happy Adventure Music";
		Id = "196131899";
		WaitTime = 60 * 2;
	}
}


coroutine.wrap(function()
	while true do
		local RandomIndex = math.random(1, #MusicTracks)
		local MusicTrackData = MusicTracks[RandomIndex]

		Music.TimePosition = 0
		Music.SoundId = "rbxassetid://" .. MusicTrackData.Id
		Music:Play()
		
		print(MusicTrackData.Name)
		
		
		if MusicTrackData.WaitTime then
			task.wait(MusicTrackData.WaitTime)
		else
			Music.Ended:Wait()
		end
	end
end)()

example 2 - sorted tracks

local Music = Instance.new("Sound")
Music.Name = "ServerMusic"
Music.Parent = workspace


--[[
	Set the track's wait time to nil if you want the full track to play
]]


local MusicTracks = {
	[1] = {
		Name = "Happy Adventure Music";
		Id = "196131899";
		WaitTime = 60 * 2;
	}
}



coroutine.wrap(function()
	local LastIndex = 0

	while true do
		if LastIndex == #MusicTracks then
			LastIndex = 0
		end

		LastIndex += 1

		local MusicTrackData = MusicTracks[LastIndex]

		Music.TimePosition = 0
		Music.SoundId = "rbxassetid://" .. MusicTrackData.Id
		Music:Play()

		print(MusicTrackData.Name)


		if MusicTrackData.WaitTime then
			task.wait(MusicTrackData.WaitTime)
		else
			Music.Ended:Wait()
		end
	end
end)()
1 Like

k I’m using example 2 but how would I also make it so everytime the song changes, there’s a string value that changes named "currentTrack in a folder called songs in workspace, and it changes the string value to the name of the song in workspace.

1 Like

If I am reading this correctly you’re asking how to change a string’s value to the song name.
Which if that IS the case then…

local StringValue = nil -- // Change this to the specified instance
local Music = Instance.new("Sound")
Music.Name = "ServerMusic"
Music.Parent = workspace


--[[
	Set the track's wait time to nil if you want the full track to play
]]


local MusicTracks = {
	[1] = {
		Name = "Happy Adventure Music";
		Id = "196131899";
		WaitTime = 60 * 2;
	}
}


coroutine.wrap(function()
	local LastIndex = 0

	while true do
		if LastIndex == #MusicTracks then
			LastIndex = 0
		end

		LastIndex += 1

		local MusicTrackData = MusicTracks[LastIndex]

		Music.TimePosition = 0
		Music.SoundId = "rbxassetid://" .. MusicTrackData.Id
		Music:Play()

		if StringValue then
		    StringValue.Value = MusicTrackData.Name
		end


		if MusicTrackData.WaitTime then
			task.wait(MusicTrackData.WaitTime)
		else
			Music.Ended:Wait()
		end
	end
end)()
1 Like

alright cool I got the string value working now I’m trynna list all my songs and I’m getting an error so how would I add another song and like more songs I wanna add to that list

Here’s the script:

local Music = Instance.new("Sound")
Music.Name = "ServerMusic"
Music.Parent = workspace
local currentTrack = workspace.Songs.CurrentTrack

--[[
	Set the track's wait time to nil if you want the full track to play
]]


local MusicTracks = {
	[1] = {
		Name = "Sub Zero";
		Id = "1576032985";
		WaitTime = 117,472 * 2;
		
		Name = "Jocelyn Flores";
		Id = "426082642";
		WaitTime = 120.084 * 2;
	}
}


coroutine.wrap(function()
	local LastIndex = 0

	while true do
		if LastIndex == #MusicTracks then
			LastIndex = 0
		end

		LastIndex += 1

		local MusicTrackData = MusicTracks[LastIndex]

		Music.TimePosition = 0
		Music.SoundId = "rbxassetid://" .. MusicTrackData.Id
		Music:Play()

		if currentTrack then
			currentTrack.Value = MusicTrackData.Name
		end


		if MusicTrackData.WaitTime then
			task.wait(MusicTrackData.WaitTime)
		else
			Music.Ended:Wait()
		end
	end
end)()```
1 Like
local rs = game:GetService("RunService")
local ss = game:GetService("SoundService")

local main = Instance.new("Sound", ss)

local songs = {
   { name = "impostor - sussy baka [MV]", id = "one hundred ninty nine"}
}

while true do
  for index, m in next, songs do
    print(("playing %"):format(m.name))
    main.SoundId = ("rbxassetid://%s"):format(m.id)
    main:Play()
    main.Ended:Wait() -- 
  end
  rs.Heartbeat:Wait()
end

i fixed the tables

1 Like

Can you help one last time on that and with the string value everytime the song changes with the name and string value is named currentTrack

1 Like
local Music = Instance.new("Sound")
Music.Name = "ServerMusic"
Music.Parent = workspace
local currentTrack = workspace.Songs.CurrentTrack

--[[
	Set the track's wait time to nil if you want the full track to play
]]

-- // [Index] not required due ot lua doing it automatically
local MusicTracks = {
	{
		Name = "Sub Zero";
		Id = "1576032985";
		WaitTime = 117.472 * 2;
	};
	
	{       Name = "Jocelyn Flores";
		Id = "426082642";
		WaitTime = 120.084 * 2;
	};
}


coroutine.wrap(function()
	local LastIndex = 0

	while true do
		if LastIndex == #MusicTracks then
			LastIndex = 0
		end

		LastIndex += 1

		local MusicTrackData = MusicTracks[LastIndex]

		Music.TimePosition = 0
		Music.SoundId = "rbxassetid://" .. MusicTrackData.Id
		Music:Play()

		if currentTrack then
			currentTrack.Value = MusicTrackData.Name
		end


		if MusicTrackData.WaitTime then
			task.wait(MusicTrackData.WaitTime)
		else
			Music.Ended:Wait()
		end
	end
end)()

You’re getting an error due to have two songs indexed into one

1 Like

this is probably a mistake, you added a comma not a period
so you pretty much made a new value in the table there

1 Like

didn’t even notice that from his previous code, plus it wouldn’t of had played the first song anyways due to the first table variables being overwritten

1 Like

So how do I add the song without every error, I want to list the songs with the wait time you’ve mentioned

1 Like

This was demonstrated in the last example code I showed.

1 Like