Spawn(function() not working as expected

Hi, I am sure the title was confusing, but I wasn’t quite sure what to say. I have a fairly simple issue I can’t figure out. I am trying to have multiple speakers play music at once. We’ve got about nearly 100 speakers and I am trying to factor in if its lag or the script is just not working as supposed to. However, the music does play, just one-by-one.

Code:

for _,Speaker in pairs(game.Workspace.Speakers:GetChildren()) do
        spawn(function()
            Speaker.Speaker.Sound.SoundId = "rbxassetid://" .. tostring(picked_value)
            Speaker.Speaker.Sound:Play()
        end)
end

However, it still goes one by one and activates the sound. How can I go about having it play to all of them instantly?

There’s no need for spawn(function() here.

for _,Speaker in pairs(game.Workspace.Speakers:GetChildren()) do

            Speaker.Speaker.Sound.SoundId = "rbxassetid://" .. tostring(picked_value)
            Speaker.Speaker.Sound:Play()
     
end

tostring(picked_value) what’s picke_value?

I use the spawn function as in a previous post months ago, I was told to have all the speakers instantly play I should use that function, as using for i,v goes one by one.

The picked value is just the song id, not related just ignore it haha

That should work.
THere is nothing wrong on that.
Maybe try this.

for _,v in pairs(game.Workspace.Speakers:GetChildren()) do

            v.Speaker.Sound.SoundId = "rbxassetid://" .. tostring(picked_value)
            v.Speaker.Sound:Play()
     print(tostring(picked_value)
end

Show output please

I’m guessing the issue could be that it takes a bit to set the song id and that’s why they’re not playing at the same time? Try this

local speakers = game.Workspace.Speakers:GetChildren()
for _,Speaker in pairs(speakers) do
    Speaker.Speaker.Sound.SoundId = "rbxassetid://" .. tostring(picked_value)
end
for _,Speaker in pairs(speakers) do
    Speaker.Speaker.Sound:Play()
end

Yeah I also tried that solution prior to posting here, but to no avail, to me made things sound even worse. The music does work in the end, so ultimately the script works, it’s just not doing it all instantly.

@prepsure
I will try that right now and see how that goes!

Unfortunately, it seems like that didn’t work~

An example I guess would be;

Speaker A;
“I’m not here for you”

Two seconds later, Speaker B;
“I’m not here for you”

It’s like an obnoxious echo.
I need to make it so Speaker A and Speaker B are playing at the same time and not delayed making an echo.

If I remember correctly, spawn() function has a built in wait() in it so I suggest you to use coroutine instead of spawn() like this:

for _,Speaker in pairs(game.Workspace.Speakers:GetChildren()) do
    local sound = coroutine.create(function()
        Speaker.Speaker.Sound.SoundId = "rbxassetid://" .. tostring(picked_value)
        Speaker.Speaker.Sound:Play()
    end)
    coroutine.resume(sound)
end

This should work better than than spawn() function.

Using a basic loop will make them all play instantly. There might be something wrong with the audio it self. Please check the audio and make sure everything is correct there.

You should set all sound ids a before attempting to play them since loading them requires time and is done sequentially. You can use the IsLoaded property or the Loaded event to implement this behaviour correctly.

@BenMactavsin
I have tried that just now and to no avail unfortunately.

@NachtHemd
I have tried that from this post and set a wait in between for 20 seconds, and then played the music, however no avail, the speaker music is still delayed throughout speakers.