Sound not playing

Everything works, but the sound just won’t play with some reason… Here is the script, thank you for reading!

local song1 = "rbxassetid://9046863253"
local song2 = "rbxassetid://9047105533"
local songtable = {song1, song2}

local sound = Instance.new("Sound")
sound.Archivable = true
sound.Parent = game.SoundService
while wait() do
	local random = songtable[math.random(1, #songtable)]
	sound.SoundId = random
	if not sound.IsLoaded then
		sound.Loaded:Wait()
	end
	repeat wait()
		sound:Play()
	until sound.Played == true
	wait(sound.TimeLength)
	sound:Stop()
end
1 Like

Try instancing the Sound object within the loop. Also, it seems like you’re making the script halt too often with the number of wait() calls you have.

This is what I propose.

local song1 = "rbxassetid://9046863253"
local song2 = "rbxassetid://9047105533"
local songtable = {song1, song2}

while wait() do
	local sound = Instance.new("Sound")
	sound.Archivable = true
	sound.Parent = game.SoundService
	local random = songtable[math.random(1, #songtable)]
	sound.SoundId = random
	sound:Play()
	wait(sound.TimeLength)
	sound:Destroy()
end

Notice how I’m creating a new Sound object with each iteration and then destroying it once the song has finished playing. I’ve also removed some of your unnecessary wait() calls to ensure that your code flows smoothly. I tried this out myself, and it seems to work.

1 Like

I could be wrong about this but I think that SoundService
can only be utilised from a local script… So you might have
to use a local script instead of a server script if you’re doing that.

1 Like

The reason it won’t play is because SoundService is a local service. You should make this a LocalScript.

1 Like