Music isn't playing when using Play()

I’ve been trying to figure out this bug to play music to the whole server by using play() and pop-up GUI that tells you who owns the song and name and length. Play() isn’t sending any errors but still not functioning.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local MG_1 = ReplicatedStorage.MG_1

musicTable =
	{
		"rbxassetid://4964811958",
		"rbxassetid://5467749866",  
		"rbxassetid://919461977", 
		"rbxassetid://3012232004",
		"rbxassetid://3486447707",
		"rbxassetid://745716722",
	}

local sound = Instance.new("Sound")
sound.Parent = game.SoundService 
sound.Volume = 1
sound.Name = "Music"

local function toHMS(s)
	return ("%02i:%02i:%02i"):format(s/60^2, s/60%60, s%60)
end

wait(5)

while true do
	for i = 1, #musicTable do
		sound.SoundId = musicTable[math.random(1,#musicTable)]
		
		game.SoundService:WaitForChild("Music"):Play()
		
		wait(3)
		
		local AssetSongId = sound.SoundId:match("%d+")

		local Asset = game:GetService("MarketplaceService"):GetProductInfo(AssetSongId)

		for i,v in pairs(game.Players:GetChildren()) do
			if v:IsA("Player") then
				v.PlayerGui.MusicGUI.Main.SongName.Text = Asset.Name

				v.PlayerGui.MusicGUI.Main.Publisher.Text = "Publisher: " .. Asset.Creator.Name

				v.PlayerGui.MusicGUI.Main.Length.Text = "Length: " .. toHMS(sound.TimeLength)

				v.PlayerGui.MusicGUI.Main.Requester.Text = "Requested By: [SERVER]"
			end
		end

		MG_1:FireAllClients() -- remote tweens
		

		wait(sound.TimeLength)
	end
end
1 Like

Try changing that line to “sound:Play()”

Move the audio from sound service to a folder in workspace and it will play

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MG_1 = ReplicatedStorage.MG_1


local musicTable =
	{
		"rbxassetid://4964811958",
		"rbxassetid://5467749866",  
		"rbxassetid://919461977", 
		"rbxassetid://3012232004",
		"rbxassetid://3486447707",
		"rbxassetid://745716722",
	}


local soundFolder = Instance.new("Folder")
soundFolder.Name = "SoundFolder"
soundFolder.Parent = workspace
local sound = Instance.new("Sound")
sound.Volume = 1
sound.Name = "Music"
sound.Parent = soundFolder 


local function toHMS(s)
	return ("%02i:%02i:%02i"):format(s/60^2, s/60%60, s%60)
end


wait(5) -- ?


while true do
	for i = 1, #musicTable do
		sound.SoundId = musicTable[math.random(1, #musicTable)]
		sound:Play()
		
		
		wait(3) -- ?
		
		
		local AssetSongId = sound.SoundId:match("%d+")
		local Asset = game:GetService("MarketplaceService"):GetProductInfo(AssetSongId)
		
		
		for i,v in pairs(game.Players:GetChildren()) do
			if v:IsA("Player") then
				v.PlayerGui.MusicGUI.Main.SongName.Text = Asset.Name
				v.PlayerGui.MusicGUI.Main.Publisher.Text = "Publisher: " .. Asset.Creator.Name
				v.PlayerGui.MusicGUI.Main.Length.Text = "Length: " .. toHMS(sound.TimeLength)
				v.PlayerGui.MusicGUI.Main.Requester.Text = "Requested By: [SERVER]"
			end
		end
		
		
		MG_1:FireAllClients() -- remote tweens


		sound.Ended:Wait()
	end
	wait()
end
2 Likes

For starters, you should not use a for loop on the players to modify a client GUI. You should use a remote event and fire all clients, and pass in the required parameters and handle the UI modifications on the client-side.

Since you already have a variable for your Sound declared, there is no need to do game.SoundService:WaitForChild("Music"):Play(). You can simply do sound:Play().

You don’t need to have a for loop inside your while loop for your music queue system to be working. You also should use sound.Ended:Wait(), rather than wait(sound.TimeLength).

Here’s a fixed version of your music system (server-code):

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MG_1 = ReplicatedStorage.MG_1

local musicTable =
	{
		"rbxassetid://4964811958",
		"rbxassetid://5467749866",  
		"rbxassetid://919461977", 
		"rbxassetid://3012232004",
		"rbxassetid://3486447707",
		"rbxassetid://745716722",
	}

local sound = Instance.new("Sound")
sound.Name = "Music"
sound.Parent = game:GetService("SoundService")

local function toHMS(s)
	return ("%02i:%02i:%02i"):format(s/60^2, s/60%60, s%60)
end

while true do
	sound.SoundId = musicTable[math.random(1,#musicTable)]

	if not sound.IsLoaded then
		sound.Loaded:Wait()
	end

	sound:Play()

	local AssetSongId = sound.SoundId:match("%d+")

	local Asset = game:GetService("MarketplaceService"):GetProductInfo(AssetSongId)

	MG_1:FireAllClients(Asset.Name, Asset.Creator.Name, toHMS(sound.TimeLength), "[SERVER]") -- remote tweens


	sound.Ended:Wait()
end

I would suggest checking out my community tutorial on a music player system I made a while ago, it explains all of this. :wink:

And yes, as @kylerzong mentioned, you should move the audio from the sound service to the Workspace.

2 Likes