How to iterate over 10k gui instantly (in a for loop)

i want to know if its worth using collection service to save memory. if i have over 10k parts being iterated over constanly, i get lag. but if i put scripts in each instead, it also lags

this is in a render system for ui

for i, frame in pairs(game.CollectionService:GetTagged("Chunk")) do
	local playerhead = frame.Parent.Parent.DragableFrame

	local collectionservice = game:GetService("CollectionService")
	if frame:IsDescendantOf(script.Parent) then
		local id = tostring(frame.Parent.Parent.Parent.Parent.ID.Value..frame.Name) 
		local guiInset = game:GetService("GuiService"):GetGuiInset()


		local function isInScreen()
			local pos = frame.AbsolutePosition + guiInset
			return pos.X + frame.AbsoluteSize.X <= frame.Parent.Parent.AbsolutePosition.X+frame.Parent.Parent.AbsoluteSize.X*3 and pos.X >= -frame.Parent.Parent.AbsoluteSize.X
		end



		frame.Parent.Parent.Octaves.DescendantAdded:Connect(function(child)
			task.wait()
			if not game.CollectionService:HasTag(child,"notes") then return end
			if child.AbsolutePosition.X >= frame.AbsolutePosition.X and child.AbsolutePosition.X <= frame.AbsolutePosition.X+frame.AbsoluteSize.X then

				collectionservice:AddTag(child,id)
				child.assets.Chunk.Value = frame
				if isInScreen()  then
					collectionservice:AddTag(child,"regions")
					child.Visible = true
				else
					collectionservice:RemoveTag(child,"regions")
					child.Visible = false
					child.MakeGUIDraggable.Enabled = false
					child.MakeGUICopyable.Enabled = false
				end	
			end
		end)

		frame.Parent.Parent.Octaves.DescendantRemoving:Connect(function(child)
			if not game.CollectionService:HasTag(child,"notes") then return end
			if child.AbsolutePosition.X >= frame.AbsolutePosition.X and child.AbsolutePosition.X <= frame.AbsolutePosition.X+frame.AbsoluteSize.X then
				collectionservice:RemoveTag(child,id)
			end
		end)

		frame.Parent.Parent:GetPropertyChangedSignal("CanvasPosition"):Connect(function()
			wait()
			if isInScreen()  then
				if frame.rendered.Value == true then return end
				frame.rendered.Value = true
				frame.BackgroundColor3 = Color3.new(1, 1, 1)
				for i, v in pairs(collectionservice:GetTagged(id)) do
					collectionservice:AddTag(v,"regions")
					v.Visible = true
				end
			else
				if frame.rendered.Value == false then return end
				frame.rendered.Value = false
				frame.BackgroundColor3 = Color3.new(0.333333, 0.333333, 0.498039)
				for i, v in pairs(collectionservice:GetTagged(id)) do
					collectionservice:RemoveTag(v,"regions")
					v.Visible = false
					v.MakeGUIDraggable.Enabled = false
					v.MakeGUICopyable.Enabled = false
				end
			end
		end)
	end
end

for this to work

1 Like

Well its going to cause some lag to load 10k gui objects at one time. Figure out a way to have to use less.

2 Likes

You can add a variable that determines how much chunk should be rendered before the next batch is rendered. For example, after 500 chunks as loaded, use task.wait and load another 500 until all the chunk has been loaded.

1 Like

how would i use getguiincirle?

What do you mean by that? Like how to get all GUI elements that are within a circle?

nevermind, sadly its locked by corescrips

it would be nice not to have to iterate over these chunks.