Music Playing script not working anymore, multiple errors

Hi!

Recently I changed my music script to offer more, and add new UI.

I now have some problems with it.

First, when it DOES work, the little popup says the name of the last song played.

Second, it seems to not play the next some. Never, only when I make the attribute false, then true again will it start playing.

Also, probably because of the not playing issue or out of date track, the UI does not update anymore.

And lastly, the mute button does not work.

Code:

local function Play()
	if not Player:GetAttribute("PlayMusic") then return end 
	if Player:GetAttribute("PlayMusic") == false then return end
	if PlayingTrack then PlayingTrack:Stop() end
	randomTrack = availableMusic[math.random(1,#availableMusic)]
	if SecretChance == 472793 then
		local availablesecrets = game.ReplicatedStorage.SecretSongs:GetChildren()
		local randsecret = availablesecrets[math.random(1,#availablesecrets)]
		randomTrack = randsecret
	end
	Player:SetAttribute("CurrentTrack",randomTrack.Name)
--	if randomTrack.Name == "SecretSongs" then randomTrack = availableMusic[math.random(1,#availableMusic)] return end
	PlayingTrack = randomTrack
	PlayingTrack.Volume = NextVolume
	randomTrack:Play() 
	playing = true
	PlayingSong = PlayingTrack
	--print ("Playing", randomTrack.Name)
	SecretChance = math.random(1,999999)
	while playing == true do
		PlayingUI.Parent.ea.time.Text = math.round(PlayingTrack.TimePosition).."/"..math.round(PlayingTrack.TimeLength)
		PlayingUI.time.Text = math.round(PlayingTrack.TimePosition).."/"..math.round(PlayingTrack.TimeLength)
		task.wait()
	end
end
Player:GetAttributeChangedSignal("PlayMusic"):Connect(function()
	Play()
end)
local function ToggleMusic()
	if Player:GetAttribute("PlayMusic") then
		Player:SetAttribute("PlayMusic",false)
	else
		Player:SetAttribute("PlayMusic",true)
	end
end
repeat task.wait() until randomTrack ~= nil
randomTrack.Ended:Connect(function()
	PlayingUI.Parent.ea:TweenPosition(UDim2.new(-0.009, 0, .723, 0), "In", "Quint", 1.5, true)
	PlayingUI.Parent.ea.name.Text = randomTrack.Name
	Play()
	PlayingUI.Parent.ea.name.Text = randomTrack.Name
	task.wait(5)
	PlayingUI.Parent.ea:TweenPosition(UDim2.new(-.233, 0, .723, 0), "In", "Quart", 1, true)
end)
PlayingUI.Parent.ea.OpenHitbox.MouseEnter:Connect(function()
	PlayingUI.Parent.ea:TweenPosition(UDim2.new(-0.111, 0, .723, 0), "Out", "Quart", 1, true)
end)
PlayingUI.Parent.ea.OpenHitbox.MouseLeave:Connect(function()
	PlayingUI.Parent.ea:TweenPosition(UDim2.new(-.233, 0, .723, 0), "In", "Quart", 1, true)
end)
PlayingUI.Parent.ea.OpenHitbox.MouseButton1Click:Connect(function()
	PlayingUI.Parent.ea:TweenPosition(UDim2.new(-.233, 0, .723, 0), "In", "Quart", 1, true)
	PlayingUI:TweenPosition(UDim2.new(0.292, 0, 0.238, 0), "In", "Quart", 1, true)
end)
PlayingUI.TextButton.MouseButton1Click:Connect(function()
	PlayingUI:TweenPosition(UDim2.new(-0.5, 0, 0.238, 0), "In", "Quart", 1, true)
end)

local function MuteMusic(bool)
	PlayingTrack.Volume = bool and 0 or DefaultVol
	if PlayingTrack.Volume == 0 then NextVolume = 0 elseif PlayingTrack.Volume == DefaultVol then NextVolume = DefaultVol elseif PlayingTrack.Volume == bool then NextVolume = bool end
end
local function SkipMusic()
	if not Player:GetAttribute("PlayMusic") then 
		return 
	end
	PlayingTrack.TimePosition = PlayingTrack.TimeLength
	task.wait(5)
	PlayingUI.Parent.ea:TweenPosition(UDim2.new(-.233, 0, .723, 0), "Out", "Quint", 1.5, true) --{-0.233, 0},{0.723, 0}
		print ("Skipped!")
		task.wait(0.5)
end
--Play()
local db = false
Player:SetAttribute("CurrentTrack","...")
Player:SetAttribute("PlayMusic",false)
MuteBTN.MouseButton1Click:Connect(function()
	MuteMusic(PlayingTrack.Volume == DefaultVol)
end)

SkipBTN.MouseButton1Click:Connect(function()
	SkipMusic()
	task.wait(1)
end)

PlayingUI.Pause.MouseButton1Click:Connect(function()
	if PlayingSong.Playing == true then
		PlayingSong:Pause()
	else
		PlayingSong:Resume()
	end
end)

Player:GetAttributeChangedSignal("Volume"):Connect(function()
	PlayingTrack.Volume = Player:GetAttribute("Volume").Value
	MusicVol = Player:GetAttribute("Volume").Value
end)

Thanks for any help!

VIDEO:

Did you mean song?

What does this mean?

Is the issue with the song not playing which is causing the text to not update, could you explain more on what the issue is please

Hi, there is not much context to go off here.

  • Is there any way, that you could explain exactly how you want this to work?

I was writing this post on my phone very quickly, let me give some more context.

So, the little popup displays the last song played, if even. It doesn’t show the song that’s now playing.

The way it should work is play a random song, updating all of the UI with the buttons.

What I added to break it is the popup UI, the timer, and the play/pause button. That seemed to break it for some reason.
Now, it plays one song, and then instantly stops. No errors, and the buttons still work, besides the next button, that doesn’t work whatsoever.

The only way I can get it to play the next song is my toggling the attribute, I will attach a video soon.

If you need any more context lmk.

  • I personally cannot see any error on the side, that could be script-wise.
    But I can not be 100% sure since it is not my own code.

Here’s the video.

just a little warning is i cut out all of the variables on the code so people cant copy and paste it

  • Hi, again. Wouldn’t putting Player:SetAttribute(“PlayMusic”) to true and false make it play after a skip?

Yes, but I have skip code that worked, until I

Or simply just for when it ends?

I don’t see a place where it would continue itself to another song, maybe making a function that connects when the current song ends and runs the Play() function perhaps?

  • I completely missed it, I see it was between some other code… Which I can see clearly now.
    if Player:GetAttribute(“PlayMusic”) == false then return end ?
1 Like

Ok, so some more context.

If I do randomTrack.Ended:Connext(Play) , it works.

If I add the tweens and text, it exits.

My only other thought is randomTrack.Name is nil?

1 Like

I’d advise putting, PlayingUI.Parent.ea.name.Text = randomTrack.Name together with the play() function.
EDIT: PlayingUI.Parent.ea.name.Text = randomTrack.Name

That fixes the prompt, but I still have this and a non working skiup button.
image

I’m adding a bunch of prints, my suspicion (amogus) is that it’s having a silent error on some kind of function or line, making the skip function unreadable, and making the script to commit die.


I did this, I’m testing it right now.

Could you run it all under a pcall function and see if it prints any errors?

1 Like

image

I will try the pcall now, it did it all :man_shrugging:

I put local function yes() at the top of the code, running now.

Not gonna clutter with another reply, but i got this.

local s, error = pcall(function()
	-- code here;
end)

print(error)
  • I’ve done this in the past, but I doubt It will give much feedback. It’s worth a try :man_shrugging: