So i’ve been working on this all morning and I’ve gotten it to an almost working state. The queue works so far with the first message and the scrubber removing duplicates n whatnot. However, it doesn’t queue the second message and the handleChatted function returns nil for some reason? Idk if it has something to do with the scrubber function somehow as that would be the latest addition but any help would be appreciated. Sorry if it’s convoluted or not very well put together I’m a bit rusty at scripting.
Any help would be appreciated, thanks in advance!
--player table incase a player gets disconnected and rejoins
local players = {}
local messageQueue = {}
local playerMsgQueue = {}
local lastMessage
local textService = game:GetService("TextService")
local events = game.ReplicatedStorage.events
local settingsValues = script.settings
--add player to table
game.Players.PlayerAdded:Connect(function(player)
table.insert(players, player)
end)
-- remove player from table
game.Players.PlayerRemoving:Connect(function(player)
for i,v in pairs(players) do
if v == player then
table.remove(players, i)
for a,b in pairs(messageQueue) do
if table.find(playerMsgQueue, player) then
if playerMsgQueue[a] == player then
table.remove(playerMsgQueue, a)
table.remove(messageQueue, a)
end
end
end
break
end
end
end)
--functions
--text filter function
local filterMsg = function(userId, message)
local filter = textService:FilterStringAsync(message, userId)
return tostring(filter:GetChatForUserAsync(userId))
end
--checkMsgQueue function
local checkQueue = function(message, player)
if #messageQueue >= 1 then
if messageQueue[message] == playerMsgQueue[player] then
return true
elseif messageQueue[message] ~= playerMsgQueue[player] then
return false
end
elseif #messageQueue <=0 then
return false
end
end
--addToQueue function
local addToQueue = function(message, player)
local filteredMsg = filterMsg(player.UserId, message)
table.insert(messageQueue,filteredMsg)
table.insert(playerMsgQueue, player)
local msgNum = table.find(messageQueue, filteredMsg)
local plrNum = table.find(playerMsgQueue, player)
if msgNum ~= plrNum then
for i,v in pairs(messageQueue) do
if v == filteredMsg then
table.remove(messageQueue, i)
end
end
for i,v in pairs(playerMsgQueue) do
if v == player then
table.remove(playerMsgQueue, i)
end
end
return false
elseif msgNum == plrNum then
print(msgNum, plrNum)
return true
end
end
--scrubcheck function
local scrubcheck = function(value, valuePos, queue)
local similarities = {}
local indexes = {}
for i,v in pairs(queue) do
if i ~= valuePos and v == value then
table.insert(similarities, v)
table.insert(indexes, i)
end
end
if #similarities >= 1 then
return similarities, indexes
elseif #similarities <= 0 then
return false
end
end
--scrubber function
local scrub = function(queue)
for i, v in pairs(queue) do
if scrubcheck(v, i, queue) ~= false then
local dupes, indexes = scrubcheck(v, i, queue)
if dupes ~= nil and indexes ~= nil then
print("dupes and indexes")
print(dupes)
print(indexes)
for a, b in pairs(dupes) do
--confirm its still in the table
if table.find(queue, b) ~= nil then
local found = table.find(queue, b)
table.remove(queue, found)
end
end
end
elseif scrubcheck(v, i, queue) == false then
print("no dupes")
end
end
end
local warningPrinted = false
local finalWarnPrinted = false
--chatted event handler
local handleChatted = function(player, message)
local added = false
if settingsValues.queueEnabled.Value == true then
if checkQueue(message, player) == false then
--error handling
if addToQueue(message, player) == false then
print("adding would have failed, initating resync protocol")
if warningPrinted == false then
warn("An error has occurred, queue numbers did not match. Starting retry attempts")
warningPrinted = true
end
for i = 0, settingsValues.retries.Value, 1 do
print("Looping due to error")
if checkQueue(message, player) == false then
if added == false then
added = true
addToQueue(message, player)
end
elseif checkQueue(message, player) == true then
if added == false then
added = true
addToQueue(message, player)
end
print("Successfully synced queue numbers! Attempts: "..tostring(i))
return true
end
if i == settingsValues.retries.Value then
if finalWarnPrinted == false then
warn("Maximum attempts made. Message has not been added to queue, informing the player.")
finalWarnPrinted = true
end
end
end
elseif addToQueue(message, player) == true then
print("if addtoqueue = true")
print(playerMsgQueue)
print(messageQueue)
if added == false then
addToQueue(message, player)
added = true
end
--print(playerMsgQueue)
--print(messageQueue)
return true
end
end
end
end
local playerAdded = false
local msgDisplayed = false
--broadcaster
coroutine.wrap(function()
while wait() do
if #messageQueue >= 1 then
for i,v in pairs(messageQueue) do
if msgDisplayed == false then
msgDisplayed = true
events.radioPlr:FireAllClients(v, playerMsgQueue[i])
wait(settingsValues.messageStay)
msgDisplayed = false
end
end
end
end
end)()
--queue monitor
if settingsValues.debugMode.Value == true then
task.spawn(function()
while wait(1) do
print(playerMsgQueue)
print(messageQueue)
print(#playerMsgQueue, #messageQueue)
end
end)
end
-- queue handler
task.spawn(function()
while wait() do
-- queueing Messages
if settingsValues.queueEnabled.Value == true then
for i, v in pairs(players) do
if playerAdded == true then
print("broken")
break
end
v.Chatted:Connect(function(message)
print(v.Name.." said: "..message)
if handleChatted(v, message) == true and playerAdded == false then
print("chatted")
handleChatted(v, message)
print("added")
playerAdded = true
print(playerMsgQueue)
print(messageQueue)
else --debugging output incase it fails for whatever reason
print("some error occured")
print(handleChatted(v, message))
print(playerAdded)
end
playerAdded = false
end)
end
--duplicate message scrubber
if #messageQueue > 1 then
scrub(messageQueue)
elseif #playerMsgQueue > 1 then
scrub(playerMsgQueue)
end
playerAdded = false
warningPrinted = false
finalWarnPrinted = false
end
end
end)