I’m making a game that involves taking two players placed in a table (queue) and teleporting the two of them into the arena to duel. Problem is, I’ve combined the two players into one value in a table, and placed said value into another table. In order to start the round, I need to get the players from that inner table, but I’m unsure of how to do so. Any help with this would be really appreciated.
local battleQueue = game.ReplicatedStorage.BattleQueue
local queueTable = {}
local roundActive = false
battleQueue.OnServerEvent:Connect(function(Player, name)
print("Adding players to queue")
local info = {Player, name}
table.insert(queueTable, info)
print(queueTable)
game.Players.PlayerRemoving:Connect(function()
for i, v in pairs(queueTable) do
if v[1] == Player then
table.remove(queueTable, i)
print("Player removed from queue.")
print(queueTable)
end
end
end)
end)
while roundActive == false do
print("Attempting round start")
local Players = table.find(queueTable, 1)
print(table.find(Players, Player, name)) -- Doesn't work
end
while roundActive == false do
print("Attempting round start")
local Players = table.find(queueTable, 1)
print(table.find(Players, Player, name)) -- Doesn't work
end
local battleQueue = game.ReplicatedStorage.BattleQueue
local queueTable = {}
local roundActive = false
battleQueue.OnServerEvent:Connect(function(Player, name)
print("Adding players to queue")
local info = {Player, name}
table.insert(queueTable, info)
print(queueTable)
game.Players.PlayerRemoving:Connect(function()
for i, v in pairs(queueTable) do
if v[1] == Player then
table.remove(queueTable, i)
print("Player removed from queue.")
print(queueTable)
end
end
end)
while roundActive == false do
wait(1.5)
print("Attempting round start")
local Players = queueTable[1]
print(`{Players[1]} ; {Players[2]}`) -- Doesn't work
end
end)
This script will cause memory leaks from the PlayerRemoving being connected inside the OnServerEvent listener. Also it would be better to make a cooldown using task.spawn and task.wait.
Example:
--[[
Example of queue:
Queue["Queue1"] = {
Player1,
Player2
}
]]
local Queues = {} -- Array with queues
local Cooldown = 10 -- Hardcoded cooldown value
Queue.OnServerEvent:Connect(function(Player, Name)
if not Queues[Name] then -- Queue doesn't exist
Queues[Name] = {Player} -- Create new queue with an array with players in it
task.spawn(function() -- Initalize a new thread for the queue
task.wait(Cooldown) -- Wait for cooldown to teleport players
for _, Player in Queues[Name] do -- Loop through players in queue
if Player then -- Check if the player exists
-- Teleport players
end
end
Queues[Name] = nil -- Remove queue from queues
end)
else
table.insert(Queues[Name], Player) -- If the queue exists then add the player to it
end
end)