How can I carry variables of tables through functions?

So I have a matchmaking system with multiple game modes. All the different game mode’s queues, player counts, and placeIds are inside of a table and I would like to carry it through functions! I have a variable called “CurrentQueue” that switches data whenever a player chooses a new game mode. However I need to find out how to carry them through functions! This is my script so far!


--Variables
local memoryStore = game:GetService("MemoryStoreService")

local tpService = game:GetService("TeleportService")

local DuelQueue = memoryStore:GetSortedMap("DuelQueue")
local DuelPlayerCount = 2
local DuelPlaceId = 13270967622
local DuelData = {DuelQueue, DuelPlayerCount, DuelPlaceId}

local DoubleQueue = memoryStore:GetSortedMap("DoubleQueue")
local DoublePlayerCount = 4
local DoublePlaceId = nil
local DoubleData = {DoubleQueue, DoublePlayerCount, DoublePlaceId}

local re = game.ReplicatedStorage.RemoteEvents:WaitForChild("Matchmake")

local CurrentQueue


--Functions to edit queue
function addToQueue(player, QueueToAdd)
	QueueToAdd:SetAsync(player.UserId, player.UserId, 2592000)
	print("1")
end

function removeFromQueue(player, QueueToRemove)
	QueueToRemove:RemoveAsync(player.UserId)
end


--Add and remove players from queue when they press the button
local cooldown = {}

re.OnServerEvent:Connect(function(player, Ready, Queue)
	
	if Queue == "Duel" then
		CurrentQueue = DuelData
	elseif Queue == "Double" then
		CurrentQueue = DoubleData
	end
	
	if cooldown[player] then return end
	cooldown[player] = true
	
	if Ready == "Unready" then
		pcall(addToQueue, player, CurrentQueue[1])
	elseif Ready == "Ready" then
		pcall(removeFromQueue, player, CurrentQueue[1])
	end
	
	wait(1)
	cooldown[player] = false
end)


--Remove player from queue if they leave the server
game.Players.PlayerRemoving:Connect(removeFromQueue)


--Check when enough players are in the queue to teleport players
local lastOverMin = tick()

while wait(1) do
	
	local success, queuedPlayers = pcall(function()
		return DuelQueue:GetRangeAsync(Enum.SortDirection.Descending, CurrentQueue[2])
	end)
	
	if success then
		print("2")
		local amountQueued = 0
		
		for i, data in pairs(queuedPlayers) do
			amountQueued += 1
		end
		
		if amountQueued < CurrentQueue[2] then
			lastOverMin = tick()
		end
		
		--Wait 20 seconds after the minimum players is reached to allow for more players to join the queue
		--Or instantly queue once the maximum players is reached
		local timeOverMin = tick() - lastOverMin
		
		if timeOverMin >= 20 or amountQueued == CurrentQueue[2] then
			
			for i, data in pairs(queuedPlayers) do	
				
				local userId = data.value
				local player = game.Players:GetPlayerByUserId(userId)
				
				if player then
					local success, err = pcall(function()
						tpService:TeleportAsync(CurrentQueue[3], {player})
					end) 
					
					spawn(function()
						if success then
							wait(1)
							pcall(function()
								DuelQueue:RemoveAsync(data.key)
							end)
						end
					end)
				end
			end
		end
	end
end

As you can see, there are multiple places with CurrentQueue[1],[2],etc. However I cant get this to work and actually switch gamemodes :frowning:

1 Like

I solved it! Thanks everyone :slight_smile:

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