Problem with playing music from local script

I wrote my simple music playback local script, for some unknown reason it does not work. I do not understand what is the error.
Its location is Workspace>=SoundService

local player1=game:GetService("Players")
local collection=game:GetService("CollectionService")
local soundService=game:GetService("SoundService")
local tag="SpawnPlatform"

local TABLE_MUSIC={
	1848354536,
	1841647093,
}

local function playMusic()
	for _,soundId in ipairs(TABLE_MUSIC) do
		local sound=Instance.new("Sound")
		sound.Parent=soundService
		sound.SoundId="rbxassetid://" .. soundId
		
		if not sound.IsLoaded then
			sound.Loaded:Wait()
		end
			sound:Play()
			task.wait(sound.TimeLength)
			sound:Destroy()
		end
	end

player1.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		playMusic()
	end)
end)
1 Like

Try moving the local script to one of the following services:

  • StarterGui
  • StarterPlayer > StarterPlayerScripts
  • StarterPlayer > StarterCharacterScripts
  • and the one service that I can’t quite remember the name of, but it isn’t really ideal for such type of script because it is primarily used for tools

I don’t think local scripts can run in SoundService

If that doesn’t work, then try actually creating the sounds within studio (maybe place them into some kind of folder so it’s more organized), instead of creating them through the script.

The easier way to do this is just to add a folder in sound service with the sounds you want and do a While loop with a nested for loop to do

while Player do
For I, sound in pairs(Playlist:GetChildren()) do

    Sound:Play()
    Sound.Ended:Wait()

end

I already tried to do this, it didn’t work

Yes this is indeed the easiest way to do what I want, but I want my original script to work the way I want it to and understand why it doesn’t work

Try this when playing your sound.
SoundService:PlayLocalSound(Sound)

Thank you. Although it didn’t really help me. It turns out the problem was in the line “player1.PlayerAdded:Connect(function(player)”. I just removed it and everything worked, most likely the problem was in CharacterAdded, it just didn’t work. Here is the final script.

P.S. And yes, I recommend using SoundService:PlayLocalSound() in some cases.

local soundService=game:GetService("SoundService")

local TABLE_MUSIC={
	1848354536,
	1841647093,
}

local function playMusic()
	for _,soundId in ipairs(TABLE_MUSIC) do
		local sound=Instance.new("Sound")
		sound.SoundId="rbxassetid://" .. soundId
		sound.Parent=soundService
		
		if not sound.IsLoaded then
			sound.Loaded:Wait()
		end
			soundService:PlayLocalSound(sound)
			task.wait(sound.TimeLength)
			sound:Destroy()
		end
	end

--player1.PlayerAdded:Connect(function(player)
--	player.CharacterAdded:Connect(function(character)
--		playMusic()
--	end)
--end)

playMusic()
print("Success music")
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.