How to make a mute / unmute music?

Hello, a person has created a script which consists of when a player joins the game there is a music playing and after this music ends I would like another one to play but the problem with the script when a player joins the game and the first sound ends there is no music playing

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.2 -- replace with the volume you'd like the song to be
local MuteImage, UnMuteImage = "rbxassetid://14327123642", "rbxassetid://14327004664"
local songmuted = false
local lastsongnumber = #musicfolder:GetChildren() - 1 -- don't change this

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

I recommend this instead:

local actualSound = Instance.new("Sound",workspace)
actualSound.SoundId =  "rbxassetid://9125627017"
actualSound:Play()
local sounds = {9125627017} --insert your song ids here
local playing = 1
local muted = false

local function play(id)
	actualSound.SoundId = "rbxassetid://"..id
	actualSound:Play()
	actualSound.TimePosition = 0
end



actualSound.Ended:Connect(function()
	playing+=1
	if playing >= #sounds then
		playing = 1
	end
	play(sounds[playing])
end)

local function toggle()
	if muted then 
		actualSound.Volume = 1
		muted = false
	else
		actualSound.Volume = 0
		muted = true
	end
end

script.Parent.MouseButton1Down:Connect(toggle)