Handling global lobbies

Im attempting to make a global lobbies sytem for my game but the issue is that its too messy and very hard to make work and i cant understand how to make it more efficent and cleaner this is my very very messy script. Can anyone point to a better way of making each server have the same dungeon lobbies and allow for anyone in any server to join anyones lobby?

local OldTable
while true do
   taks.wait(5)
   local Sa, Era = pcall(function()
   		local Table = DungeonLobby:GetAsync("GlobalDungeons") --gets all lobbies from data store
		Table = HttpService:JSONDecode(Table)
		if Table ~= OldTable then --Checks if the new table is the same as the old one
			OldTable = Table
			for i,v in Player.PlayerGui.GUI.DungLobbies.DungLobbies.LobbiesFrame.AllMains:GetChildren() do
				for i,v in v.Outer.Scroll:GetChildren() do
					if not v:IsA("UIGridLayout") then
						if Table[v.Name] == nil then
							v:Destroy()-- Removes all old lobbies
						end
					end
				end
			end
			for i,v in Table do
				if v[3] == "Normal" and (not Player.PlayerGui.GUI.DungLobbies.DungLobbies.LobbiesFrame.AllMains.Normal.Outer.Scroll:FindFirstChild(v[1]) or Player.PlayerGui.GUI.DungLobbies.DungLobbies.LobbiesFrame.AllMains.Normal.Outer.Scroll[v[1]].Dungeon.Table.Value ~= HttpService:JSONEncode(v)) then -- checks if the lobbies gui is already there
					if v[5] == true then -- if dungeon lobby is sending players into the dungeon
						repeat 
							local Sa,Era = pcall(function()--attempts to teleport player if the dungeon is ready to start
								local Table = DungeonLobby:GetAsync("GlobalDungeons")
								Table = HttpService:JSONDecode(Table)
								MainDSS:SetAsync(Player.UserId.."DungeonPlayerAmount"..DataClearNumber+game.ReplicatedStorage.PlayerObjects[Player.Name].Slot.Value, {Table[PlayLobby[Player.UserId]][2],game.ReplicatedStorage.PlayerObjects[Player.Name].Tier.Value,Table[PlayLobby[Player.UserId]][3]})
								LastSave = Table
								Table[Player.UserId] = nil
								tps:TeleportToPrivateServer(18804592483, v[6], {Player})
								task.wait(5)
								DungeonLobby:SetAsync("GlobalDungeons",HttpService:JSONEncode(Table))
							end)
							print(Era)
						until Sa
					else
						if Player.PlayerGui.GUI.DungLobbies.DungLobbies.LobbiesFrame.AllMains.Normal.Outer.Scroll:FindFirstChild(v[1]) then
							Player.PlayerGui.GUI.DungLobbies.DungLobbies.LobbiesFrame.AllMains.Normal.Outer.Scroll[v[1]]:Destroy()
							Player.PlayerGui.GUI.DungLobbies.DungLobbies.LobbiesFrame.AllMains.All.Outer.Scroll[v[1]]:Destroy()
						end
						local Frame = game.ReplicatedStorage.GUIS.Dungeon:Clone()
						local ImageSize = Enum.ThumbnailSize.Size420x420
						local ImageType = Enum.ThumbnailType.HeadShot
						Frame.Dungeon.Table.Value = HttpService:JSONEncode(v)
						Frame.Name = v[1]
						local content = game.Players:GetUserThumbnailAsync(v[1], ImageType, ImageSize) 
						Frame.Dungeon.PlayerIcon.Image = content --sets host icon
						Frame.Dungeon.Type.Text = "Normal"
						Frame.Dungeon.Type.TextColor3 = Color3.new(0.447059, 0.623529, 0.8)
						Frame.Parent = Player.PlayerGui.GUI.DungLobbies.DungLobbies.LobbiesFrame.AllMains.Normal.Outer.Scroll
						Frame.Dungeon.Amount.Text = #v[4].."/"..v[2] 
						local function Joining()
							local Framas = {}
							for i,v in v[4] do
								local ImageSize = Enum.ThumbnailSize.Size420x420
								local ImageType = Enum.ThumbnailType.HeadShot
								local content = game.Players:GetUserThumbnailAsync(v[1], ImageType, ImageSize) 
								local Icon = game.ReplicatedStorage.GUIS.PlayerIcon:Clone()
								Icon.Image = content
								Icon.Rating.Text = v[2]
								table.insert(Framas,Icon)-- adds players icon to the listof play icons
							end
							for i,v in Framas do
								v.Parent = Player.PlayerGui.GUI.DungInfo.DungInfo.Players
							end
							if v[1] == Player.UserId then -- if player is the host then
								Player.PlayerGui.GUI.DungInfo.DungInfo.Start.Visible = true
								Player.PlayerGui.GUI.DungInfo.DungInfo.Back.Visible = false
								Player.PlayerGui.GUI.DungInfo.DungInfo.Play.Visible = false
								Player.PlayerGui.GUI.DungInfo.DungInfo.Visible = true
	
								Player.PlayerGui.GUI.DungInfo.DungInfo.Start.MouseButton1Down:Connect(function()
									if Player.PlayerGui.GUI.DungInfo.DungInfo.Start.Visible == true then
										repeat
											local Sa,Era = pcall(function()
												local Table = DungeonLobby:GetAsync("GlobalDungeons")
												Table = HttpService:JSONDecode(Table)
												local code = tps:ReserveServer(18804592483)
												Table[v[1]] = {v[1],v[2],v[3],v[4],true,code}
												LastSave = Table
												PlayLobby[Player.UserId] = v[1]
												DungeonLobby:SetAsync("GlobalDungeons",HttpService:JSONEncode(Table))--Save dungeon in the starting state
											end)
											task.wait()
											print(Era)
										until Sa
									end
								end)
							else
								Player.PlayerGui.GUI.DungInfo.DungInfo.Start.Visible = false
								Player.PlayerGui.GUI.DungInfo.DungInfo.Back.Visible = true
								Player.PlayerGui.GUI.DungInfo.DungInfo.Play.Visible = true
								Player.PlayerGui.GUI.DungInfo.DungInfo.Visible = true
								Player.PlayerGui.GUI.DungInfo.DungInfo.Play.MouseButton1Down:Connect(function()
									if PlayLobby[Player.UserId] == nil and Player.PlayerGui.GUI.DungInfo.DungInfo.Play.Visible == true then
										repeat
											local Sa,Era = pcall(function()
												local Table = DungeonLobby:GetAsync("GlobalDungeons")
												Table = HttpService:JSONDecode(Table)
												local Tablo = v[4]
												if Tablo == nil then
													Tablo = {{Player.UserId,game.ReplicatedStorage.PlayerObjects[Player.Name].Tier.Value}}
												else
													table.insert(Tablo,{Player.UserId,game.ReplicatedStorage.PlayerObjects[Player.Name].Tier.Value})
												end
												local Bool = false
												if #Tablo >= v[2] then -- if number of players is the same or more han the amout of players needed to start then
													Bool = true
												end
												LastSave = Table
												if Bool == true then
													local code = tps:ReserveServer(18804592483)
													Table[v[1]] = {v[1],v[2],v[3],Tablo,Bool,code}
												else
													Table[v[1]] = {v[1],v[2],v[3],Tablo,Bool}
												end
												DungeonLobby:SetAsync("GlobalDungeons",HttpService:JSONEncode(Table))-- saves dungeon in default or starting state
											end)
											task.wait()
											print(Era)
										until Sa
									end
								end)
							end
							Player.PlayerGui.GUI.DungInfo.Visible = false
							Player.PlayerGui.GUI.DungInfo.Visible = true
						end
						Frame.Dungeon.Info.MouseButton1Down:Connect(Joining)
						local Frame2 = Frame:Clone()
						Frame2.Dungeon.Info.MouseButton1Down:Connect(Joining)
						Frame2.Parent = Player.PlayerGui.GUI.DungLobbies.DungLobbies.LobbiesFrame.AllMains.All.Outer.Scroll
					end
				end
			end
			if PlayLobby[Player.UserId] ~= nil and Table[PlayLobby[Player.UserId]] == nil then -- if players lobby doesnt exist in the current table folder then
				MainDSS:SetAsync(Player.UserId.."DungeonPlayerAmount"..DataClearNumber+game.ReplicatedStorage.PlayerObjects[Player.Name].Slot.Value, {LastSave[PlayLobby[Player.UserId]][2],game.ReplicatedStorage.PlayerObjects[Player.Name].Tier.Value,LastSave[PlayLobby[Player.UserId]][3]})
				tps:TeleportToPrivateServer(18804592483, LastSave[PlayLobby[Player.UserId]][6],{Player})-- teleport player to dungeon using old data
			end
			if PlayLobby[Player.UserId] ~= nil and Table[PlayLobby[Player.UserId]][2] <= #Table[PlayLobby[Player.UserId]][4] then -- if enough players to start then
				repeat
					local Sa,Era = pcall(function()
						local Table = DungeonLobby:GetAsync("GlobalDungeons")
						Table = HttpService:JSONDecode(Table)
						local code = tps:ReserveServer(18804592483)
						Table[PlayLobby[Player.UserId]] = {Table[PlayLobby[Player.UserId]][1],Table[PlayLobby[Player.UserId]][2],Table[PlayLobby[Player.UserId]][3],Table[PlayLobby[Player.UserId]][4],true,code}
						LastSave = Table
						PlayLobby[Player.UserId] = Table[PlayLobby[Player.UserId]][1]
						DungeonLobby:SetAsync("GlobalDungeons",HttpService:JSONEncode(Table))-- save dungeon in starting state
					end)
					task.wait()
					print(Era)
				until Sa
			end
		end
	   end)
   print(Era)
end

anyone who has any ideas on what to use (other than MessagingService) please tell me

You could use MemoryStoreService

1 Like