loleriss

Untitled

Sep 26th, 2021 (edited)
696
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local runService = game:GetService("RunService")
  2.  
  3. local tel = game:GetService("TeleportService")
  4.  
  5. local mes = game:GetService("MessagingService")
  6.  
  7. local listTopic = "ServerList"
  8.  
  9. local requestServers = "RequestServers"
  10.  
  11. local codeMatch = "CheckCodeMatch"
  12.  
  13. local requestCode = "RequestCode"
  14.  
  15. local requestModify = "RequestModify"
  16.  
  17. local mapSkeldId = 5928953638
  18.  
  19. local queued = {
  20.  
  21. }
  22.  
  23. -- variables and tables
  24.  
  25. local serverCodes = {
  26.     --["ABCDEF"] = "21i4b5i2tewtewr3u5bip34u2"
  27. }
  28.  
  29. function addServer(message, updating)
  30.     game.ReplicatedStorage.CreateServer:FireAllClients(message, nil, updating)
  31.  
  32.     print(4)
  33. end -- adds a server to all clients
  34.  
  35. function addServerSingular(player, message, bundle, updating)
  36.     print("sing")
  37.    
  38.     game.ReplicatedStorage.CreateServer:FireClient(player, message, bundle, updating)
  39.    
  40.     print("sing2")
  41.  
  42.     print(5)
  43. end
  44.  
  45.  -- adds a server to a specific client
  46.  
  47. function addServerPersonal(message, updating)
  48.     print("pers")
  49.    
  50.     local data = message.Data
  51.    
  52.     if updating then
  53.         data = message.Data[4]
  54.     end
  55.  
  56.     local clone = game.ReplicatedStorage.Servers["123456789-ABCDEF"]:Clone()
  57.  
  58.     clone.Parent = game.ReplicatedStorage.Servers.List
  59.  
  60.     clone.Name = data[2] .. "-" .. "ABCDEF"
  61.  
  62.     clone.HostUserId.Value = data[2]
  63.  
  64.     clone.Code.Value = data[4]
  65.  
  66.     clone.ReservedCode.Value = data[5][1]
  67. end -- adds a server to the games own storage for debugging / etc
  68.  
  69. mes:SubscribeAsync(listTopic, function(message)
  70.     print(message)
  71.    
  72.     local toDo = message.Data[1]
  73.  
  74.     print(2)
  75.  
  76.     print(toDo)
  77.    
  78.     local data = message.Data[4]
  79.  
  80.     if toDo == "Add" then
  81.         print(3)
  82.  
  83.         addServer(message, message.Data[5])
  84.  
  85.         addServerPersonal(message, message.Data[5])
  86.     else
  87.         print(toDo)
  88.        
  89.         if data[1] == "Add" then
  90.             print(4)
  91.            
  92.             addServer(message, message.Data[5])
  93.  
  94.             --addServerPersonal(message, message.Data[5])
  95.         else
  96.             print(data[1], "not")
  97.         end
  98.     end
  99. end) -- adds a server when it is created to all clients
  100.  
  101. mes:SubscribeAsync(codeMatch, function(message)
  102.     local data = message.Data
  103.  
  104.     if serverCodes[data[2]] and message[3] ~= game.JobId then
  105.         mes:PublishAsync(data[1] .. codeMatch, true)
  106.     else
  107.         print(message[3], game.JobId)
  108.     end
  109. end) -- checks if the invite code for a server exists or not.
  110.  
  111. game.ReplicatedStorage.CreateServer.OnServerEvent:Connect(function(player)
  112.     local code = ""
  113.  
  114.     local function settage(num)
  115.         local codage = {}
  116.  
  117.         for i = 1, num do
  118.             codage[#codage + 1] = string.char(math.random(65, 90))
  119.         end
  120.  
  121.         local codaged = table.concat(codage, "")
  122.  
  123.         local event1
  124.  
  125.         local taken = false
  126.  
  127.         event1 = mes:SubscribeAsync(player.UserId .. codeMatch, function(message)
  128.             if message.Data then
  129.                 taken = true
  130.  
  131.                 event1:Disconnect()
  132.             end
  133.         end)
  134.  
  135.         mes:PublishAsync(codeMatch, {player.UserId, codaged, game.JobId})
  136.  
  137.         wait(1)
  138.  
  139.         if taken then
  140.             return settage(num)
  141.         else
  142.             return codaged
  143.         end
  144.     end
  145.  
  146.     code = settage(6)
  147.  
  148.     print(code)
  149.  
  150.     local id
  151.  
  152.     if runService:IsStudio() then
  153.         id = {"studioPlayer", player.UserId}
  154.     else
  155.         id = {tel:ReserveServer(mapSkeldId), player.UserId}
  156.     end
  157.  
  158.     local data = {"Add", player.UserId, player.Name, code, id}
  159.  
  160.     serverCodes[code] = id[1]
  161.  
  162.     print(data[1])
  163.    
  164.     --lined because of private / public features (private is default, public comes after)
  165.  
  166.     --mes:PublishAsync(listTopic, data)
  167.  
  168.     print(1)
  169.  
  170.     game.ReplicatedStorage.TeleportCode.InititateJoin:Fire(player, id[1], code)
  171. end) -- fires when the player creates a new server and creates the server. the creator of the server will auto join the server aswell.
  172.  
  173. game.Players.PlayerAdded:Connect(function(player)
  174.     for i, v in pairs(game.ReplicatedStorage.Servers.List:GetChildren()) do
  175.         local id = v.HostUserId.Value
  176.  
  177.         local name
  178.  
  179.         if not runService:IsStudio() then
  180.             name = game.Players:GetNameFromUserIdAsync(id)
  181.         else
  182.             name = "StudioPlayer" .. -id
  183.         end
  184.  
  185.         local data = {"Add", id, name, v.Code.Value, v.ReservedCode.Value}
  186.  
  187.         addServerSingular(player, data, true)
  188.     end
  189.  
  190.     queued[player.UserId] = true
  191.  
  192.     local event
  193.  
  194.     local lastRecieverTime = 0
  195.  
  196.     event = mes:SubscribeAsync(player.UserId, function(message)
  197.         local data = message.Data
  198.  
  199.         lastRecieverTime = -1
  200.        
  201.         if data[6] then
  202.             addServerSingular(player, data, true)
  203.         else
  204.             addServerSingular(player, data)
  205.         end
  206.        
  207.         print("data", data)
  208.  
  209.         lastRecieverTime = 0
  210.     end)
  211.  
  212.     mes:PublishAsync(requestServers, player.UserId)
  213.  
  214.     repeat
  215.         wait()
  216.  
  217.         if lastRecieverTime >= 0 then
  218.             lastRecieverTime += 0.033333333333333333333333333333333333333333333333333333333333333333333333333333333
  219.         end
  220.     until lastRecieverTime >= 1
  221.  
  222.     event:Disconnect()
  223.  
  224.     queued[player.UserId] = nil
  225.  
  226.     --should receive all servers
  227. end) -- will retrieve a list of every server to add to the player
  228.  
  229. mes:SubscribeAsync(requestServers, function(message)
  230.     local data = message.Data
  231.  
  232.     if not game.Players:FindFirstChild(game.Players:GetNameFromUserIdAsync(data)) then
  233.         for i, v in pairs(game.ReplicatedStorage.Servers.List:GetChildren()) do
  234.             local info
  235.  
  236.             if not runService:IsStudio() then
  237.                 info = {"Add", v.HostUserId.Value, game.Players:GetNameFromUserIdAsync(v.HostUserId.Value), v.Code.Value, v.ReservedCode.Value}
  238.             else
  239.                 info = {"Add", v.HostUserId.Value, "StudioPlayer" .. -v.HostUserId.Value, v.Code.Value, v.ReservedCode.Value}
  240.             end
  241.  
  242.             mes:PublishAsync(data, info)
  243.         end
  244.     end
  245. end) -- returns a list of all the game server's serves when requested to.
  246.  
  247.  
  248.  
  249. game.ReplicatedStorage.TeleportCode.OnServerEvent:Connect(function(player, key)
  250.     tel:TeleportToPrivateServer(mapSkeldId, key, {player})
  251. end) -- teleports the player to the server they join
  252.  
  253. game.ReplicatedStorage.TeleportCode.InititateJoin.Event:Connect(function(player, key, code)
  254.     tel:TeleportToPrivateServer(mapSkeldId, key, {player}, nil, {
  255.         ["Code"] = code,
  256.        
  257.         ["Key"] = key,
  258.        
  259.         ["UserId"] = player.UserId
  260.     }, nil
  261.     )
  262.    
  263.     print("host")
  264.  
  265.     --host
  266. end) -- teleports the host to the server he creates.
  267.  
  268.  
  269.  
  270. mes:SubscribeAsync(requestCode, function(message)
  271.     local data = message.Data
  272.  
  273.     local code = serverCodes[data[2]]
  274.  
  275.     print(22)
  276.  
  277.     print(code)
  278.  
  279.     if code then
  280.         mes:PublishAsync(data[1] .. requestCode, code)
  281.  
  282.         print(33)
  283.     end
  284. end) -- tells the sender if the specified code for a server exists.
  285.  
  286. mes:SubscribeAsync(requestModify, function(message)
  287.     print("server modify")
  288.    
  289.     local data = message.Data
  290.    
  291.     game.ReplicatedStorage.ServerModify:FireAllClients(data)
  292. end) -- modifies all clients servers with allocated data.
  293.  
  294. function requestCodeAvailability(player, key)
  295.     local event
  296.  
  297.     print(11)
  298.  
  299.     local datad
  300.  
  301.     event = mes:SubscribeAsync(player.UserId .. requestCode, function(message)
  302.         local data = message.Data
  303.  
  304.         print(44)
  305.  
  306.         datad = data
  307.  
  308.         event:Disconnect()
  309.     end)
  310.  
  311.     mes:PublishAsync(requestCode, {player.UserId, key})
  312.  
  313.     local timer = 0
  314.  
  315.     repeat
  316.         wait(0.01)
  317.  
  318.         timer += 0.01
  319.     until datad or timer >= 1
  320.  
  321.     if event.Connected then
  322.         event:Disconnect()
  323.     end
  324.  
  325.     print("ended")
  326.  
  327.     return datad
  328. end -- sends a request to every server ingame to check if the specified code is available for a hosts server.
  329.  
  330. game.ReplicatedStorage.RequestCode.OnServerInvoke = requestCodeAvailability
RAW Paste Data