Loop to look for any audio not playing. not working

My goal is to make a function that loops through a part holding all swoosh sounds for a sword, then it plays any of the 3 sounds that “IsPlaying == false”.

My code:

local function swoosh()
	for _, sound in pairs(weapon.SwooshSounds:GetChildren()) do
		if sound.IsPlaying == false then
			sound:Play()
		end
	end
end

There’s 3 audios in the part and the main issue is it works somewhat but it only will see the first audio not the other two in the part. :confused:

local children = weapon.SwooshSounds:GetChildren()
local sounds = {}
local playing = {}

for i, v in pairs(children) do
   if v:IsA("Audio") then
      table.insert(sounds, v)
   end
end

local function play()
   local n = math.random(1,#sounds)
   local sound = sounds[n]
   sound:Play()
   table.remove(sounds, n)
   table.insert(playing, sound)

   coroutine.wrap(function()
      sound.Ended:Wait()
      table.insert(sounds, sound)
      table.remove(playing, table.find(playing, sound))
   end)
end
1 Like

So I got an error using that

Error
Players.bbnoshanky.Backpack.Stevonnie.ToolServer:283: invalid argument #2 to ‘random’ (interval is empty)

line that is errored:

local n = math.random(1,#sounds)

also it plays all 3 sounds but when it goes back to i guess replay them it gives that error

Both this issue and

This issue has to do with me forgetting to run the coroutine, add at the end of the coroutine (), like so:

coroutine.wrap(function()
   sound.Ended:Wait()
   table.insert(sounds, sound)
   table.remove(playing, table.find(playing, sound))
end)()

This should fix it, if any other error occurs it has probably to do with not enough audios being added.

1 Like

works so nicely omg!!

Thank you so much <3