2 Issues with Music Playing Script

Code:


local Player = game.Players.LocalPlayer
local availableMusic = game.ReplicatedStorage:WaitForChild("Music"):GetChildren() 
local PlayingTrack 
local PlayingUI = Player.PlayerGui:WaitForChild("NowPlaying").Frame
local SkipBTN = PlayingUI.skip
local MuteBTN = PlayingUI.Mute
local DefaultVol = 5
local Count = 0 
local DB = false
local function PlayNexTrack()
	if not Player:GetAttribute("PlayMusic") then return end
	local randomTrack = availableMusic[math.random(1,#availableMusic)]
	Player:SetAttribute("CurrentTrack",randomTrack.Name)
	PlayingTrack = randomTrack
	randomTrack:Play() 
	print ("Playing", randomTrack.Name)
	randomTrack.Ended:Connect(PlayNexTrack)
	
end

local function CheckForBug()
	for i = 1, #availableMusic do
		--issue 1
		--Kinda confused on what to do here. I want it to check if the song is playing.
		--If it is, add 1 to count. If count is 2, stop the one that isn't playing.
	end
end
local function ToggleMusic()
	if Player:GetAttribute("PlayMusic") then
		Player:SetAttribute("PlayMusic",false)
	else
		Player:SetAttribute("PlayMusic",true)
	end
end
local function MuteMusic(bool)
	PlayingTrack.Volume = bool and 0 or DefaultVol
	print (PlayingTrack.Volume)
end
local function SkipMusic()
	if not Player:GetAttribute("PlayMusic") then return end
	PlayingTrack.TimePosition = PlayingTrack.TimeLength

	print ("Skipped!")
	wait(0.5)
end
--PlayNexTrack()
Player:GetAttributeChangedSignal("PlayMusic"):Connect(function()
	if Player:GetAttribute("PlayMusic") then
		PlayNexTrack()
		
	else
		PlayingTrack.Playing = false
	end
end)
Player:GetAttributeChangedSignal("CurrentTrack"):Connect(function()
	PlayingUI.Playing.Text = Player:GetAttribute("CurrentTrack")
end)

Player:SetAttribute("CurrentTrack","...")
Player:SetAttribute("PlayMusic",true)
MuteBTN.MouseButton1Click:Connect(function()
	
	MuteMusic(PlayingTrack.Volume == DefaultVol)
end)

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

Player.MusicPlaying.Value.Changed:Connect(function()  --issue 2, i dont want it to play if MusicPlaying is false, but if it's true then play
														--it shouldn't set Playing to false after a song is done playing..
	if DB == false then
		DB = true
		
	end
end)

Hi!

Issue 1: Music randomly plays 2 at the same time
Video proof:


I don’t know what’s causing this.

Issue 2: How do I make it stop with a value in Player?
Very simple, what the name says. I need the music to pause using UI sometimes.

Thanks for any help!

New issue: After skipping again, it plays like 100 of them.

I’m unsure why you’re having this issue. Maybe you should pause all other track when playing a new song?

It only happens around the 4th-8th skip

Did you try putting this in your code?

local function PlayNexTrack()
	if not Player:GetAttribute("PlayMusic") then return end 
+  if playingTrack then PlayingTrack:Stop() end
	local randomTrack = availableMusic[math.random(1,#availableMusic)]
	Player:SetAttribute("CurrentTrack",randomTrack.Name)
	PlayingTrack = randomTrack
	randomTrack:Play() 
	print ("Playing", randomTrack.Name)
	randomTrack.Ended:Connect(PlayNexTrack)
	
end

It still does this.

Ah right. Change the :Pause() to a :Stop() in the code I gave you, mb.

Thanks, do you have an answer to problem 2?

Ideally, you shgouldn’t be creating a seperate property just for music playing. Use the Sound’s Playing property, and then you could easily pause it from other scripts.

Yes, but how?

Fre example, my build mode script.

availableMusic:FindFirstChild(Player:GetAttribute("CurrentTrack")):Pause()
1 Like

Doing it from my script now, is this correct?

local playing = game.Players.LocalPlayer:GetAttribute("MusicPlaying")
local sound = script.Parent.Parent.ImageLabel.Intro
local btn = script.Parent
local fade = script.Parent.Parent.Parent.fadeLoad
local plr = game.Players.LocalPlayer
playing:SetAttribute(false)

It’s a separate script, so I cant use availableMusic