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)
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.
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")