Looking to decrease the runtime of my scripts

Title is quite self explanatory.

local textureModule = {
	["Textures"] = {	
		["Blocks"] = {
			["TextureName"] = 123456789,
		}	
	}	
}

return textureModule

The above module has roughly 200 lines originally of ids, indexed by the name I’ve given them for access.

metaBlocks = game.ReplicatedFirst.MetaBlocks
blocks = game.ReplicatedFirst.Blocks
textureConfig = require(script.Parent.TextureConfig)
textures = textureConfig.Textures
blockTextures = textures.Blocks
local MarketplaceService = game:GetService("MarketplaceService")
remoteFunction = game.ReplicatedStorage:WaitForChild("CallInsertService")

function applySameTextureAround(blockname, decalid) 
	
	local id = remoteFunction:InvokeServer(decalid)
	
	if metaBlocks:FindFirstChild(blockname) then
		
		local metaBlock = metaBlocks[blockname]
		
		if metaBlock:IsA("MeshPart") then
			metaBlock.TextureID = id
		end
		
		if metaBlock:FindFirstChildWhichIsA("Texture") or metaBlock:FindFirstChildWhichIsA("Decal")  then
			
			for i, v in metaBlock:GetChildren() do
				if v:IsA("Texture") or v:IsA("Decal") then
					v.Texture = id
				end
			end
			
		end

	end
	
	local block = blocks[blockname]
	
	if block:IsA("MeshPart") then
		block.TextureID = id
	end
	
	if block:FindFirstChildWhichIsA("Texture") or block:FindFirstChildWhichIsA("Decal") then
		
		for i, v in block:GetChildren() do
			if v:IsA("Texture") or v:IsA("Decal") then
				v.Texture = id
			end
		end
		
	end
	
end

function applyTextureDifferently(blockname, topDecalid, bottomDecalid, frontDecalid, backDecalid, rightDecalid, leftDecalid)
	
	local top = remoteFunction:InvokeServer(topDecalid)
	local bottom = remoteFunction:InvokeServer(bottomDecalid)
	local front = remoteFunction:InvokeServer(frontDecalid)
	local back = remoteFunction:InvokeServer(backDecalid)
	local right = remoteFunction:InvokeServer(rightDecalid)
	local left = remoteFunction:InvokeServer(leftDecalid)

	if metaBlocks:FindFirstChild(blockname) then
		
		local metaBlock = metaBlocks[blockname]

		if not metaBlock:FindFirstChildWhichIsA("Texture") or metaBlock:FindFirstChildWhichIsA("Decal")  then
			warn("Did not find textures inside a block that should have them.")
			return
		end
		
		metaBlock.T.Texture = top
		metaBlock.Bottom.Texture = bottom
		metaBlock.R.Texture = right
		metaBlock.L.Texture = left
		metaBlock.F.Texture =  front 
		metaBlock.B.Texture = back
		
	end

	local block = blocks[blockname]

	if  block:FindFirstChildWhichIsA("Texture") or block:FindFirstChildWhichIsA("Decal")  then
		block.T.Texture = top
		block.Bottom.Texture = bottom
		block.R.Texture = right
		block.L.Texture = left
		block.F.Texture =  front 
		block.B.Texture = back
	else
		warn("could not find textures inside ".. block.Name )
		return
	end



end

function apply2DTexture(blockname, decalid)
	print(decalid)
	local id = remoteFunction:InvokeServer(decalid)
	
	if metaBlocks:FindFirstChild(blockname) then

		local metaBlock = metaBlocks[blockname]

		if metaBlock:IsA("MeshPart") then
			metaBlock.TextureID = id
		end

		if metaBlock:FindFirstChildWhichIsA("Texture") or metaBlock:FindFirstChildWhichIsA("Decal")  then

			for i, v in metaBlock:GetChildren() do
				if v:IsA("Texture") or v:IsA("Decal") then
					v.Texture = id
				end
			end

		end

	end

	local block = blocks[blockname]

	if block:FindFirstChildWhichIsA("Texture") or block:FindFirstChildWhichIsA("Decal") then

		for i, v in block:GetChildren() do
			if v:IsA("Texture") or v:IsA("Decal") then
				v.Texture = id
			end
		end

	end
	
end

The above script creates the functions I use to apply the textures to the blocks. Inside of the same script, I have around 150 lines calling these functions.

local InsertService = game:GetService("InsertService")
local MarketplaceService = game:GetService("MarketplaceService")
remoteFunction = game.ReplicatedStorage:WaitForChild("CallInsertService")
_G.texturesLoadedNum = 0


function getImageIdFromDecal(plr, decalId) 
	local assetInfo = MarketplaceService:GetProductInfo(decalId, Enum.InfoType.Asset)
	if assetInfo.AssetTypeId == 1 then
		--warn(decalId.. " was already an image.")
		_G.texturesLoadedNum += 1
		print(_G.texturesLoadedNum)

		return "http://www.roblox.com/asset/?id=" .. decalId
	end
	local decal = InsertService:LoadAsset(decalId):FindFirstChildWhichIsA("Decal")
	if decal == nil then
		warn(decalId.." had an error loading. AssetTypeId: ".. assetInfo.AssetTypeId)
		return ""
	end
	_G.texturesLoadedNum += 1
	print(_G.texturesLoadedNum)
	return decal.Texture
end


remoteFunction.OnServerInvoke = getImageIdFromDecal

--remoteFunction.OnServerInvoke = _G.texturesLoadedNum

game.ReplicatedStorage.TextureUpdateFinished.OnServerEvent:Connect(function(plr)
	
	game.ReplicatedStorage.TextureUpdateFinished:FireClient(plr)
	
end)

This above script calls the input service to convert decals to images. It cannot be called in a localscript, so I needed to call it in a remote function on a server script.

I can give further clarification if needed.

Currently, it takes around 20-30 seconds for just these scripts to finish running. I want to decrease the runtime, and I want to know if there are ways to do this.

Why don’t you just have the items already loaded?

The module is so that I can change these textures if need be pretty quickly, for seasonal things or just updating them etc.

1 Like

??? just have them in replicated storage. It’s not even that many decals

Its about 200 and counting, and I much rather have them in text format. I plan to change many of these textures every few weeks.