Finding the contents of a table within another table

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

The line you put --Doesn’t Work on is because “Player” and “name” are not in the While do function so it wouldnt be able to find player and name

Would I have to mix the round script with the function then? Or is there any other way?

You would have to move

while roundActive == false do
		print("Attempting round start")
		local Players = table.find(queueTable, 1)
		print(table.find(Players, Player, name)) -- Doesn't work
end

into the OnServerEvent section for that to work

1 Like

i believe i fixed the script it works for me now

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)
1 Like

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)
1 Like

Going to try this, will take me some time. Thank you, I’ll be back.

ChatChanisin back at it again with more posts

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.