Sounds don't play (no errors)

Hi,
I did a script in button that play all sounds when button is clicked (and stop them) everything works but sounds don’t play help:

-- Script inside button in game.StarterGui.ScreenGui
local b = script.Parent
local Activated = false

b.MouseButton1Click:Connect(function()
	local allSounds = game.StarterPlayer.SoundsFolder:GetChildren()
	if Activated then
		Activated = false
		b.BackgroundColor3 = Color3.fromRGB(55, 177, 33)
		b.Text = "PLAY ALL SOUNDS"
		for _, v in pairs(allSounds) do
			v:Stop()
		end
	else
		Activated = true
		b.BackgroundColor3 = Color3.fromRGB(200, 0, 0)
		b.Text = "STOP"
		for _, v in pairs(allSounds) do
			v:Play()
		end
	end
end)

I think that this is a local script. Probably SoundsFolder is empty when the game is loading for a client. I would use repeat until loop to wait for certain number of children inside the folder before using GetChildren() function to get the folder’s content.

No, this is script and the function is activated when event go

I might be wrong but events related to clicking a GUI button never work for server scripts. Try using local script.

You can’t interface with gui on the server. Make a local script and put it in the button. Switch all the code out and try agian.

1 Like

Also try putting a print before the if statement to see if your mouse clicked connection even works.

I quicly made a GUI with a button. I put a server script inside the button. The event somehow worked. There are other potential issues (the folder is empty, sounds got removed, volume is set to 0 and more)

I came up with more ideas. Make sure that you can play any sounds at all (outside the Roblox game) You can also try cloning the sounds and setting their parent to Workspace. Try using these pieces of code inside the script to find the reason for the issue:
print(table.getn(allSounds)) -- expected output: a number highter than 0

for _, v in pairs(allSounds) do
	print(v.TimeLength) -- expected output: a number highter than 0
end

No. Color and Text change works, only sounds don’t work

I did a quick test and I found out that if a sound is an ancestor of StarterPlayer, the sound won’t play. I tested it by using a script and two indentical sounds in Workspace and StarterPlayer and the script didn’t print any errors:

wait(6)
print("Start")
workspace.Sound:Play() -- I heard this sound
wait(3)
print("Start")
game:GetService("StarterPlayer").Sound:Play() -- I did not hear this sound

So I need to use remote events? ok thanks

But the other sounds I used in StarterPlayer worked

I would recommend trying SoundService:PlayLocalSound(). This function plays a sound locally, regardless of where the sound is parented.

Your edited code:

local SoundService = game:GetService("SoundService")
local b = script.Parent
local Activated = false

b.MouseButton1Click:Connect(function()
	local allSounds = game.StarterPlayer.SoundsFolder:GetChildren()
	
	Activated = not Activated
	
	if Activated then
		b.BackgroundColor3 = Color3.fromRGB(200, 0, 0)
		b.Text = "STOP"
		
		for _, v in pairs(allSounds) do
			SoundService:PlayLocalSound(v)
		end
	else
		b.BackgroundColor3 = Color3.fromRGB(55, 177, 33)
		b.Text = "PLAY ALL SOUNDS"
		
		for _, v in pairs(allSounds) do
			v:Stop()
		end
	end
end)

Edit: I made your activate set itself opposite each time as opposed to setting each time in the if/else bit. Makes no difference, but it’s slightly more efficient. :grinning_face_with_smiling_eyes:

1 Like

Trying to index nil with PlayLocalSound line 15

Are you sure the sounds aren’t two mono tracks affected by error 46? Recently Roblox has disabled these from playing due to the fact they were used for copyright bypassing.

Check the output for the client as well.

Plays a Sound locally, meaning the sound will only be heard by the client calling this function, regardless of where it’s parented to.

The function requires only one parameter - sound to play. I’m 99% sure that PlayLocalSound() works only for local scripts.

1 Like

I incorrectly named one of my variables. That’s my bad. Try it now. Also, as Spider said, make sure it is in a local script.

There are certain folders and services in the studio that sounds are not playable in.