Why does ContentProvider:PreloadAsync() require instances?

The following results in error status being printed:

ContentProvider:PreloadAsync({"rbxassetid://392838370"}, print)

You need an Instance with the ID:

local Sound = Instance.new("Sound")
Sound.SoundId = "392838370"
ContentProvider:PreloadAsync({Sound}, print)

Why should you need an Instance? Why cant you just preload a raw Asset Id? What motivated this design decision? Is this somehow to save memory, and delete the asset if the Instance is destroyed?

local function Preload(AssetList : table, Callback : any)
	local AssetsLoaded = 1
	local AssetsToLoad = #AssetList
	game:GetService("ContentProvider"):PreloadAsync(AssetList,function()
		AssetsLoaded += 1
		Callback(AssetsLoaded,AssetsToLoad)
	end)
end

This is a function that I use to preload an array of AssetId’s.
You don’t need instances.

The Callback argument is a function that’s like this:

function(Loaded,ToLoad)
	print(("%d/%d assets loaded"):format(Loaded,ToLoad))
end

I wonder as well, it was originally that it took in an array of id’s but they changed it to instances. This has always been a curious decision.

1 Like

Does putting : table after the argument have any effect on the code? Or is it just for readability or conveying intent?

readability and conveying intent

local function greaterthanfive(x : number): boolean
	return x > 5
end

x : number indicates that a number should be passed as an argument in the function
(): boolean indicates the return type of the function

additionally i learned about declaring function types since posting this:

Preload(AssetList : table, Callback : any)
Preload(AssetList : table, Callback : ((--[[argument types passed with Callback]]) -> (--[[argument types returned by Callback]]))

1 Like