Why won't this get the value i'm trying to get and play the music audio?

Alright so i’m trying to play this audio while having this scrollingframe GUI get the value from the ScrollingFrame Label which gets named as it forms on the list.

For some reason, it won’t play the music no matter what I try.

I looked on the Developer Wiki and saw nothing on this situation and I looked into it and I see no way to fix it.

Here’s the button.

--Made By MillerrIAm
-------------------Variables------------------
Event = game.ReplicatedStorage.GUIEvents.PlayMusic
------------------Main Script------------------
script.Parent.MouseButton1Click:Connect(function()
	Event:FireServer("Yes",script.Parent.Text)
end)

Here’s the script that controls the event.

--Made By MillerrIAm
-------------------Variables------------------
Music = game:GetService("ServerStorage").MusicFolder
Event = game.ReplicatedStorage.GUIEvents.PlayMusic
------------------Main Script------------------
Event.OnServerEvent:Connect(function(plr,Play,argument)
	if Play == "Yes" then
		Music[""..argument].Volume = 1
		Music[""..argument]:Play()
	elseif Play == "No" then
		Music[argument]:Stop()
	end
end)

Sorry, I did not understand what you said. Could you please explain the first part in more detail?

What I mean by this is I’m attempting to make it so when I click this GUI button, it fires an event that gets the value to play it while also getting the name it is to search for and play.
Like this…
When the event is fired…
It gets the name of the audio inside the argument then gets the value to play yes.

1 Like

Well, right off the bat there are a few problems.

You should do some checks to see if the value argument exists, since exploiters can fire the remote event and make argument nil, causing your script to error. I also see that you’re doing “”…argument, which should just be turned into argument.
Anyway, I think the reason your music won’t play is because you’re using a colon instead of a period to set the volume.

It should be Music[argument].Volume = 1

That didn’t fix it sadly, do you have any other suggestions?

Are you able to check your output for any errors or warnings?

Yeah I did, no errors or warnings

It’s because your Music folder is stored in ServerStorage. Sounds only play if parented in the workspace or SoundService.

That didn’t fix it either sadly. I have no idea why it isn’t working.

Can you give me any updated code?

--Made By MillerrIAm
-------------------Variables------------------
Music = game:GetService("SoundService").MusicFolder
Event = game.ReplicatedStorage.GUIEvents.PlayMusic
------------------Main Script------------------
Event.OnServerEvent:Connect(function(plr,Play,argument)
	if Play == "Yes" then
		Music[argument].Volume = 1
		Music[argument]:Play()
	elseif Play == "No" then
		Music[argument]:Stop()
	end
end)
--Made By MillerrIAm
-------------------Variables------------------
Event = game.ReplicatedStorage.GUIEvents.PlayMusic
------------------Main Script------------------
script.Parent.MouseButton1Click:Connect(function()
	Event:FireServer("Yes",script.Parent.Text)
end)

I’ve rewritten your code so that it’s cleaner and hopefully works.
There are also test prints in case nothing happens.

Server:

--Made By MillerrIAm
-------------------Variables------------------
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")

local Music = SoundService:WaitForChild("MusicFolder")

local GUIEvents = ReplicatedStorage:WaitForChild("GUIEvents")
local MusicEvent = GUIEvents:WaitForChild("PlayMusic")
------------------Main Script------------------
MusicEvent.OnServerEvent:Connect(function(_, PlayType, Arg)
	local MusicArg = Music[Arg]
	
	if MusicArg then
		if PlayType == true then
			MusicArg.Volume = 1
			warn("Played")
			MusicArg:Play()
		elseif PlayType == false then
			MusicArg:Stop()
		end
		return
	end
	warn(string.format("Didn't play. MusicArg: %s Arg: %s", MusicArg, Arg))
end)

Client:

--Made By MillerrIAm
-------------------Variables------------------
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local GUIEvents = ReplicatedStorage:WaitForChild("GUIEvents")
local MusicEvent = GUIEvents:WaitForChild("PlayMusic")
------------------Main Script------------------
script.Parent.Activated:Connect(function()
	MusicEvent:FireServer(true, script.Parent.Text)
	warn("Fired")
end)

Alright so, this is all working perfectly except it isn’t getting the music name hence it not playing anything. What else would you suggest I do to fix that part?

I know why.
It’s because the music folder is an instance, not a table.
You are supposed to use FindFirstChild.
Fixed code:

--Made By MillerrIAm
-------------------Variables------------------
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")

local Music = SoundService:WaitForChild("MusicFolder")

local GUIEvents = ReplicatedStorage:WaitForChild("GUIEvents")
local MusicEvent = GUIEvents:WaitForChild("PlayMusic")
------------------Main Script------------------
MusicEvent.OnServerEvent:Connect(function(_, PlayType, Arg)
	local MusicArg = Music:FindFirstChild(Arg)
	
	if MusicArg then
		if PlayType == true then
			MusicArg.Volume = 1
			warn("Played")
			MusicArg:Play()
		elseif PlayType == false then
			MusicArg:Stop()
		end
		return
	end
	warn(string.format("Didn't play. MusicArg: %s Arg: %s", MusicArg, Arg))
end)

Apologies but that didn’t fix it. Any other suggestions? If not, I understand.

Well, I think it’s most likely due to the text not lining up with the song that’s supposed to be played. Therefore, there is not a song being found and it erroring.
Make sure there are no typos in either the song name or the text.

1 Like

Pretty sure capitalization is very important and matching up the string, otherwise you cannot match it correctly, therefore you should work around it somehow.

Oh yeah. Now I realized that game:GetService("ServerStorage").MusicFolder. Do not play audios placed in ServerStorage, as they are unable to replicate to client. Try playing the audios from a different service. I don’t think the way I played audios was using SoundService.

Try moving MusicFolder into Workspace and playing them from there instead.

3 Likes