Convert a Decal Id to an Asset (Image) Id via Script

Hello,

Recently, I noticed trouble trying to consistently and efficiently convert Decal Ids (generally not useful for game development now-a-days) into an Asset Id. It is for this reason that I have spent some time creating and annotating a method to do such a conversion in a modular and ordered manner.

  • If I have made a mistake or you would like to provide a suggestion for a better method, please feel free to let me know!

Both the annotated and non-annotated versions of the script can be found below:

ANNOTATED VERSION

--[[

// INTRODUCTION

PROBLEM: Decal Ids are not compatible with instances such as ImageLabels
SOLUTION: Convert the Decal Id to an Asset Id, and print the converted URL to the console or output.

// METHODOLOGY

In this script, we're using pairs() instead of ipairs() to control the order in which the assets are processed. 
While ipairs() is used for tables with integer indices, like arrays, orderedKeys in this case is a table of strings, 
so ipairs() wouldn't work because it only iterates through numeric indices in order. pairs() works for tables with string keys, 
but it doesn’t guarantee any specific order by default. However, because we’re manually defining the order of keys in orderedKeys, 
we make sure that the iteration happens in the exact sequence we want. So, even though pairs() doesn’t guarantee order, the 
explicit structure of orderedKeys does.

// ADDITIONAL INFORMATION

1. The URL produced by this script is use-ready and can be directly added as the .Image value of some instance.
2. This logic can be instead re-routed to directly alter the .Image value of an instance, however, for the purposes
   of this test, we will simply print the onverted Id.
3. A non-annotated version of this script will be published below for ease of access.
]]



-- Define InsertService.
local InsertService = game:GetService("InsertService")
-- NOTE THAT: InsertService:LoadAsset(), which will be called later in this script, is not available on the client.
--			  In the event that you would like to use this script during runtime in studio, only run it through the 
--			  developer console's 'server' run and not through studio's 'command bar'.

-- Define an asset table dictionary containing original Decal Ids for various keys.
local assetTable = {
	-- Populate this table with keys and their respective Ids.
	
	-- EXAMPLE: 
	--			a = 12345678
	--			b = 10101010
	--			c = 12120101	
}

-- Define a table with numerical indicies for processing the assets.
local orderedKeys = {
	-- Populate this table *ONLY* with its corresponding key from the assetTable and not its value.
	
	-- EXAMPLE:
	-- "a", "b", "c"
	
	-- NOTE THAT: This script has been designed to run in order of table entries. This means that
	--			  the order of these keys will directly correspond to the order of output.
}

-- Loop through each key in the ordered list and load the corresponding asset.
for _, key in pairs(orderedKeys) do
	
	-- Retrieve the Decal Id from the assetTable using the current key.
	local decalId = assetTable[key]
	
	-- Run only if decalId exists to avoid unecessary errors.
	if decalId then
		-- Load the model
		local model = InsertService:LoadAsset(decalId)
		-- Parent this model to the workspace, or directory of your choice.
		model.Parent = game.Workspace
		-- For clarity's sake, name the model according to its key.
		model.Name = key
		
		-- Output a message showing the model's name and decal texture Id. Follow this with a line of separation.
		print("Converted Id for [" .. model.Name .. "]:" .. model.Decal.Texture )
		print("-----------------------------------------------------------------")
	else
		-- If the Decal Id is not found, print a message.
		warn("Decal Id not found for key: " .. key)
	end
	
end

NON-ANNOTATED VERSION

local InsertService = game:GetService("InsertService")

local assetTable = {
	
}

local orderedKeys = {

}

for _, key in pairs(orderedKeys) do
    local decalId = assetTable[key]
    
    if decalId then
        local model = InsertService:LoadAsset(decalId)
        model.Parent = game.Workspace
        model.Name = key
        
        print("Converted Id for [" .. model.Name .. "]:" .. model.Decal.Texture )
        print("-----------------------------------------------------------------")
    else
        warn("Decal Id not found for key: " .. key)
    end
end

Thank you,
101waves