Issues with my matchmaking script

  1. What do you want to achieve?
    For the players to be teleported to the same random map chosen from the map id’s

  2. What is the issue?
    It simply doesn’t work, and i have no idea whats the issue, i tried adding some prints to find what could be the problem but still couldn’t find the issue

  3. What solutions have you tried so far?
    Changing the script a few times with hope it will work eventually

local memoryStore = game:GetService("MemoryStoreService")
local queue = memoryStore:GetSortedMap("Queue")

local tpService = game:GetService("TeleportService")

local minimum = 2
local maximum = 2


local mapIds = {
	14493083952, 14499249428, 14499079982, 14498823338, 14498792405, 14498770601, 14498753396, 14498711952, 14498658262, 14493181814, 14498584708,
	14498516008, 14498461694, 14498252945, 14498253101, 14497782992, 14493259975, 14493310064, 14493373296, 14493526788, 14493744649, 14497518355,
	14497597722, 14497995350, 14497967099
}

local re = game.ReplicatedStorage:WaitForChild("QueueRE")


function addToQueue(player)
	queue:SetAsync(player.UserId, player.UserId, 2592000)
end

function removeFromQueue(player)
	queue:RemoveAsync(player.UserId)
end


function getRandomMapId()
	return mapIds[math.random(1, #mapIds)]
end


local cooldown = {}

re.OnServerEvent:Connect(function(player, inQueue)

	if cooldown[player] then return end
	cooldown[player] = true

	if inQueue == "Waiting for another player.." then
		pcall(addToQueue, player)
	elseif inQueue == "Random Queue" then
		pcall(removeFromQueue, player)
	end

	wait(1)
	cooldown[player] = false
end)


game.Players.PlayerRemoving:Connect(removeFromQueue)


local lastOverMin = tick()

while wait(1) do

	local success, queuedPlayers = pcall(function()
		return queue:GetRangeAsync(Enum.SortDirection.Descending, maximum)
	end)

	if success then

		local amountQueued = 0

		for i, data in pairs(queuedPlayers) do
			amountQueued += 1
		end

		if amountQueued < minimum then
			lastOverMin = tick()
		end


		local timeOverMin = tick() - lastOverMin

		if timeOverMin >= 20 or amountQueued == maximum then
			local randomMapId = getRandomMapId()

			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(randomMapId, {player})
					end)

					spawn(function()
						if success then
							wait(1)
							pcall(function()
								queue:RemoveAsync(data.key)
							end)
						end
					end)
				end
			end
		end
	end
end

What could be the issue of the script not functioning? It sometimes teleports 1 player and sometimes it doesn’t teleport anybody, I’m clueless of what to do now so im asking for help

1 Like