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)