Looping through big folders lags the game out significantly?

So, I have this script where when I run an event, it finds a specific item in a folder and if the item’s name is the same as the value, then it adds it into a viewport frame. Well, it seems that looping through big folders (as in folders with alot of children inside) lags the game REALLY BADLY. I have considered just simply iterating through the folder and checking if the item and value are the same, but alas, still lags the game. How would I stop this?


game.ReplicatedStorage.Summon1.Event:Connect(function(value)
	script.Parent.Parent.Tower.Text = value
	script.Parent.Parent.Tier.Text = checkTier(value)
	script.Parent.Parent.Size = UDim2.new(0.01, 0, 0.01, 0)
	script.Parent.Parent.Visible = true
	if not game:GetService("UserInputService").TouchEnabled then
		script.Parent.Parent:TweenSize(UDim2.new(0.256, 0, 0.581, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Back, 0.5, true)
	else
		script.Parent.Parent:TweenSize(UDim2.new(0.256, 0, 0.432, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Back, 0.5, true)
	end
	for i,tower in pairs(allUnits) do
		if value == tower then
			local canFindTower = workspace.ViewportFrames.SummonedUI:FindFirstChild(value)
			if canFindTower then
				local clone = canFindTower:Clone()
				clone.Parent = script.Parent.WorldModel
				game:GetService("Debris"):AddItem(clone, 4)
			end
		end
	end
	if not game:GetService("UserInputService").TouchEnabled then
		script.Parent.Parent.Size = UDim2.new(0.256, 0, 0.581, 0)
	else
		script.Parent.Parent.Size = UDim2.new(0.256, 0, 0.432, 0)
	end
	wait(3)
	if script.Parent.Parent.Visible then
		open = false
		script.Parent.Parent:TweenSize(UDim2.new(0.01, 0, 0.01, 0), Enum.EasingDirection.In, Enum.EasingStyle.Back, 0.5, false)
		wait(0.5)
		script.Parent.Parent.Visible = false
	end
end)

Could you show more code? Like how you make the allUnits table? The whole script would be appreciated.

local allUnits = {
	"Cameraman",
	"Speakerman",
	"Large Speakerman",
	"Large Cameraman",
	"Speaker Helicopter",
	"TV Man",
	"Medic Cameraman",
	"Camera Airship",
	"Speaker Airship",
	"Large TV Man",
	"Plunger Cameraman",
	"Titan Cameraman",
	"Dark Speakerman",
	"Titan Speakerman",
	"Laser Cameraman Car",
	"Detonator Cameraman",
	"Demo Speakerman",
	"Titan TV Man",
	"Upgraded Titan Cameraman",
	"Upgraded Titan Speakerman",
	"Titan Cinema Man",
}

local Basic = {
	"Basic",
	"Cameraman"
}

local Uncommon = {
	"Uncommon",
	"Speakerman"
}

local Rare = {
	"Rare",
	"Large Speakerman",
	"Large Cameraman",
	"Speaker Helicopter",
	"TV Man"
}

local Epic = {
	"Epic",
	"Medic Cameraman",
	"Camera Airship",
	"Speaker Airship",
	"Large TV Man"
}

local Legendary = {
	"Legendary",
	"Plunger Cameraman",
	"Titan Cameraman",
	"Dark Speakerman",
	"Titan Speakerman",
	"Laser Cameraman Car",
	"Detonator Cameraman",
	"Demo Speakerman",
	"Titan TV Man"
}

local Mythic = {
	"Mythic",
	"Upgraded Titan Cameraman",
	"Upgraded Titan Speakerman",
	"Titan Cinema Man"
}

local function checkTier(value)
	for i, v in pairs(Basic) do
		if value == v then
			return Basic[1]
		end
	end
	for i, v in pairs(Uncommon) do
		if value == v then
			return Uncommon[1]
		end
	end
	for i, v in pairs(Rare) do
		if value == v then
			return Rare[1]
		end
	end
	for i, v in pairs(Epic) do
		if value == v then
			return Epic[1]
		end
	end
	for i, v in pairs(Legendary) do
		if value == v then
			return Legendary[1]
		end
	end
	for i, v in pairs(Mythic) do
		if value == v then
			return Mythic[1]
		end
	end
end

game.ReplicatedStorage.Summon1.Event:Connect(function(value)
	script.Parent.Parent.Tower.Text = value
	script.Parent.Parent.Tier.Text = checkTier(value)
	script.Parent.Parent.Size = UDim2.new(0.01, 0, 0.01, 0)
	script.Parent.Parent.Visible = true
	if not game:GetService("UserInputService").TouchEnabled then
		script.Parent.Parent:TweenSize(UDim2.new(0.256, 0, 0.581, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Back, 0.5, true)
	else
		script.Parent.Parent:TweenSize(UDim2.new(0.256, 0, 0.432, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Back, 0.5, true)
	end
	for i,tower in pairs(allUnits) do
		if value == tower then
			local canFindTower = workspace.ViewportFrames.SummonedUI:FindFirstChild(tower)
			if canFindTower then
				local clone = canFindTower:Clone()
				clone.Parent = script.Parent.WorldModel
				game:GetService("Debris"):AddItem(clone, 4)
			end
		end
	end
	if not game:GetService("UserInputService").TouchEnabled then
		script.Parent.Parent.Size = UDim2.new(0.256, 0, 0.581, 0)
	else
		script.Parent.Parent.Size = UDim2.new(0.256, 0, 0.432, 0)
	end
	wait(3)
	if script.Parent.Parent.Visible then
		open = false
		script.Parent.Parent:TweenSize(UDim2.new(0.01, 0, 0.01, 0), Enum.EasingDirection.In, Enum.EasingStyle.Back, 0.5, false)
		wait(0.5)
		script.Parent.Parent.Visible = false
	end
end)

Do you even need to loop over the table?

Code:

game.ReplicatedStorage.Summon1.Event:Connect(function(value)
	script.Parent.Parent.Tower.Text = value
	script.Parent.Parent.Tier.Text = checkTier(value)
	script.Parent.Parent.Size = UDim2.new(0.01, 0, 0.01, 0)
	script.Parent.Parent.Visible = true
	if not game:GetService("UserInputService").TouchEnabled then
		script.Parent.Parent:TweenSize(UDim2.new(0.256, 0, 0.581, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Back, 0.5, true)
	else
		script.Parent.Parent:TweenSize(UDim2.new(0.256, 0, 0.432, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Back, 0.5, true)
	end
	local canFindTower = workspace.ViewportFrames.SummonedUI:FindFirstChild(value)
	if canFindTower then
		local clone = canFindTower:Clone()
		clone.Parent = script.Parent.WorldModel
		game:GetService("Debris"):AddItem(clone, 4)
	end
	if not game:GetService("UserInputService").TouchEnabled then
		script.Parent.Parent.Size = UDim2.new(0.256, 0, 0.581, 0)
	else
		script.Parent.Parent.Size = UDim2.new(0.256, 0, 0.432, 0)
	end
	wait(3)
	if script.Parent.Parent.Visible then
		open = false
		script.Parent.Parent:TweenSize(UDim2.new(0.01, 0, 0.01, 0), Enum.EasingDirection.In, Enum.EasingStyle.Back, 0.5, false)
		wait(0.5)
		script.Parent.Parent.Visible = false
	end
end)

I tried that, but it still lags for some reason. I know its this specific script because I disabled it and it didnt lag.