Help with slow cloning of image buttons

Problem: Slow cloning images from inventory

What I need:


Apologies for un-edited video
What I have:

Apologies for un-edited video

Tried: Reducing customization of cloned TextButton
Idea: Use an API of my own to get the marketplace info

Script:

local button = script.Parent
local inventoryFrame = script.Parent.Parent.Parent.InventoryFrame
local accessoryFrame = script.Parent.Parent.Parent.AccessoryFrame
local avatarService = game:GetService("AvatarEditorService")
local marketPlaceService = game:GetService("MarketplaceService")
local marketplace = game:GetService("MarketplaceService")
local sortByButton = script.Parent.Parent.Parent.SwitchFilterTypeInventory
local count = 0

avatarService:PromptAllowInventoryReadAccess()
local result = avatarService.PromptAllowInventoryReadAccessCompleted:Wait()

local function creation(assetTypes)

	for _,UIObject in pairs(inventoryFrame:GetChildren()) do
		if UIObject then
			if UIObject:IsA("ImageButton") then
				UIObject:Destroy()
			end
		end
	end
	
	local pagesObject = avatarService:GetInventory(assetTypes)
	local currentPage = pagesObject:GetCurrentPage()
	for _,accessory in pairs(currentPage) do
		local item = accessory

		local accessoryInfo = marketplace:GetProductInfo(accessory.AssetId)

		local accessoryName = accessoryInfo.Name
		local accessoryId = accessory.AssetId
		local accessoryType = accessory.AccessoryType

		local infoTable = {
			Name = accessoryName,
			Id = accessoryId,
			Type = accessoryType,
		}

		local info = marketPlaceService:GetProductInfo(accessoryId)

		local newImageButton = game.ReplicatedStorage.CloneForAccessory:Clone()
		newImageButton.AssetId.Value = accessoryId
		newImageButton.AssetType.Value = tostring(infoTable.Type)
		newImageButton.Image = "rbxthumb://type=Asset&id=" .. accessoryId .. "&w=420&h=420" 
		newImageButton.Parent = script.Parent.Parent.Parent.InventoryFrame
		newImageButton.Name = accessoryName .. "_Accessory"
		newImageButton.BackgroundTransparency = 0.5
		newImageButton.BackgroundColor3 = Color3.fromRGB(255,255,255)
		newImageButton.BorderSizePixel = 0
		newImageButton.Size = UDim2.new(0,0)



		local limitedTag = newImageButton.IsLimited
		local verifiedTag = newImageButton.IsVerified

		if info.ProductType == "Collectible Item" or info.IsLimited == true or info.IsLimitedUnique == true then
			limitedTag.Visible = true
		end

		if info.Creator.HasVerifiedBadge == true then
			verifiedTag.Visible = true
		end

		print(infoTable)

	end
end

if result == Enum.AvatarPromptResult.Success then
	local assetTypes = {
		Enum.AvatarAssetType.BackAccessory,
		Enum.AvatarAssetType.ShoulderAccessory,
		Enum.AvatarAssetType.WaistAccessory,
		Enum.AvatarAssetType.Hat,
		Enum.AvatarAssetType.Face,
		Enum.AvatarAssetType.FaceAccessory,
		Enum.AvatarAssetType.HairAccessory,
		Enum.AvatarAssetType.NeckAccessory,
	}
	creation(assetTypes)
	
end

sortByButton.MouseButton1Click:Connect(function()
	if count == 0 then
		count = count + 1
		sortByButton.Text = "Sort By: All"
		
		local assetTypes = {
			Enum.AvatarAssetType.BackAccessory,
			Enum.AvatarAssetType.ShoulderAccessory,
			Enum.AvatarAssetType.WaistAccessory,
			Enum.AvatarAssetType.Hat,
			Enum.AvatarAssetType.Face,
			Enum.AvatarAssetType.FaceAccessory,
			Enum.AvatarAssetType.HairAccessory,
			Enum.AvatarAssetType.NeckAccessory,
		}
	
		creation(assetTypes)
	elseif count == 1 then
		count = count + 1
		sortByButton.Text = "Sort By: Hat"
		
		local assetTypes = {
			Enum.AvatarAssetType.Hat,
		}

		creation(assetTypes)
		count = 0
	else
		count = 0
	end
end)

button.MouseButton1Click:Connect(function()
	accessoryFrame.Visible = false
	inventoryFrame.Visible = true
	sortByButton.Visible = true
	inventoryFrame.Size = UDim2.new(0.603, 0,0.684, 0)
end)

What Im cloning:
image

Solution:
Wrap the loop inside a task.spawn() function.

local function creation(assetTypes)
	for _,UIObject in pairs(inventoryFrame:GetChildren()) do
		if UIObject and UIObject:IsA("ImageButton") then
			UIObject:Destroy()
		end
	end

	local pagesObject = avatarService:GetInventory(assetTypes)
	pagesObject:AdvanceToNextPageAsync() 	
	local currentPage = pagesObject:GetCurrentPage()

	for _,accessory in pairs(currentPage) do
		spawn(function()
			local item = accessory
			local accessoryInfo = marketplace:GetProductInfo(accessory.AssetId)
			local accessoryName = accessoryInfo.Name
			local accessoryId = accessory.AssetId
			local accessoryType = accessory.AccessoryType
			local info = marketPlaceService:GetProductInfo(accessoryId)

			local newImageButton = game.ReplicatedStorage.CloneForAccessory:Clone()
			newImageButton.AssetId.Value = accessoryId
			newImageButton.AssetType.Value = tostring(accessoryType)
			newImageButton.Image = "rbxthumb://type=Asset&id=" .. accessoryId .. "&w=420&h=420" 
			newImageButton.Parent = inventoryFrame
			newImageButton.Name = accessoryName .. "_Accessory"

			local limitedTag = newImageButton.IsLimited
			local verifiedTag = newImageButton.IsVerified

			if info.ProductType == "Collectible Item" or info.IsLimited == true or info.IsLimitedUnique == true then
				limitedTag.Visible = true
			end

			if info.Creator.HasVerifiedBadge == true then
				verifiedTag.Visible = true
			end
		end)
	end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.