Quick help with an audio queue script needed


#1

So let me tell you what I want: I have 29 sound objects in my game which contain different voices for different situations. In order to prevent them from being played at the same time, I am trying to make a queue script. So if a voice is playing, and another voice wants to be played too, it should be queued in a table.

local SoundRemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("Voice")
local SoundBindableEvent = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("MainGui"):WaitForChild("Frames"):WaitForChild("Events"):WaitForChild("Voice")
local Folder = script.Parent
local Queue = {}

function PlayVoice(Number)
    for i,v in pairs(Folder:GetChildren()) do
        if v:IsA("Sound") then
            if v.IsPlaying == true then
                table.insert(Queue, #Queue + 1, Folder:WaitForChild(tostring(Number)))
                repeat wait() until v.IsPlaying == false
                local A = Queue[1]
                table.remove(Queue, 1)
                A:Play()
                break
            else
                Folder:WaitForChild(tostring(Number)):Play()
                break
            end
        end
    end
end

SoundRemoteEvent.OnClientEvent:Connect(PlayVoice)
SoundBindableEvent.Event:Connect(PlayVoice)

Sounds still play at the same time…

Anyone see what I did wrong?


#2

You are probably calling “PlayVoice” multiple times, somewhere.

 if v.IsPlaying == true then
                table.insert(Queue, #Queue + 1, Folder:WaitForChild(tostring(Number)))
                repeat wait() until v.IsPlaying == false
                local A = Queue[1]
                table.remove(Queue, 1)
                A:Play()
                break
            else
                Folder:WaitForChild(tostring(Number)):Play()
                break
            end

Probably runs multiple times at once, since it is probably called from multiple places at once.


#3

The queue should contain all sounds. If you insert the actual sound object in to the table you can then remove it.

All sounds by default should be set to not playing.
When you go to play a sound you should do a check to see if the current queue is at 0, if it is then you can play the sound & add it… if not then add it to the queue without playing.

When the sound is done remove it from the queue.