Why isn't my sound playing?

Im making a boombox gamepass but for some reason the audio wont play when you say in chat /music ID. I check the sound in studio and the id is in it but it just wont play. Thanks!

Code Snippet:

player.CharacterAdded:Connect(function(character)
	local boombox = game.ServerStorage.Boombox:Clone()
	boombox.Parent = character

	player.Chatted:Connect(function(message)
		if message:sub(1, soundCommand:len()):lower() == soundCommand:lower() then
			local soundID = message:sub(soundCommand:len() + 1)
			boombox.Handle.Sound.SoundId = "http://www.roblox.com/asset/?id="..soundID
			boombox.Handle.Sound:Play()
		end
	end)
end)
2 Likes

Hi, @ScriptedPoptartz.

player.CharacterAdded:Connect(function(character)
	local boombox = game.ServerStorage.Boombox:Clone()
	boombox.Parent = character

	player.Chatted:Connect(function(message)
		if message:sub(1, soundCommand:len()):lower() == soundCommand:lower() then
			local soundID = message:sub(soundCommand:len() + 1)
			boombox.Handle.Sound.SoundId = "http://www.roblox.com/asset/?id="..soundID
			boombox.Handle.Sound:Play()
		end
	end) -- this was missing
end)

A end) tag was missing for the player.Chatted connection.
Lemme know if more errors still occur?

2 Likes

yes, I forgot to add that in the code example so let me re-add that but thats not the issue.

2 Likes

Alright, does the soundCommand variable include the “/” or not? if it doesn’t make this line

this

if message:sub(2, soundCommand:len()):lower() == soundCommand:lower() then
2 Likes

yep here is full code for more context:

local MarketplaceService = game:GetService("MarketplaceService")
local soundCommand = "/music "
local gamepassId = 15075233

game.Players.PlayerAdded:Connect(function(player)
	if MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassId) then
		print("Player owns pass.")
		player.CharacterAdded:Connect(function(character)
			local boombox = game.ServerStorage.Boombox:Clone()
			boombox.Parent = character
			
			player.Chatted:Connect(function(message)
				if message:sub(1, soundCommand:len()):lower() == soundCommand:lower() then
					local soundID = message:sub(soundCommand:len() + 1)
					boombox.Handle.Sound.SoundId = "http://www.roblox.com/asset/?id="..soundID
					boombox.Handle.Sound:Play()
				end
			end)
		end)
	else
		print("Player does not own pass.")
	end
end)
3 Likes

When I tested, I didn’t include any of the gamepass code (for obvious reasons). It gave no errors and played the sound effect I chose.

Could it be something to do with the gamepass section of code?

2 Likes

But how is that? The boombox clones to the player, why else isent the sound playing? Also for some reason now the ID isent going into the boombox.

2 Likes

I’ve had no issue with the code whatsoever, the only problem I can see is that maybe the Sound.Volume is too low? I tested and my “Boombox” was a floating part so I set the .Volume to 10 for testing (even after the Boombox is parented under my Character) and it still worked.

edit:
Also, do you get any errors whatsoever?

second edit:
I would possibly consider allowing access to Studio API services as well (this includes DataStores). I’m not sure if that would make a difference.
Also try the code without the if statement checking for the gamepass and see if it works that way too.

Source: API and save game on studio

2 Likes

Try to add more print() to let us see that the script works.

local MarketplaceService = game:GetService("MarketplaceService")
local soundCommand = "/music "
local gamepassId = 15075233

game.Players.PlayerAdded:Connect(function(player)
	print("Player added.")
	if MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassId) then
		print("Player owns pass.")
		player.CharacterAdded:Connect(function(character)
			print("Player's character added.")
			local boombox = game.ServerStorage.Boombox:Clone()
			boombox.Parent = character

			player.Chatted:Connect(function(message)
				print("Player chatted.")
				if message:sub(1, soundCommand:len()):lower() == soundCommand:lower() then
					print("The player has just said the command.")
					local soundID = message:sub(soundCommand:len() + 1)
					boombox.Handle.Sound.SoundId = "http://www.roblox.com/asset/?id="..soundID
					boombox.Handle.Sound:Play()
				end
			end)
		end)
	else
		print("Player does not own pass.")
	end
