Hello i have a problem with the script

Hey someone made a script for me and it works but there is a little problem when the player enters the game and the first song ends there is no sound starting to play

here is the script

local musicfolder = game.Workspace:WaitForChild("songs") -- place a folder into workspace. Add three UNLOOPED sounds into the folder named "sound1", "sound2", and "sound3". 
-- Set only sound1's isPlaying property to true

local songnumber = musicfolder.song -- insert an intvalue into the folder called "song". Set the value to 1
local button = script.Parent
local songvolume = 0.5 -- replace with the volume you'd like the song to be
local MuteImage, UnMuteImage = "rbxassetid://14337097790", "rbxassetid://14337008762"
local songmuted = false
local lastsongnumber = 10 -- REMEMBER, EVERYTIME YOU ADD A NEW SONG, NAME THE SONG sound4, sound5, etc., THEN UPDATE THIS NUMBER TO WHATEVER THE LAST SONG's NUMBER IS!!!

local function PreLoadMuteImages() 
	game:GetService("ContentProvider"):PreloadAsync({ MuteImage, UnMuteImage })
	print("Done Loading")
end
coroutine.wrap(PreLoadMuteImages)()
local function mute()
	print("attempt to toggle mute")
	for i, v in pairs(musicfolder:GetChildren()) do
		if v:IsA("Sound") then
			if v.Volume ~= 0 then
				songmuted = false
			end
		end
	end
	if songmuted == false then
		for i, v in pairs(musicfolder:GetChildren()) do
			if v:IsA("Sound") then
				v.Volume = 0
			end
		end
		button.Image = MuteImage
		songmuted = true
		print("muted")
	else
		for i, v in pairs(musicfolder:GetChildren()) do
			if v:IsA("Sound") then
				if v.Volume == 0 then
					v.Volume = songvolume
					songmuted = false
				end
			end
		end
		button.Image = UnMuteImage
		songmuted = true
		print("unmuted")
	end
end


local function changeSong()
	if musicfolder:FindFirstChild("sound"..songnumber.Value) and songnumber.Value ~= lastsongnumber then
		musicfolder:FindFirstChild("sound"..songnumber.Value+1):Play()
		songnumber.Value += 1
		print("next song: song "..songnumber.Value)
	else
		musicfolder:FindFirstChild("sound1"):Play()
		songnumber.Value = 1
		print("reached the end, restarting")
	end
end


button.MouseButton1Down:Connect(mute)


for i, v in musicfolder:GetChildren() do 
	if v:IsA("Sound") then 
		v.Ended:Connect(changeSong)
	end
end

my disco if you want to help me Dwalen

3 Likes

Is this a server-side music player or a client-side music player?

I think the problem is the script not properly starting the next song after the first one ends.
One possible solution is to use the “Ended” event of the Sound object to detect when a song has ended, and then play the next song in the folder.

Here try this

local songnumber = musicfolder.song
local button = script.Parent
local songvolume = 0.5
local MuteImage, UnMuteImage = "rbxassetid://14337097790", "rbxassetid://14337008762"
local songmuted = false
local lastsongnumber = 10

local function PreLoadMuteImages()
	game:GetService("ContentProvider"):PreloadAsync({ MuteImage, UnMuteImage })
	print("Done Loading")
end
coroutine.wrap(PreLoadMuteImages)()

local function mute()
	print("attempt to toggle mute")
	for i, v in pairs(musicfolder:GetChildren()) do
		if v:IsA("Sound") then
			if v.Volume ~= 0 then
				songmuted = false
			end
		end
	end
	if songmuted == false then
		for i, v in pairs(musicfolder:GetChildren()) do
			if v:IsA("Sound") then
				v.Volume = 0
			end
		end
		button.Image = MuteImage
		songmuted = true
		print("muted")
	else
		for i, v in pairs(musicfolder:GetChildren()) do
			if v:IsA("Sound") then
				if v.Volume == 0 then
					v.Volume = songvolume
					songmuted = false
				end
			end
		end
		button.Image = UnMuteImage
		songmuted = true
		print("unmuted")
	end
end

local function playNextSong()
	local currentSong = musicfolder:FindFirstChild("sound" .. tostring(songnumber.Value))
	if currentSong then
		currentSong:Play()
		currentSong.Ended:Connect(function()
			songnumber.Value = (songnumber.Value % lastsongnumber) + 1
			playNextSong()
		end)
	end
end

playNextSong()

It’ll play the next song in the folder when the previous song ends. Also make sure to update the lastsongnumber variable when you add the new songs to the folder.

For your music list (assuming it’s client-side) use this

  1. Make a Folder in “ReplecatedStorage” and call it “Music”
  2. Add a “LocalScript” in StarterPlayerScripts.
  3. Paste This code into the LocalScript:

local music = game.ReplicatedStorage.Music. --Put your music here

while true do
music:Play()
music.Ended:Wait()
wait(3) – Wait until the next song plays
end

You can add more music just make another variable and continue the

Music:Play()
Music.Ended:Wait()

but replace music with your new variable.

I’ll look into the mute part of it.

Remember this part:

What is the issue? Include screenshots / videos if possible! Try to be clear, give as much information about the issue.

These may seem such trivial pieces of information for you to provide that it wasn’t worth the effort of you fingers to type it out. But it changes everything when people are trying to help you.

Are you running this in a LocalScript or a Script?
Where is this script executing from?
Is this script a single instance or is it replicated to each client?

La di da…

I put the script in local script so it looks good to me

here is the output

it doesn’t work, and when i click on this image sound it nothing that happening