Hi there. I have a queue system in which players players queue up to play a 1v1 PVP game. I want my queue system to make sure that it teleports users to the same server in that place.
The queue system works fine, however I stumbled upon the problem that the queue system doesn’t always teleport players to the exact same server. I’ve tried modifying my code using ReservedServers however now my code does not work at all. I’m not getting any errors in the output.
What can I do to fix this code in order to make it teleport users to the right place. Am I using ReserveServer() wrong? Any help would be appreciated, thanks!
local memoryStore = game:GetService("MemoryStoreService")
local queue = memoryStore:GetSortedMap("Queue")
local TeleportService = game:GetService("TeleportService")
local minimum = 2
local maximum = 2
local place = game:GetService("TeleportService"):ReserveServer(15840374569)
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
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)
elseif inQueue == "Join 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 >= 2 or amountQueued == maximum then
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()
TeleportService:TeleportAsync(place, {player})
end)
spawn(function()
if success then
wait(1)
pcall(function()
queue:RemoveAsync(data.key)
end)
end
end)
end
end
end
end
end