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

Just found a new bug when a player dies, a new “Sound” appears in the SoundService. This is most likely due to the local script being overwritten back into the player, since it is stored directly inside the player.

Possible options to combat overwriting:

  1. Save state on the server: save important data and game state on the server so that it is not lost when local scripts are overwritten.
  2. Use ReplicatedStorage: save shared scripts and modules in ReplicatedStorage so that they are accessible to both the server and the client.
  3. Handle events: use events to synchronize data between the client and the server. For example, you can try using RemoteEvent to pass data between the client and the server.
  4. Check state: when loading a local script, check the current game state and restore it if necessary.