Applying SoundID to multiple sounds in a script

Hey everyone, need some assistance:

I have a script where it’ll make music play from multiple parts with the same name. The issue is it only works on 1/4 parts, and it’s a different part each time. Here’s my script:

local MusicData = {1836009584,1841274055}


for i,v in pairs(workspace.speakers:GetChildren()) do
	if v.Name == "Speaker" then
		local s = v.MusicPart.Sound
		s:Stop()
		if not s.IsPlaying then
			local newSound = MusicData[math.random(1,#MusicData)]
			s.SoundId = 'rbxassetid://'..newSound
			s:Play()
			wait(1)
			wait((s.TimeLength)-0.00001)
		end
	end
end

image

As you can see, they are all the same
image

Any help is appreciated. Thanks!

1 Like

Now someone correct me if I’m wrong but what I believe is happening is that the “wait” is making the actual for do loop wait as well, maybe try putting the actual code into a spawn()

Like this

local MusicData = {1836009584,1841274055}


for i,v in pairs(workspace.speakers:GetChildren()) do
	if v.Name == "Speaker" then
		local s = v.MusicPart.Sound
		s:Stop()
		if not s.IsPlaying then
            spawn(function()
                local newSound = MusicData[math.random(1,#MusicData)]
                s.SoundId = 'rbxassetid://'..newSound
                s:Play()
                wait(1)
                wait((s.TimeLength)-0.00001)
            end)
		end
	end
end

I know a spawn() might be unethical, but it’ll get the job done.

Thank you so much!!! It worked. Now onto another problem… they all are uneven with eachother :uhh:

Uneven how-so? Like you don’t want them to have different songs?

1 Like

Yeah. How would I end up doing this?`

Put the newSound line that gets a random ID outside of the for do loop.