Matchmaking help

I have been trying to make a 1v1 game.

But I’ve ran into a problem that is getting me stuck and not able to continue the game development.

The problem I’m facing is, if 3 players queue, 2 get teleported to the same server and also the one with no opponent gets teleported but to a separate server alone.

And what I am trying to achieve is if a player has no opponent then it shouldn’t teleport even when others players find a opponent and teleport.

How could I implement a logic that could do that?


local TPS = game:GetService("TeleportService")

local PlayersInQueue = {}

local MinPlayers = 2
local MaxPlayers = 2

local isMatchmaking = false

function Matchmake()
	if isMatchmaking == false then
		isMatchmaking = true
		
		repeat wait()
			for i,v in pairs(PlayersInQueue) do
				game.ReplicatedStorage.UpdateStatus:FireClient(v,"Waiting For at least "..tostring(MinPlayers).."players to join the Queue.")
				
			end
		until #PlayersInQueue == MinPlayers
		
		-Enough players to start-
		
		for i=0,15,1 do
			if #PlayersInQueue == 0 then
				isMatchmaking = false
				
				-Players have left the game-
				return
			end
			
			if #PlayersInQueue == MaxPlayers then
				break
			end
			
			for _,value in pairs(PlayersInQueue) do
				game.ReplicatedStorage.UpdateStatus:FireClient(value, "Teleporting in: ("..tostring(15-i)..")seconds. Players in Queue: ("..tostring(#PlayersInQueue)..")")
			end
			
			wait(1)
		end
		
		-Finished waiting. teleporting players now-
		
		local Server = TPS:ReserveServer(yourgameid)
		TPS:TeleportToPrivateServer(GameId,Server,PlayersInQueue)
		
		wait(3)
		
		PlayersInQueue = {}
		isMatchmaking = false
	end
end

script.Parent.Head.QueuePrompt.Triggered:Connect(function(player)
	if not table.find(PlayersInQueue,player) then
		table.insert(PlayersInQueue,player)
	end
		Matchmake()
end)

game.Players.PlayerRemoving:Connect(function(player)
	for i,v in pairs(PlayersInQueue) do
		if v.Name == player.Name then
			table.remove(PlayersInQueue,i)
		end
	end
end)
2 Likes

Would wrapping it in an if statement fix that?

	if #PlayersInQueue > 1 then
		TPS:TeleportToPrivateServer(GameId,Server,PlayersInQueue)
	end
1 Like

If that’s what you want then ‘MinPlayers’ should be 4 instead of 2 since you’re only waiting for 2 players. There’s better ways to handle this but that seems to be the simplest solution.

Create a dictionary containing tables. Each table will contain a “private server players list”. The only thing you need to do is place the player’s object inside this table to teleport them at the end of the countdown.

The whole dictionary should look like this:

local MatchmakingList = {
   {PlayerObject1,PlayerObject2}, -- Index is 1, the list of players for private server index1
 {PlayerObject1,PlayerObject2} -- Index is 2, the list of players for private server index2
}

Using this method, you can teleport all players inside index 1 by doing TPS:TeleportToPrivateServer(GameId,Server,MatchmakingList[1])

*Note: this method requires the dictionary to be emptied after players have been teleported, to avoid accumulation of player object inside the dictionary.
MatchmakingList[1] = nil
Doing so will change the index number of every index after the one you just removed. Index 2 will become index 1, ect.

Another edit, I saw that you where not using pcall to handles ̶p̶l̶a̶c̶e̶ “experiences” teleporation failure. You can learn more about them here :

*TeleportPartyAsync was the only close exemple using pcalls, I don’t know why the TeleportToPrivateServer exemple don’t use them, but it’s the same, you can also use pcalls with TeleportToPrivateServer.

1 Like

but that would not allow that if there were only 2 players on a server queueing to teleport?