end)
2 Likes

Im pretty sure if you want to play sounds in the client, you need a local script.

repeat wait() until game:IsLoaded() -- So nothing crashes, doesn't work in ServerScripts, and not even needed for those. 

local player = game:GetService("Players").LocalPlayer

local boombox = function()
	local boombox = game.ServerStorage.Boombox:Clone()
	boombox.Parent = character

	player.Chatted:Connect(function(message)
		if message:sub(1, soundCommand:len()):lower() == soundCommand:lower() then
			local soundID = message:sub(soundCommand:len() + 1)
			boombox.Handle.Sound.SoundId = "http://www.roblox.com/asset/?id="..soundID
			boombox.Handle.Sound:Play()
		end
	end)
end)

boombox()
  1. Make this script a local script, and put it in PlayerScripts.
  2. Put the boombox model in ReplicatedStorage, for LocalScripts can’t access ServerStorage.

Let me know if this helps. Thanks, WE

3 Likes

sound is at one, and everything is on since I have leaderstats

1 Like

nope, I want it to play on server so everything should work.

1 Like

It works it just doesent play or anything.

1 Like

Since the Boombox is parented to the Character (a child of the workspace) it would be server-sided.

Can you be a little more descriptive like where it doesn’t work?

  • So you get the print output: “Player owns pass”
  • The Boombox is properly cloned onto the player
  • The Boombox has a Handle with a Sound child on it
  • When you try the command, does it put a SoundId onto the Handle.Sound.SoundId?

Also, have you tried with a different sound or with multiple sounds? I tried with a handful and all of them worked fine.

Yes, most of the time the sound ID goes into the sound in handle. Can you show me the code you used?

2 Likes

I have tried @55167233gf prints and it all seems to working, I added a print(“playing”) after the audio is played and and runs threw but theres still no audio being played.

2 Likes

For sure, I made a file you can download that has the functional code, a boombox, etc. You can play test with a 1 person server to see the results.

BoomboxTest.rbxl (23.4 KB)

As for the code, it is included with the file, but I will also put it here for anyone who doesn’t want to download the entire project.

-- The SoundId I used: 323899165
--local MarketplaceService = game:GetService("MarketplaceService")
local soundCommand = "/music "
--local gamepassId = 15075233

game.Players.PlayerAdded:Connect(function(player)
	--if MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassId) then
		print("Player owns pass.")
		player.CharacterAdded:Connect(function(character)
			local boombox = game.ServerStorage:WaitForChild("Boombox"):Clone()
			boombox.Parent = character

			player.Chatted:Connect(function(message)
				if message:sub(1, soundCommand:len()):lower() == soundCommand:lower() then
					local soundID = message:sub(soundCommand:len() + 1)
					boombox.Handle.Sound.SoundId = "http://www.roblox.com/asset/?id="..soundID
					boombox.Handle.Sound:Play()
				end
			end)
		end)
	--[[else
		print("Player does not own pass.")
	end--]]
end)

Of course, I have the Gamepass & MarketplaceService disabled because it wouldn’t work for me otherwise. Your’s should work since you own the Gamepass. :slight_smile:

Also, the Sound I used is quite loud so be aware of that. :laughing:

2 Likes

I am really confused, I tried the code for me and I still can here sound, Ill try out the game download you replied as well.

1 Like

okay so, I have tried you game and it works, I really don’t know why it doesn’t work in my games though, maybe it has something do to the boombox I’m using? idk lol

1 Like

BTW: when you are testing, is:

  • Your computer volume not muted (yes I know it is a dumb question just making sure)
  • Your ROBLOX Studio sound under the volume mixer is not at 0 (Right click Sound Icon on your task bar – bottom right – and then click “Open Volume Mixer” and look for studio while the application is open).
  • When you’re testing, the view window where the game is highlighted.
  • The mute button (under Studio: Test tab->Mute) isn’t selected.

Also, just seen your post as I was typing this:

I’m not sure, how was your Boombox setup? Was it an Accessory with a Handle part like mine?

Also, you are free to the Boombox in the project if it works for you. I didn’t make the mesh, I just made the Accessory.

1 Like