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)