Help with Match Making!

Hey ya’ll! I’m currently making a across server match making system. I have made some basic code (which I hope works) that will add a players player ID to a table on all servers. I am currently stuck on matching two players together. I tried matching but I ran into a problem. If they both look for a match. What if player1 gets another player while player2 gets player1. It will be all screwed up. How can I make it find both opponents and match them together without them getting a different opponent?

Note: I am going to use Teleport Service to create a server for them and teleport them there with their userID that’s why I have their userID’s in the table.

Current Code:

local PS = game:GetService("Players")
local MS = game:GetService("MessagingService")
local TS = game:GetService("TeleportService")

local Event = game:GetService("ReplicatedStorage").StartMatchmaking

local GameId = 7684162178

local InQueue = {}

function CreateMatch(player, status)
	local foundMatch = false
	local function Waiting()
		repeat
			status.Text = "Looking for Match! Please Wait"
			wait(0.25)
			status.Text = "Looking for Match! Please Wait."
			wait(0.25)
			status.Text = "Looking for Match! Please Wait.."
			wait(0.25)
			status.Text = "Looking for Match! Please Wait..."
			wait(0.25)
		until foundMatch
	end
	coroutine.wrap(Waiting)()
end

MS:SubscribeAsync("PlayersInQueue", function(PlayerId)
	table.insert(InQueue, PlayerId.Data)
	print(InQueue)
end)

Event.OnServerEvent:Connect(function(player, status)
	--// Insert UserID to All Servers
	if (not table.find(InQueue, player.UserId)) then
		MS:PublishAsync("PlayersInQueue", player.UserId)
		print("User Added to Waiting For Match")
	else
		print("User Already Waiting For Match")
	end
	CreateMatch(player, status)
end)
1 Like

You could make a boolvalue whether player is in a match or not:

Visualization:

local Players= {
	Player1 = {
		inmatch = false
	},
	Player2 = {
		inmatch = false
	}
}

And do an if conditional that checks for inmatch and see’s if it’s false or true, and before teleporting, check if Player1 match is Player2 and Player2 match is Player1. Either, you can do the inmatch solution only or the two.

2 Likes

Thanks for your suggestion! I will definitely try to do something with this.