I feel as if I have too many threads running in this

If I could get some feedback on how I canchange this so it doesn’t run so many threads at once.


  • The code is just the LocalPlayer’s framework (Guis etc.)
  • I have considered dialing down on the amount of threads I open
  • Dial down on the threads so a memory leak is less likely to occur
--	// Name: Framework.lua
--	// Written By: Ivan Ivanovich Gregrovnich
--	// Description: Manages all LocalPlayer events, including ui and Function firing

--	// Remove un-wanted Ui elements and disabled character loading
game:GetService("StarterGui"):SetCoreGuiEnabled("PlayerList", false)
game:GetService("StarterGui"):SetCoreGuiEnabled("Backpack", false)
game:GetService("StarterGui"):SetCoreGuiEnabled("EmotesMenu", false)
--	// Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local GuiService = game:GetService("GuiService")
local MarketplaceService = game:GetService("MarketplaceService")
local RunService = game:GetService("RunService")
--	// Player
local Player = Players.LocalPlayer
local PlayerScripts = Player:WaitForChild("PlayerScripts")
local PlayerGui = Player:WaitForChild("PlayerGui")
--	// Screen Guis
local MainGuiPC = PlayerGui:WaitForChild("MainGuiPC")
local MainGuiMobile = PlayerGui:WaitForChild("MainGuiMobile")
local SubGuiPC = PlayerGui:WaitForChild("SubGuiPC")
local SubGuiMobile = PlayerGui:WaitForChild("SubGuiMobile")
--	// Frames
-- 	////// SubGuiPC:
local PlayerList = SubGuiPC["PlayerList"]
local PlayerListMobile = SubGuiMobile["PlayerList"]
local Slots = PlayerList["OffsetFrame"].PlayerScrollList.SizeOffsetFrame
local SlotsMobile = PlayerListMobile["OffsetFrame"].PlayerScrollList.SizeOffsetFrame
local MobileTemplate = PlayerListMobile["Template"]
local Template = PlayerList["Template"]
--	////// MainGuiPC:
local MenuButtons = MainGuiPC["Buttons"].OffsetFrame.SizeOffsetFrame
local MobileMenuButtons = MainGuiMobile["Buttons"].OffsetFrame.SizeOffsetFrame
local PlayTypeSelection = MainGuiPC["PlayTypeSelection"]
local PlayTypeSelectionInnerMeta = PlayTypeSelection.OffsetFrame.SizeOffsetFrame.InnerMeta.OffsetFrame.SizeOffsetFrame
local PlayTypeSelectionButtons = PlayTypeSelection.OffsetFrame.SizeOffsetFrame.InnerMeta.OffsetFrame.SizeOffsetFrame.Buttons.OffsetFrame.SizeOffsetFrame
local PlayTypeSelectionInnerInnerMeta = PlayTypeSelectionInnerMeta.InnerInnerMeta.OffsetFrame.SizeOffsetFrame
local TemplateOuter = PlayTypeSelectionInnerMeta.InnerInnerMeta.TemplateOuter
local ScrollingFrameOuter = PlayTypeSelectionInnerInnerMeta.ScrollingFrame
--	// Tweens
local newtweeninfo = TweenInfo.new(.75, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local TweenInfoInOut = TweenInfo.new(0.15, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local TweenOut = TweenService:Create(PlayerList, TweenInfoInOut, {Position = UDim2.fromScale(1.5, 1)})
local TweenIn = TweenService:Create(PlayerList, TweenInfoInOut, {Position = UDim2.new(0.998, -4, 1, -4)})
local TransparencyTweenOut = TweenService:Create(MainGuiPC.TopMask, newtweeninfo, {BackgroundTransparency = 0})
local TransparencyTweenIn = TweenService:Create(MainGuiPC.TopMask, newtweeninfo, {BackgroundTransparency = 1})
local TransparencyTweenOutSplash = TweenService:Create(MainGuiPC.TopMask.Splash, newtweeninfo, {TextTransparency = 0})
local TransparencyTweenInSplash = TweenService:Create(MainGuiPC.TopMask.Splash, newtweeninfo, {TextTransparency = 1})
--	// Camera
local CurrentCamera = workspace.CurrentCamera
local CameraPart = workspace.CameraPart
--	// PlayerList Usables
local Splash = {
	"War does not determine who is right - only who is left.",
	"I dream of a better tomorrow, where chickens can cross the road and not be quested about their motives",
	"No I didn't trip, the floor looked like it needed a hug",
	"Better late than never, but never late is better",
	"I was standing in the park wondering why frisbees got bigger as they get closer. Then it hit me.",
	"When tempted to fight fire with fire, remember that the fire department generally uses water.",
	"Some cause happiness wherever they go; others whenever they go.",
	"Never underestimate the power of stupid people in large groups",
	"A successful man is one who makes more money than his wife can spend. A successful woman is one who can find such a man.",
	"Behind every great man is a woman rolling her eyes.",
	"Perfection is not attainable, but if we chase perfection we can catch excellence.",
	"An idea isn't responsible for the people who believe in it.",
	"I would like to die on Mars. Just not on impact.",
	"If women ran the world we wouldn't have wars, just intense negotiations every 28 days. [Robin Williams]",
	"Between two evils, I always pick the one I never tried before.",
	"If two wrongs don't make a right, try three.",	
	"Man cannot live by bread alone; he must have peanut butter.",
	"A pessimist is a person who has had to listen to too many optimists.",
	"All men are equal before fish.",
	"I've never been married, but I tell people I'm divorced so they won't think something is wrong with me.",
	"O Lord, help me to be pure, but not yet.",
	"Any kid will run any errand for you, if you ask at bedtime.",
	"We owe to the Middle Ages the two worst inventions of humanity - romantic love and gunpowder.",
	"Guilt: the gift that keeps on giving.",
	"The point of war is not to die for your country, but to make the noob on the other side die for his",
	"Always borrow money from a pessimist. He won't expect it back.",
	"Friendship is like peeing on yourself: everyone can see it, but only you get the warm feeling that it brings.",
	"Dogs have masters. Cats have staff.",
	"Knowledge is knowing a tomato is a fruit; wisdom is not putting it in a fruit salad.",
	"Why do people say 'no offense' right before they're about to offend you?",
	"By all means, marry. If you get a good wife, you'll become happy; if you get a bad one, you'll become a philosopher.",
	"I asked God for a bike, but I know God doesn't work that way. So I stole a bike and asked for forgiveness.",
	"The best way to lie is to tell the truth . . . carefully edited truth.",
	"Do not argue with an idiot. He will drag you down to his level and beat you with experience.",
	"The only mystery in life is why the kamikaze pilots wore helmets.",
	"Going to church doesn't make you a Christian any more than standing in a garage makes you a car.",
	"A bargain is something you don't need at a price you can't resist.",
	"If you steal from one author, it's plagiarism; if you steal from many, it's research.",
	"If you think nobody cares if you're alive, try missing a couple of car payments.",
	"How is it one careless match can start a forest fire, but it takes a whole box to start a campfire?",
	"God gave us our relatives; thank God we can choose our friends.",
	"Children: You spend the first 2 years of their life teaching them to walk and talk. Then you spend the next 16 telling them to sit down and shut-up.",
	"Nothing sucks more than that moment during an argument when you realize you're wrong.",
	"By the time a man realizes that his father was right, he has a son who thinks he's wrong.",
	"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true.",
	"Women who seek to be equal with men lack ambition.",
	"When you go into court you are putting your fate into the hands of twelve people who weren't smart enough to get out of jury duty.",
	"Those people who think they know everything are a great annoyance to those of us who do.",
	"By working faithfully eight hours a day you may eventually get to be boss and work twelve hours a day.",
	"When tempted to fight fire with fire, remember that the Fire Department usually uses water.",
	"America is a country where half the money is spent buying food, and the other half is spent trying to lose weight.",
	"A bank is a place that will lend you money, if you can prove that you don't need it.",
	"The best time to give advice to your children is while they're still young enough to believe you know what you're talking about.",
	"Tell a man there are 300 billion stars in the universe and he'll believe you. Tell him a bench has wet paint on it and he'll have to touch it to be sure.",
	"The human brain is a wonderful thing. It starts working the moment you are born, and never stops until you stand up to speak in public.",
	"At every party, there are two kinds of people'those who want to go home and those who don't. The trouble is, they are usually married to each other.",
	"You love flowers, but you cut them. You love animals, but you eat them. You tell me you love me, so now I'm scared!",
	"I don't need a hair stylist, my pillow gives me a new hairstyle every morning.",
	"Don't worry if plan A fails, there are 25 more letters in the alphabet.",
	"Studying means 10% reading and 90% complaining to your friends that you have to study.",
	"If you want your wife to listen to you, then talk to another woman; she will be all ears."
}
local plr_dict = {}

--	// TeleportToReservedServerAsync (Debug):
MenuButtons.DebugFrame.TeleportToReservedServerAsync.MouseButton1Click:Connect(function()
	ReplicatedStorage.DebugFunctionsAndEvents.TeleportToReservedServerAsync:FireServer()
end)

PlayTypeSelectionButtons.JoinRandomServer.MouseButton1Click:Connect(function()
	
	if not RunService:IsStudio() then
		MainGuiPC.TopMask.Visible = true
		MainGuiPC.TopMask.Splash.Text = Splash[math.random(#Splash)]
		TransparencyTweenOutSplash:Play()
		TransparencyTweenOut:Play()
	end
	ReplicatedStorage.Events.TeleportToServerAsync:FireServer(MainGuiPC.TopMask.Splash.Text)
end)

PlayTypeSelectionButtons.ExitScreen.MouseButton1Click:Connect(function()
	ReplicatedStorage.Lobby.Parent = workspace
	MainGuiPC.Version.OffsetFrame.List.SizeOffsetFrame.Version.TextColor3 = Color3.new(0,0,0)
	MainGuiPC.TopMask.Visible = true
	MainGuiPC.TopMask.Splash.Text = Splash[math.random(#Splash)]
	TransparencyTweenOut:Play()
	TransparencyTweenOutSplash:Play()
	wait(.75)
	PlayTypeSelection.Visible = false
	MenuButtons.Visible = true
	TransparencyTweenIn:Play()
	TransparencyTweenInSplash:Play()
	wait(.75)
	MainGuiPC.TopMask.Visible = false
	game:GetService("StarterGui"):SetCoreGuiEnabled("Chat", true)
end)
--	// Play
MenuButtons.Play.MouseButton1Click:Connect(function()
	game:GetService("StarterGui"):SetCoreGuiEnabled("Chat", false)
	MainGuiPC.Version.OffsetFrame.List.SizeOffsetFrame.Version.TextColor3 = Color3.new(1,1,1)
	MainGuiPC.TopMask.Visible = true
	MainGuiPC.TopMask.Splash.Text = Splash[math.random(#Splash)]
	TransparencyTweenOut:Play()
	TransparencyTweenOutSplash:Play()
	wait(.75)
	PlayTypeSelection.Visible = true
	MenuButtons.Visible = false
	TransparencyTweenIn:Play()
	TransparencyTweenInSplash:Play()
	wait(.75)
	MainGuiPC.TopMask.Visible = false
	workspace.Lobby.Parent = ReplicatedStorage
end)
MenuButtons.Play.MouseButton2Click:Connect(function()
	MenuButtons.Play.Text = "lol."
	wait(1.5)
	MenuButtons.Play.Text = "Play"
end)
MenuButtons.PlayImageButton.MouseButton1Click:Connect(function()
	game:GetService("StarterGui"):SetCoreGuiEnabled("Chat", false)
	MainGuiPC.Version.OffsetFrame.List.SizeOffsetFrame.Version.TextColor3 = Color3.new(1,1,1)
	MainGuiPC.TopMask.Visible = true
	MainGuiPC.TopMask.Splash.Text = Splash[math.random(#Splash)]
	TransparencyTweenOut:Play()
	TransparencyTweenOutSplash:Play()
	wait(.75)
	PlayTypeSelection.Visible = true
	MenuButtons.Visible = false
	TransparencyTweenIn:Play()
	TransparencyTweenInSplash:Play()
	wait(.75)
	MainGuiPC.TopMask.Visible = false
	workspace.Lobby.Parent = ReplicatedStorage
end)
--	// Garage
MenuButtons.Garage.MouseButton1Click:Connect(function()
	print("-- // Do code")
end)
MenuButtons.Garage.MouseButton2Click:Connect(function()
	MenuButtons.Garage.Text = "lol."
	wait(1.5)
	MenuButtons.Garage.Text = "Garage"
end)
MenuButtons.GarageImageButton.MouseButton1Click:Connect(function()
	print("-- // Do code")
end)
--	// Shop
MenuButtons.Shop.MouseButton1Click:Connect(function()
	print("-- // Do code")
end)
MenuButtons.Shop.MouseButton2Click:Connect(function()
	MenuButtons.Shop.Text = "lol."
	wait(1.5)
	MenuButtons.Shop.Text = "Shop"
end)
MenuButtons.ShopImageButton.MouseButton1Click:Connect(function()
	print("-- // Do code")
end)
--	// Settings
MenuButtons.Settings.MouseButton1Click:Connect(function()
	print("-- // Do code")
end)
MenuButtons.Settings.MouseButton2Click:Connect(function()
	MenuButtons.Settings.Text = "lol."
	wait(1.5)
	MenuButtons.Settings.Text = "Settings"
end)
MenuButtons.SettingsImageButton.MouseButton1Click:Connect(function()
	print("-- // Do code")
end)
--	// PlayMobile
MobileMenuButtons.PlayImageButton.MouseButton1Click:Connect(function()
	ReplicatedStorage.Events.TeleportToServerAsync:FireServer("lol")
end)
--	// GarageMobile
MobileMenuButtons.GarageImageButton.MouseButton1Click:Connect(function()
	print("-- // Do code")
end)
--	// ShopMobile
MobileMenuButtons.ShopImageButton.MouseButton1Click:Connect(function()
	print("-- // Do code")
end)
--	// SettingsMobile
MobileMenuButtons.SettingsImageButton.MouseButton1Click:Connect(function()
	print("-- // Do code")
end)
local function PlayerAddedPC()
	local PlayerObjects = Players:GetChildren()
	for Count = 1, #PlayerObjects do
		if plr_dict[PlayerObjects[Count].UserId] == nil then
			plr_dict[PlayerObjects[Count].UserId] = 1
		    local Slot = Template:Clone()
			local plrname = Slot.ScrollingFrameClippingFrame.ScollingFrame.OffsetUndoFrame.Template.ChildrenFrame.NameFrame.BGFrame.OverlayFrame.PlayerName.PlayerName
			local plricon = Slot.ScrollingFrameClippingFrame.ScollingFrame.OffsetUndoFrame.Template.ChildrenFrame.NameFrame.BGFrame.OverlayFrame.PlayerIcon
			if PlayerObjects[Count]:IsInGroup(1200769) then
				Slot.Name = "p_" .. PlayerObjects[Count].UserId
				plrname.Text = PlayerObjects[Count].Name
				Slot.Parent = Slots
				plricon.Image = "rbxasset://textures/ui/PlayerList/AdminIcon@3x.png"
			elseif PlayerObjects[Count]:IsInGroup(2868472) then
				Slot.Name = "p_" .. PlayerObjects[Count].UserId
				plrname.Text = PlayerObjects[Count].Name
				Slot.Parent = Slots
				plricon.Image = "rbxasset://textures/ui/icon_intern-16.png"
			elseif PlayerObjects[Count].UserId == 158190828 then
				Slot.Name = "p_" .. PlayerObjects[Count].UserId
				plrname.Text = PlayerObjects[Count].Name
				Slot.Parent = Slots
				plricon.Image = "rbxasset://textures/ui/PlayerList/StarIcon@2x.png"
			elseif PlayerObjects[Count].UserId == 1653712523 then
				Slot.Name = "p_" .. PlayerObjects[Count].UserId
				plrname.Text = PlayerObjects[Count].Name
				Slot.Parent = Slots
				plricon.Image = "rbxasset://textures/ui/icon_intern-16.png"
			elseif PlayerObjects[Count].MembershipType == Enum.MembershipType.Premium then
				Slot.Name = "p_" .. PlayerObjects[Count].UserId
				plrname.Text = PlayerObjects[Count].Name
				Slot.Parent = Slots
				plricon.Image = "rbxasset://textures/ui/PlayerList/PremiumIcon@2x.png"
			elseif PlayerObjects[Count].UserId == 1710097067 then
				Slot.Name = "p_" .. PlayerObjects[Count].UserId
				plrname.Text = PlayerObjects[Count].Name
				Slot.Parent = Slots
				plricon.Image = "rbxasset://textures/ui/PlayerList/AdminIcon@3x.png"
			else
				Slot.Name = "p_" .. PlayerObjects[Count].UserId
				plrname.Text = PlayerObjects[Count].Name
				Slot.Parent = Slots
			end
		end
	end
end
local function PlayerAddedMobile()
	local PlayerObjects = Players:GetChildren()
		for Count = 1, #PlayerObjects do
		if plr_dict[PlayerObjects[Count].UserId] == nil then
			plr_dict[PlayerObjects[Count].UserId] = 1
		    local Slot = MobileTemplate:Clone()
			local plrname = Slot.ScrollingFrameClippingFrame.ScollingFrame.OffsetUndoFrame.Template.ChildrenFrame.NameFrame.BGFrame.OverlayFrame.PlayerName.PlayerName
			local plricon = Slot.ScrollingFrameClippingFrame.ScollingFrame.OffsetUndoFrame.Template.ChildrenFrame.NameFrame.BGFrame.OverlayFrame.PlayerIcon
			if PlayerObjects[Count]:IsInGroup(1200769) then
				Slot.Name = "p_" .. PlayerObjects[Count].UserId
				plrname.Text = PlayerObjects[Count].Name
				Slot.Parent = SlotsMobile
				plricon.Image = "rbxasset://textures/ui/PlayerList/AdminIcon@3x.png"
			elseif PlayerObjects[Count]:IsInGroup(2868472) then
				Slot.Name = "p_" .. PlayerObjects[Count].UserId
				plrname.Text = PlayerObjects[Count].Name
				Slot.Parent = SlotsMobile
				plricon.Image = "rbxasset://textures/ui/icon_intern-16.png"
			elseif PlayerObjects[Count].UserId == 158190828 then
				Slot.Name = "p_" .. PlayerObjects[Count].UserId
				plrname.Text = PlayerObjects[Count].Name
				Slot.Parent = SlotsMobile
				plricon.Image = "rbxasset://textures/ui/PlayerList/StarIcon@2x.png"
			elseif PlayerObjects[Count].UserId == 1653712523 then
				Slot.Name = "p_" .. PlayerObjects[Count].UserId
				plrname.Text = PlayerObjects[Count].Name
				Slot.Parent = SlotsMobile
				plricon.Image = "rbxasset://textures/ui/icon_intern-16.png"
			elseif PlayerObjects[Count].MembershipType == Enum.MembershipType.Premium then
				Slot.Name = "p_" .. PlayerObjects[Count].UserId
				plrname.Text = PlayerObjects[Count].Name
				Slot.Parent = SlotsMobile
				plricon.Image = "rbxasset://textures/ui/PlayerList/PremiumIcon@2x.png"
			elseif PlayerObjects[Count].UserId == 1710097067 then
				Slot.Name = "p_" .. PlayerObjects[Count].UserId
				plrname.Text = PlayerObjects[Count].Name
				Slot.Parent = SlotsMobile
				plricon.Image = "rbxasset://textures/ui/PlayerList/AdminIcon@3x.png"
			else
				Slot.Name = "p_" .. PlayerObjects[Count].UserId
				plrname.Text = PlayerObjects[Count].Name
				Slot.Parent = SlotsMobile
			end
		end
	end
end
Players.PlayerAdded:connect(function()
	if UserInputService.TouchEnabled and not UserInputService.KeyboardEnabled and not UserInputService.MouseEnabled and not UserInputService.GamepadEnabled and not GuiService:IsTenFootInterface() then
		SubGuiMobile.Enabled = true
		MainGuiMobile.Enabled = true
		MainGuiPC.Enabled = false
		SubGuiPC.Enabled = false
		PlayerAddedMobile()
	elseif not UserInputService.TouchEnabled and UserInputService.KeyboardEnabled and UserInputService.MouseEnabled then
		
		SubGuiMobile.Enabled = false
		MainGuiMobile.Enabled = false
		MainGuiPC.Enabled = true
		SubGuiPC.Enabled = true
		PlayerAddedPC()
	end
end)
Players.PlayerRemoving:connect(function(Plr)
	if Plr then
		plr_dict[Plr.UserId] = nil
		local Slot = Slots:FindFirstChild("p_" .. Plr.UserId)
		coroutine.wrap(function(Slot) Slot:Dstory() end)
	end
end)
coroutine.resume(coroutine.create(function(Plr)
	do
		if UserInputService.TouchEnabled and not UserInputService.KeyboardEnabled and not UserInputService.MouseEnabled and not UserInputService.GamepadEnabled and not GuiService:IsTenFootInterface() then
			SubGuiMobile.Enabled = true
			MainGuiMobile.Enabled = true
			MainGuiPC.Enabled = false
			SubGuiPC.Enabled = false
			PlayerAddedMobile()
		elseif not UserInputService.TouchEnabled and UserInputService.KeyboardEnabled and UserInputService.MouseEnabled then
			SubGuiMobile.Enabled = false
			MainGuiMobile.Enabled = false
			MainGuiPC.Enabled = true
			SubGuiPC.Enabled = true
			PlayerAddedPC()
		end
	end
	do
		if Plr then
			plr_dict[Plr.UserId] = nil
			local Slot = Slots:FindFirstChild("p_" .. Plr.UserId)
			coroutine.wrap(function(Slot) Slot:Destory() end)
		end
	end
	local Count = 0
	for k, _ in pairs(plr_dict) do
		Count = Count + 1
		local Slot = Slots:FindFirstChild(k)
	end
end))
coroutine.resume(coroutine.create(function()
	repeat
		wait()
		CurrentCamera.CameraType = Enum.CameraType.Scriptable
	until CurrentCamera.CameraType
	CurrentCamera.CFrame = CameraPart.CFrame
end))
coroutine.resume(coroutine.create(function()
	while wait() do
		local keys = UserInputService:GetKeysPressed()
		for _, key in pairs(keys) do
			if (key.KeyCode == Enum.KeyCode.Tab) then
				if PlayerList.Visible == true then
					TweenOut:Play()
					wait(.15)
					PlayerList.Visible = false
				elseif PlayerList.Visible == false then
					PlayerList.Visible = true
					TweenIn:Play()
				end
			end
			repeat wait() until UserInputService:IsKeyDown(Enum.KeyCode.Tab) == false
		end
	end
end))
local function ServerListParse(data)
	local players = {}
	
	if ScrollingFrameOuter:FindFirstChild("s_"..data.Data.ServerId) == nil then
		for i,v in pairs(data.Data.Players) do
			players[i] = v
		end
		local TempClone = TemplateOuter:Clone()
		for i = 1, #players do
			local PlayerName = Instance.new("TextLabel", TempClone.OffsetFrame.SizeOffsetFrame.ScrollingFrame)
			PlayerName.Name = "p_"..players[i]
			PlayerName.AnchorPoint = Vector2.new(0.5, 0.5)
			PlayerName.Size = UDim2.new(1, 0, 0, 45)
			PlayerName.Text = players[i]
			PlayerName.BackgroundColor3 = Color3.new(11/255, 11/255, 11/255)
			PlayerName.TextScaled = true
			PlayerName.Font = Enum.Font.SourceSansBold
			PlayerName.BorderColor3 = Color3.new(12/255, 19/255, 25/255)
			PlayerName.TextColor3 = Color3.new(1,1,1)
		end
		TempClone.Name = "s_"..data.Data.ServerId
		TempClone.Parent = ScrollingFrameOuter
		TempClone.OffsetFrame.SizeOffsetFrame.ServerId.Text = string.sub(data.Data.ServerId, 1, 8).." :^)"
		TempClone.OffsetFrame.SizeOffsetFrame.JoinServer.MouseButton1Click:Connect(function()
			MainGuiPC.TopMask.Visible = true
			MainGuiPC.TopMask.Splash.Text = Splash[math.random(#Splash)]
			TransparencyTweenOutSplash:Play()
			TransparencyTweenOut:Play()
			ReplicatedStorage.Events.TeleportToSelectionServerAsync:FireServer(data.Data.ServerId, MainGuiPC.TopMask.Splash.Text)
		end)
		wait(5.25)
		TempClone:Destroy()
	else
		for i,v in pairs(data.Data.Players) do
			players[i] = v
		end
		ScrollingFrameOuter:FindFirstChild("s_"..data.Data.ServerId):Destroy()
		local TempClone = TemplateOuter:Clone()
		for i = 1, #players do
			local PlayerName = Instance.new("TextLabel", TempClone.OffsetFrame.SizeOffsetFrame.ScrollingFrame)
			PlayerName.Name = "p_"..players[i]
			PlayerName.AnchorPoint = Vector2.new(0.5, 0.5)
			PlayerName.Size = UDim2.new(1, 0, 0, 45)
			PlayerName.Text = players[i]
			PlayerName.BackgroundColor3 = Color3.new(11/255, 11/255, 11/255)
			PlayerName.TextScaled = true
			PlayerName.Font = Enum.Font.SourceSansBold
			PlayerName.BorderColor3 = Color3.new(12/255, 19/255, 25/255)
			PlayerName.TextColor3 = Color3.new(1,1,1)
		end
		TempClone.Name = "s_"..data.Data.ServerId
		TempClone.Parent = ScrollingFrameOuter
		TempClone.OffsetFrame.SizeOffsetFrame.ServerId.Text = string.sub(data.Data.ServerId, 1, 8).." :^)"
		TempClone.OffsetFrame.SizeOffsetFrame.JoinServer.MouseButton1Click:Connect(function()
			MainGuiPC.TopMask.Visible = true
			MainGuiPC.TopMask.Splash.Text = Splash[math.random(#Splash)]
			TransparencyTweenOutSplash:Play()
			TransparencyTweenOut:Play()
			ReplicatedStorage.Events.TeleportToSelectionServerAsync:FireServer(data.Data.ServerId, MainGuiPC.TopMask.Splash.Text)
		end)
		wait(5.25)
		TempClone:Destroy()
	end
end
coroutine.resume(coroutine.create(function()
	if PlayTypeSelectionInnerInnerMeta.Visible == true then
		PlayTypeSelectionInnerMeta.TitleCard.Text = "Select Server"
	end
end))
coroutine.resume(coroutine.create(function()
	if MainGuiPC.Version.OffsetFrame.List.SizeOffsetFrame.Version.Text ~= "v"..string.sub(MarketplaceService:GetProductInfo(game.PlaceId).Name, 2, 8).."tPh" then
		MainGuiPC.Version.OffsetFrame.List.SizeOffsetFrame.Version.Text = "v"..string.sub(MarketplaceService:GetProductInfo(game.PlaceId).Name, 2, 8).."tPh"
		--[[
		pPlS -- PrePlanStage(lol ideas)
		PlS -- PlanStage
		PrS	-- ProductionStage
		tPh	-- testPhase
		pA	-- PreAlpha
		A	-- Alpha
		pPrS -- PostProductionStage
		--]]
	end
end))

coroutine.resume(coroutine.create(function()
	while wait() do
		for _,v in pairs(ScrollingFrameOuter:GetChildren()) do
			if #ScrollingFrameOuter:GetChildren() == 1 then
				PlayTypeSelectionInnerInnerMeta.DisplayLayer.Visible = true
			elseif #ScrollingFrameOuter:GetChildren() > 1 then
				PlayTypeSelectionInnerInnerMeta.DisplayLayer.Visible = false
			end
		end
	end
end))

ReplicatedStorage.Events.ApplyDebugAsync.OnClientEvent:Connect(function(isEnabled)
	MenuButtons.DebugFrame.Visible = isEnabled
end)
ReplicatedStorage.Events.GetPlayerNameAsync:FireServer(Players.LocalPlayer)
ReplicatedStorage.Events.ServerListUpdate.OnClientEvent:Connect(ServerListParse)

-nsg

3 Likes

If you’re concerned about thread count, you should condense all of the code wrapped in coroutine.resume(coroutine.create(function() end)) to be in a single coroutine.wrap(function() end)(). If you have multiple while loops that need to run synchronously, just try and condense what each of them do into a single loop at the end of the aforementioned coroutine.wrap(function() end)().

Example:

coroutine.resume(coroutine.create(function()
    while wait() do
        print("hi")
    end
end))

coroutine.resume(coroutine.create(function()
	if PlayTypeSelectionInnerInnerMeta.Visible == true then
		PlayTypeSelectionInnerMeta.TitleCard.Text = "Select Server"
	end
end))

coroutine.resume(coroutine.create(function()
	if MainGuiPC.Version.OffsetFrame.List.SizeOffsetFrame.Version.Text ~= "v"..string.sub(MarketplaceService:GetProductInfo(game.PlaceId).Name, 2, 8).."tPh" then
		MainGuiPC.Version.OffsetFrame.List.SizeOffsetFrame.Version.Text = "v"..string.sub(MarketplaceService:GetProductInfo(game.PlaceId).Name, 2, 8).."tPh"
	end
end))

coroutine.resume(coroutine.create(function()
	while wait() do
		for _,v in pairs(ScrollingFrameOuter:GetChildren()) do
			if #ScrollingFrameOuter:GetChildren() == 1 then
				PlayTypeSelectionInnerInnerMeta.DisplayLayer.Visible = true
			elseif #ScrollingFrameOuter:GetChildren() > 1 then
				PlayTypeSelectionInnerInnerMeta.DisplayLayer.Visible = false
			end
		end
	end
end))

should become:

coroutine.wrap(function()
    if PlayTypeSelectionInnerInnerMeta.Visible == true then
		PlayTypeSelectionInnerMeta.TitleCard.Text = "Select Server"
	end
    if MainGuiPC.Version.OffsetFrame.List.SizeOffsetFrame.Version.Text ~= "v"..string.sub(MarketplaceService:GetProductInfo(game.PlaceId).Name, 2, 8).."tPh" then
		MainGuiPC.Version.OffsetFrame.List.SizeOffsetFrame.Version.Text = "v"..string.sub(MarketplaceService:GetProductInfo(game.PlaceId).Name, 2, 8).."tPh"
	end

    -- Make sure the loop is at the end to not hold up the other code
	while wait() do
        print("hi") -- Just tack it on somewhere that makes sense
		for _,v in pairs(ScrollingFrameOuter:GetChildren()) do
			if #ScrollingFrameOuter:GetChildren() == 1 then
				PlayTypeSelectionInnerInnerMeta.DisplayLayer.Visible = true
			elseif #ScrollingFrameOuter:GetChildren() > 1 then
				PlayTypeSelectionInnerInnerMeta.DisplayLayer.Visible = false
			end
		end
	end
end)() -- The "()" is required when using .wrap() since it returns a function
3 Likes