Music script not working

Hello Developers!

I have been trying to fix this for a while, but I can’t find out why it is not working.

Here is the full script:

local Playing = true

local Music = {
	{'rbxassetid://1840202507', 1, 'Drifting Star'},
	{'rbxassetid://187918023', 2, 'Relaxing Music... :)'}
}



script.Parent.PlayButton.MouseButton1Click:Connect(function()
	print(tostring(Playing))
	if Playing == false then
		Playing = true
		script.Parent.PlayButton.Image = 'rbxassetid://3192517628'
		script.Parent.Music:Play()
	elseif Playing == true then
		Playing = false
		script.Parent.PlayButton.Image = 'rbxassetid://8215093320'
		script.Parent.Music:Stop()
	end
end)

local Song = math.random(1, #Music)

while wait(0.5) do
	pcall(function()
		if script.Parent.Music.SoundId ~= Music[Song][1] then
			script.Parent.Music.SoundId = Music[Song][1]
			script.Parent.Music.Volume = Music[Song][2]
			script.Parent.Music:Play()
		end
		if script.Parent.Music.TimePosition >= script.Parent.Music.TimeLength then
			if Song > #Music then
				Song = 1
			else
				Song = Song + 1
			end
		end		
		script.Parent.SongName.Text = tostring(Music[Song][3])
		script.Parent.Length.Text = tostring(script.Parent.Music.TimePosition)..' / '..tostring(script.Parent.Music.TimeLength)
	end)
end

Here is the line that is not working:

script.Parent.Length.Text = tostring(script.Parent.Music.TimePosition)..' / '..tostring(script.Parent.Music.TimeLength)

When I go to the Gui, It is just “0 / 0”, even though that if I check the Sound, it is playing and loaded.

Can you see what’s wrong, thanks.

Edit: Here is the Explorer:

image

1 Like

You’re playing the sound instance every 0.5 seconds.

1 Like

How am I? This text will be blurred

while wait(0.5) do
	pcall(function()
		if script.Parent.Music.SoundId ~= Music[Song][1] then
			script.Parent.Music.SoundId = Music[Song][1]
			script.Parent.Music.Volume = Music[Song][2]
			script.Parent.Music:Play()
1 Like

That only happens one time. This text will be blurred

Anything inside that while loop will be executed every 0.5 seconds, since the loop isn’t broken out of and there is no yielding code inside the loop.

2 Likes

I know that, the loop keeps going until the song is done.

while wait(0.5) do
	pcall(function()
		if script.Parent.Music.SoundId ~= Music[Song][1] then

The if only runs one time because in the if, it sets it to the same thing, so It only runs one time.

2 Likes

Ok, I will just remove it. Thanks anyways.

local Frame = script.Parent
local Button = Frame.PlayButton
local Music = Frame.Music
local SongName = Frame.SongName
local Length = Frame.Length

local Musics = {
	{'rbxassetid://1840202507', 'Drifting Star'},
	{'rbxassetid://187918023', 'Relaxing Music... :)'}
}
local Playing = false



Button.MouseButton1Click:Connect(function()
	if not Playing then
		Playing = true
		Button.Image = 'rbxassetid://3192517628'
		Music:Play()
	elseif Playing then
		Playing = false
		Button.Image = 'rbxassetid://8215093320'
		Music:Stop()
	end
end)

task.spawn(function()
	while task.wait() do
		local Song = math.random(1, #Musics)
		SongName.Text = tostring(Musics[Song][2])
		Music.SoundId = Music[Song][1]
		if not Music.IsLoaded then
			Music.Loaded:Wait()
		end
		Music:Play()
		task.spawn(function()
			while Music.TimePosition < Music.TimeLength do
				task.wait()
				Length.Text = tostring(Music.TimePosition)..' / '..tostring(Music.TimeLength)
			end
		end)
		Music.Ended:Wait()
	end
end)
1 Like