Make players join same server in queue

I have a match making script, but when I tested with another person we got teleported to different servers, how would I fix this and make all players in the queue teleport to the same server.

I would suggest using Reserved Servers but I would have no idea on how to add it to the code.

This is my current script:

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

local tpService = game:GetService("TeleportService")

local minimum = 2
local maximum = 12
 
local placeId = 12576175275
 
local re = game.ReplicatedStorage.MenuRemotes:WaitForChild("Queue")
 
 

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

local cooldown = {}
 
re.OnServerEvent:Connect(function(player, inQueue)
    
    if cooldown[player] then return end
    cooldown[player] = true
    
    if inQueue == "IN QUEUE" then
        pcall(addToQueue, player)
		print("Player in Queue")
    elseif inQueue == "QUEUE" then
        pcall(removeFromQueue, player)
		print("Player not in queue")
    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
			print("Added Player to Queue")
			print(amountQueued)
        end
        
        if amountQueued < minimum then
			print("Not enough players")
            lastOverMin = tick()
        end
        
        local timeOverMin = tick() - lastOverMin
        
        if timeOverMin >= 15 or amountQueued == maximum then
            print("Queue Process Begin")
            for i, data in pairs(queuedPlayers) do  
                
                local userId = data.value
                local player = game.Players:GetPlayerByUserId(userId)
                
                if player then
					print("Player found in queue")
                    local success, err = pcall(function()
						player.FoundMatch.Value = true
						print("Value Configured")
                        tpService:TeleportAsync(placeId, {player})
						print("Teleporting")
                    end) 
                    
                    spawn(function()
                        if success then
                            wait(1)
                            pcall(function()
                                queue:RemoveAsync(data.key)
                            end)
                        end
                    end)
                end
            end
        end
    end
end
1 Like

Just add players you want to teleport within this table.
image

If you dont want anyone to join the game server then you can teleport them in the private server.

local code = teleportService:ReserveServer(placeId)
local players = game.Players:GetPlayers()
teleportService:TeleportToPrivateServer(placeid,code,players)

-- This code will teleport every player in the server to a private server.

For some reason, I get no errors but it won’t teleport the players to a private server on a different place when I test ingame.

local tpService = game:GetService("TeleportService")
local code = tpService:ReserveServer(12576175275)
tpService:TeleportToPrivateServer(12576175275,code,queuedPlayers)

To learn more about Teleporting players: TeleportService | Roblox Creator Documentation

This is code from the link above/ingame:

local TeleportService = game:GetService(“TeleportService”)
local Players = game:GetService(“Players”)
local DataStoreService = game:GetService(“DataStoreService”)

local dataStore = DataStoreService:GetGlobalDataStore()

– Get the saved code
local code = dataStore:GetAsync(“ReservedServer”)
if typeof(code) ~= “string” then – None saved, create one
code = TeleportService:ReserveServer(game.PlaceId)
dataStore:SetAsync(“ReservedServer”, code)
end

local function joined(player)
player.Chatted:Connect(function(message)
if message == “reserved” then
TeleportService:TeleportToPrivateServer(game.PlaceId, code, { player })
end
end)
end

Players.PlayerAdded:Connect(joined)

Why wont this teleport the queued players?
It doesnt give any errors.

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

local tpService = game:GetService("TeleportService")

local minimum = 1
local maximum = 12
 
local placeId = 12576175275
 
local re = game.ReplicatedStorage.MenuRemotes:WaitForChild("Queue")
 

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

local cooldown = {}
 
re.OnServerEvent:Connect(function(player, inQueue)
    
    if cooldown[player] then return end
    cooldown[player] = true
    
    if inQueue == "IN QUEUE" then
        pcall(addToQueue, player)
		print("Player in Queue")
    elseif inQueue == "QUEUE" then
        pcall(removeFromQueue, player)
		print("Player not in queue")
    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
			print("Added Player to Queue")
			print(amountQueued)
        end
        
        if amountQueued < minimum then
			print("Not enough players")
            lastOverMin = tick()
        end
        
        local timeOverMin = tick() - lastOverMin
        
        if timeOverMin >= 15 or amountQueued == maximum then
            print("Queue Process Begin")
            for i, data in pairs(queuedPlayers) do  
                
                local userId = data.value
                local player = game.Players:GetPlayerByUserId(userId)
                
                if player then
					print("Player found in queue")
                    local success, err = pcall(function()
						player.FoundMatch.Value = true
						local code = tpService:ReserveServer(12576175275)
						tpService:TeleportToPrivateServer(12576175275, code, queuedPlayers)
						--tpService:TeleportAsync(placeId, {player}) -- original code
						print("Teleporting")
                    end) 
                    
                    spawn(function()
                        if success then
                            wait(1)
                            pcall(function()
                                queue:RemoveAsync(data.key)
                            end)
                        end
                    end)
                end
            end
        end
    end
end