Problem With Music/playlistscript

ok so the script at first works fine but the problem is My Boolan value & Textbutton in my screengui
isint working to pause/resume the music

local SoundService = game:GetService("SoundService")

local tracks = {
	SoundService:WaitForChild("Gamemusic"),
	SoundService:WaitForChild("Gamemusic2"),
	SoundService:WaitForChild("Gamemusic3"),
	SoundService:WaitForChild("Gamemusic4")
}

local toggle = SoundService:WaitForChild("MusicToggle")

print("🔁 Music Controller is running. Toggle is:", toggle.Value)

while true do
	for _, track in ipairs(tracks) do
		local duration = track.TimeLength > 0 and track.TimeLength or 180
		local timeElapsed = 0

		track.TimePosition = 0
		track:Play()
		print("🎵 Now playing:", track.Name)

		while timeElapsed < duration do
			if toggle.Value then
				if track.IsPlaying then
					print("⏸️ Paused:", track.Name)
					track:Pause()
				end
			else
				if not track.IsPlaying then
					print("▶️ Resumed:", track.Name)
					track:Resume()
				end
			end

			task.wait(0.1)
			timeElapsed += 0.1
		end

		track:Stop()
	end
end

Idk why t is happening the Script is inside scriptservicescript btw

LocalScript
inside the screengui Textbutton

local button = script.Parent
local soundService = game:GetService("SoundService")
local toggle = soundService:WaitForChild("MusicToggle")
button.MouseButton1Click:Connect(function()
	toggle.Value = not toggle.Value
	button.Text = toggle.Value and "Music Off" or "Music On"
end)

button.Text = toggle.Value and "Music Off" or "Music On"

You’re saying the first script is in a server script? I just want to make sure you acknowledge that every player will hear the same music at the same time, by doing so.
If your intent is to have a client-only music player, then the solution is to place everything in a script.
Now, for your main problem:
I have no idea because your system is weird. What I’d want you to do is to remove all that weird stuff:
local duration = track.TimeLength > 0 and track.TimeLength or 180 - useless; just do local duration = track.TimeLength.

You have nested a while look inside of another while loop. It’s absolutely FOUL. I recommend, instead, something like this (if you really wanna keep the script on server side, that I strongly recommend you NOT to do):

local track = 1;
local playing: boolean = true;
local function ChangeMusic(): nil
   playing = false;
   task.wait();
   playing = true;
   track += 1;
end

local function PauseMusic(): nil
   playing = false;
   -- be sure not to make the song start over from in the while loop if you wish to use this, else it restarts from 0 every time
end

local function ContinueMusic(): nil
   playing = true;
end

toggle.Changed:Connect(function()
-- do whatever with your functions PauseMusic, ContinueMusic and ChangeMusic
end)

while playing do
   tracks[track]:Play();
   task.wait(track[track].TimeLength - track[track].TimePosition)
end

Probably something along these lines. Although, it’s a very trash system to do it and, I’ll say it now for the last time: it shouldn’t be making it this way. You should transfer everything locally and not nest multiple loops nested altogether.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.