Advertisement
HowToRoblox

MatchmakingServer

Jul 6th, 2022 (edited)
3,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1. --Variables
  2. local memoryStore = game:GetService("MemoryStoreService")
  3. local queue = memoryStore:GetSortedMap("Queue")
  4.  
  5. local tpService = game:GetService("TeleportService")
  6.  
  7. local minimum = 2
  8. local maximum = 30
  9.  
  10. local placeId = 9869371728
  11.  
  12. local re = game.ReplicatedStorage:WaitForChild("QueueRE")
  13.  
  14.  
  15. --Functions to edit queue
  16. function addToQueue(player)
  17.     queue:SetAsync(player.UserId, player.UserId, 2592000)
  18. end
  19.  
  20. function removeFromQueue(player)
  21.     queue:RemoveAsync(player.UserId)
  22. end
  23.  
  24.  
  25. --Add and remove players from queue when they press the button
  26. local cooldown = {}
  27.  
  28. re.OnServerEvent:Connect(function(player, inQueue)
  29.    
  30.     if cooldown[player] then return end
  31.     cooldown[player] = true
  32.    
  33.     if inQueue == "IN QUEUE" then
  34.         pcall(addToQueue, player)
  35.     elseif inQueue == "QUEUE" then
  36.         pcall(removeFromQueue, player)
  37.     end
  38.    
  39.     wait(1)
  40.     cooldown[player] = false
  41. end)
  42.  
  43.  
  44. --Remove player from queue if they leave the server
  45. game.Players.PlayerRemoving:Connect(removeFromQueue)
  46.  
  47.  
  48. --Check when enough players are in the queue to teleport players
  49. local lastOverMin = tick()
  50.  
  51. while wait(1) do
  52.    
  53.     local success, queuedPlayers = pcall(function()
  54.         return queue:GetRangeAsync(Enum.SortDirection.Descending, maximum)
  55.     end)
  56.    
  57.     if success then
  58.        
  59.         local amountQueued = 0
  60.        
  61.         for i, data in pairs(queuedPlayers) do
  62.             amountQueued += 1
  63.         end
  64.        
  65.         if amountQueued < minimum then
  66.             lastOverMin = tick()
  67.         end
  68.        
  69.         --Wait 20 seconds after the minimum players is reached to allow for more players to join the queue
  70.         --Or instantly queue once the maximum players is reached
  71.         local timeOverMin = tick() - lastOverMin
  72.        
  73.         if timeOverMin >= 20 or amountQueued == maximum then
  74.            
  75.             for i, data in pairs(queuedPlayers) do 
  76.                
  77.                 local userId = data.value
  78.                 local player = game.Players:GetPlayerByUserId(userId)
  79.                
  80.                 if player then
  81.                     local success, err = pcall(function()
  82.                         tpService:TeleportAsync(placeId, {player})
  83.                     end)
  84.                    
  85.                     spawn(function()
  86.                         if success then
  87.                             wait(1)
  88.                             pcall(function()
  89.                                 queue:RemoveAsync(data.key)
  90.                             end)
  91.                         end
  92.                     end)
  93.                 end
  94.             end
  95.         end
  96.     end
  97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement