Best method of getting the template ID of clothing?

local marketplace = game:GetService"MarketplaceService"
local getProductInfo = marketplace.GetProductInfo

local assetTypeIds = {2, 11, 12, 13} --T-Shirt, Shirt, Pants, Decal.
local maxTries = 5 --Maximum retries per catalog ID.
local maxDepth = 100 --Maximum depth to search for.

local function getAssetIdFromCatalogId(catalogId : number) : number
	local success, result = pcall(getProductInfo, marketplace, catalogId)
	if success then
		if result then
			if table.find(assetTypeIds, result.AssetTypeId) or (result.AssetTypeId == 21 and catalogId <= 2124343749) then --Badges with an ID below 2124343749 (before they were given their own URL directory).
				local cache, success2, result2 = table.create(100), nil, nil
				repeat
					task.wait()
					catalogId -= 1
					if result.AssetId - catalogId > maxDepth then
						return
					end
					if cache[catalogId] then
						cache[catalogId] += 1
					else
						cache[catalogId] = 1
					end
					if cache[catalogId] > maxTries then
						continue
					end
					success2, result2 = pcall(getProductInfo, marketplace, catalogId)
					if not success2 then
						warn(result2)
						catalogId += 1
					end
				until result2 and result2.Creator.CreatorType == result.Creator.CreatorType and result2.Creator.CreatorTargetId == result.Creator.CreatorTargetId
				return result2.AssetId
			end
		end
	else
		warn(result)
	end
end

local assetId = getAssetIdFromCatalogId(14417332)
print(assetId) --14417331

Here’s an implentation which works for all asset types where this behavior is observed (decals, t-shirts, shirts, pants & badges).

The following badge was used for testing purposes.
https://www.roblox.com/badges/14417332/John-Loved-of-Muses

7 Likes