Looping through metatables

I’m making a game that will require looping through metatables in order to work a queue system. Problem is, table looping is my absolute worst nightmare in scripting. Just can’t seem to grasp it. Nonetheless, this is how it currently works:

2 players are put into one of 5 tables; queue1, queue2, queue3, queue4 or queue5.

Each of these queues are put into a metatable called queueTable.

I need to make the game loop through these queues, in order, and pull the names of the two players so that it can teleport them into the ring and start the round. I’d really appreciate an explanation on how exactly to do this and how it works, as metatables are a new thing for me. Thank you! Script below:

local battleQueue = game.ReplicatedStorage.BattleQueue

local queueTable = {}
local queue1 = {}
local queue2 = {}
local queue3 = {}
local queue4 = {}
local queue5 = {}

setmetatable(queueTable, queue1)
setmetatable(queueTable, queue2)
setmetatable(queueTable, queue3)
setmetatable(queueTable, queue4)
setmetatable(queueTable, queue5)

local roundActive = game.ReplicatedStorage.RoundActive
roundActive = false

battleQueue.OnServerEvent:Connect(function(Player, name)
	print(Player)
	print(name)
	
	if queue1 == 0 then
		table.insert(queue1, Player, name)
	elseif queue2 == 0 then
		table.insert(queue2, Player, name)
	elseif queue3 == 0 then
		table.insert(queue3, Player, name)
	elseif queue4 == 0 then
		table.insert(queue4, Player, name)
	elseif queue5 == 0 then
		table.insert(queue5, Player, name)
	else
		print("Impossible??")
	end
	
	while roundActive == false do
		
		print("Attempting round start")
		
		local Challenger = Player
		local Defender = name
		
		local challengerTP = game.Workspace.Map.ChallengerSpawn
		local DefenderTP = game.Workspace.Map.DefenderSpawn
		
		-- This is where I've left off
		
	end
end)

table looping is simple

-- switch ipairs() for pairs() if it doesnt seem to do anything, lmk if it does that
for indexNumber, indexObject in ipairs(yourMetatable)
     print(indexNumber)
     print(indexObject)
end

in a table you can loop through it with for, and it’s going to loop through it for each index and do something per index, it can give you some stuff about its current index too so for each index it can give you the index number, or where it’s at and the index object itself, or what’s at the index number

it SHOULD work for metatables but if your metatable is using a key value pair then it’s best you use pairs() instead (i dont actually know tho stick to ipairs() for an ordered thingy i think)

btw i dont think thats how setmetatable() works, put in all of the queue tables as key-value pairs inside queueTable then do setmetatable(queueTable, {})

note that a key-value pair looks like this

local tableExample = {
   key1 = "bla";
   key2 = "ble";
   key3 = 1;
   key4 = true;
   key5 = 4 + 4
}

compared to a normal table

local tableExample = {
   "bla";
   "ble";
   1;
   true;
   4 + 4
}

and have these documentation and devforum links to help you understand more

1 Like