So I make an announcement system with Queue System, but why everytime I do it, yes it fire, yes it makes queue, but only take the last one :
So let’s say I announce “CCCC”, and before the announcement finish I Announce twice again, let’s say the first one “AAAA” the second one “BBBBB”, why after it wait, it only fire the second one, and like removing the first one.
This is the script
-- Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Events
local eventsFolder = ReplicatedStorage.Events
local announcementEvent = eventsFolder.Announcement
-- Prefix
local Prefix = "!"
local broadcasted = 0
local queue = {}
local function getKeyFromId(keyId)
for key, keyTable in pairs(queue) do -- iterate over myTable
if keyTable == keyId then
return key
end
end
end
function announcement(player, message)
broadcasted += 1
print(broadcasted)
announcementEvent:FireAllClients(player, message)
task.wait(6)
broadcasted -= 1
print(broadcasted)
local message = queue[player]
local playerName = getKeyFromId(message)
if message then
table.remove(queue, table.find(queue, message))
announcement(playerName, message)
end
end
function requested(player, message)
if broadcasted < 1 then
announcement(player, message)
else
table.insert(queue, message)
queue[player] = queue[1]
queue[1] = nil
end
end
Players.PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(Chat)
local Split = Chat:split(" ")
local Command = Split[1]
if Command == Prefix.."n" then
local Arguments = string.sub(Chat, string.len(tostring(Prefix).."n".." "))
if Arguments then
requested(Player, Arguments)
end
end
end)
end)