I’m trying to make a modular music queue system for a group, but the queue part of the module doesn’t want to work.
The first requested song plays, but the second one never plays.
I’ve tried using next, tried using table.find(Queue,CurrentSong) + 1
, but none of my solutions worked
The songs also get added more times than they should, which is not supposed to happen in any situation.
The module:
local Music = {}
local Queue = require(script.Queue)
local MarketPlaceService = game:GetService("MarketplaceService")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Info
local CurrentSong
local Events = {
AddSong = ReplicatedStorage.AddSong
}
local DefaultQueue = {
5831568359,
2671105037,
}
local Cooldown = {} -- User cooldowns
local function BeginQueue()
print("HELOOO")
for _,Song in pairs(Queue) do
local Sound = workspace.Music
if Sound.SoundId ~= CurrentSong then
print(Queue)
print(true)
CurrentSong = Song
Sound.SoundId = Song
Sound:Play()
Events.AddSong:FireAllClients(Info)
end
Song = string.gsub(Song,"rbxassetid://","")
if #Queue <= 0 then
break
end
Sound.Ended:Connect(function()
if #Queue > 1 then
--// Just so it doesn't break the whole thing
local NextSong
local Success,Failure = pcall(function()
NextSong = next(Queue,Song.SoundId)
end)
if Success then
print(NextSong)
Sound.SoundId = NextSong
Sound:Play()
Info = MarketPlaceService:GetProductInfo(Sound.SoundId)
Events.AddSong:FireAllClients(Info)
end
else
Events.AddSong:FireAllClients({Name = "Nothing", Creator = {Name = "No one"}})
end
end)
end
end
--// :Play thing
Players.PlayerAdded:Connect(function(Player)
if Player:GetRankInGroup(3743720) >= 12 then
Player.Chatted:Connect(function(msg)
local Arguments = {}
for k in string.gmatch(msg,"[%S]+") do
table.insert(Arguments, k)
end
if Arguments[1] == ":play" then
table.remove(Arguments,1)
Music:ForceAddToQueue(Arguments[2])
end
end)
end
end)
function AddUserToCooldown(Player)
table.insert(Cooldown,Player)
delay(1, function()
local Pos = table.find(Cooldown,Player)
if Pos then table.remove(Cooldown,Pos) end
end)
end
function AddSong(Id)
Info = MarketPlaceService:GetProductInfo(Id)
print("Yep")
print(Queue)
print(#Queue)
table.insert(Queue,"rbxassetid://"..Id)
print(Queue)
print(#Queue)
if #Queue <= 1 then
print("Hello!")
BeginQueue()
else
Queue = DefaultQueue
BeginQueue()
end
end
function Music:AddSongToQueue(Player,Id)
-- Check if the user has the unlimited songs gamepass or is CCO+
if table.find(Cooldown,Player) then return end
if MarketPlaceService:UserOwnsGamePassAsync(Player.UserId,12541323) or Player:GetRankInGroup(3743720) >= 12 and not table.find(Cooldown,Player.UserId) then
print(Player.Name)
AddSong(Id)
if Player:GetRankInGroup(3743720) <= 12 then
AddUserToCooldown(Player)
end
else
MarketPlaceService:PromptGamePassPurchase(Player,776368)
MarketPlaceService.PromptGamePassPurchaseFinished:Connect(function(PlayerPurchased,GamepassId,WasPurchased)
if PlayerPurchased == Player and GamepassId == 776368 and WasPurchased then
AddSong(Id)
AddUserToCooldown(Player)
end
end)
end
end
function Music:ForceAddToQueue(Id)
AddSong(Id)
end
Events.AddSong.OnServerEvent:Connect(function(Player,Id)
Music:AddSongToQueue(Player,Id)
end)
return Music
The function I’m stuck on:
local function BeginQueue()
for _,Song in pairs(Queue) do
local Sound = workspace.Music
print(tostring(Sound.SoundId))
if Sound.SoundId ~= CurrentSong then
print(Queue)
Sound.SoundId = Song
Sound:Play()
CurrentSong = Song
Events.AddSong:FireAllClients(Info)
end
-- Check if the song is completed
Sound.Ended:Connect(function()
if #Queue > 0 then
print(Queue)
print(table.find(Queue,CurrentSong)+ 1)
local NextSong = Queue[table.find(Queue,CurrentSong) + 1]
if NextSong ~= nil then
Sound.SoundId = NextSong
CurrentSong = NextSong
Sound:Play()
end
end
end)
end
end