ContentProvider

Show Deprecated
Not Creatable
Service
Not Replicated

Service that is used to load content, or assets, into a game.

The service's main use is to preload assets into a game. When a new asset such as a Decal or Sound is used in a game, Roblox will load the content associated with it from Roblox servers. In some cases, this can be undesirable for developers as it can lead to a delay before the content loads into the game.

With ContentProvider, developers can preload assets using the ContentProvider:PreloadAsync() function. Another useful property is ContentProvider.RequestQueueSize, which can be used to measure what proportion of assets in the request queue have been downloaded.

Code Samples

ContentProvider

1local ContentProvider = game:GetService("ContentProvider")
2
3local LOGO_ID = "rbxassetid://658743164"
4local PAGE_TURN_ID = "rbxassetid://12222076"
5
6local decal = Instance.new("Decal")
7decal.Texture = LOGO_ID
8
9local sound = Instance.new("Sound")
10sound.SoundId = PAGE_TURN_ID
11
12local assets = { decal, sound }
13
14ContentProvider:PreloadAsync(assets)
15
16print("All assets loaded.")

Summary

Properties

Used by the ContentProvider to download assets from the Roblox website.

READ ONLY
NOT REPLICATED

Gives the number of items in ContentProvider's request queue that need to be downloaded.

READ ONLY
NOT REPLICATED

Events

Methods

RegisterDefaultEncryptionKey(encryptionKey: string): void  


RegisterDefaultSessionKey(sessionKey: string): void  


RegisterEncryptedAsset(assetId: Content, encryptionKey: string): void  


RegisterSessionEncryptedAsset(contentId: Content, sessionKey: string): void  


SetBaseUrl(url: string): void  

Sets ContentProvider.BaseUrl, which is used by CoreScripts to interact with the web APIs.

UnregisterEncryptedAsset(assetId: Content): void  


PreloadAsync(contentIdList: Array, callbackFunction: function): void  YIELDS

Yields until all of the assets associated with the given Instances have loaded.

Properties

BaseUrl

Read Only
Not Replicated

Used by the ContentProvider to download assets from the Roblox website.

This URL points to a Roblox hosted website from which assets are downloaded and is pulled from the AppSettings.xml file, located in the version-hash folder.

It is possible to overwrite this property using the ContentProvider:SetBaseUrl() function in the command bar; however, this is not recommended and may cause asset loading issues.

RequestQueueSize

Read Only
Not Replicated

Gives the number of items in ContentProvider's request queue that need to be downloaded.

Items are added to the client's request queue when an asset is used for the first time or ContentProvider:PreloadAsync() is called.

Developers are advised not to use RequestQueueSize to create loading bars. This is because the queue size can both increase and decrease over time as new assets are added and downloaded. Developers looking to display loading progress should load assets one at a time (see example below).

Events

AssetFetchFailed

Parameters

assetId: Content

Methods

CalculateNumTrianglesInMeshSync

Roblox Script Security

Parameters

meshId: string

Returns

GetDetailedFailedRequests

Roblox Script Security

Returns

GetFailedRequests

Roblox Script Security

Returns

ListEncryptedAssets


Returns

RegisterDefaultEncryptionKey

void

Parameters

encryptionKey: string

Returns

void

RegisterDefaultSessionKey

void

Parameters

sessionKey: string

Returns

void

RegisterEncryptedAsset

void

Parameters

assetId: Content
encryptionKey: string

Returns

void

RegisterSessionEncryptedAsset

void

Parameters

contentId: Content
sessionKey: string

Returns

void

SetBaseUrl

void
Local User Security

Sets ContentProvider.BaseUrl, which is used by CoreScripts to interact with the web APIs.

This function is restricted and intended for internal Roblox scripts. Although it can be used from the command line in Roblox Studio, developers are advised not to do this as it may prevent assets from loading.

Parameters

url: string

The base URL to be set.


Returns

void

Code Samples

ContentProvider:SetBaseUrl

1local ContentProvider = game:GetService("ContentProvider")
2
3ContentProvider:SetBaseUrl("http://www.roblox.com/")

UnregisterDefaultEncryptionKey

void

Returns

void

UnregisterEncryptedAsset

void

Parameters

assetId: Content

Returns

void

CalculateNumTrianglesInMesh

Yields
Roblox Script Security

Parameters

meshId: string

Returns

PreloadAsync

void
Yields

Yields until all of the assets associated with the given Instances have loaded and takes an array of Instances as a parameter.

This can be used to pause a script and not use content until it is certain that the content has been loaded into the game.

When the function is called, the engine will go through the array of instances (and all of the descendants of the passed-in instances). If any of the instances have a property that defines a link to content, such as a Decal or a Sound, then the function will attempt to load the asset from the Roblox website. If any of the assets fail to load, an error message will appear in the output, but the PreloadAsync function itself will not error and will continue executing until it has processed each passed-in instance.

Parameters

contentIdList: Array

An array of Instances to be preloaded.

callbackFunction: function

The Lua function to be called on completion of each asset request.

Default Value: "nil"

Returns

void

No return.

Code Samples

Preloading Assets

1local ContentProvider = game:GetService("ContentProvider")
2
3local sound = Instance.new("Sound")
4sound.SoundId = "rbxassetid://9120386436"
5local decal = Instance.new("Decal")
6decal.Texture = "rbxassetid://5447528495"
7
8local assets = {
9 decal,
10 sound,
11 "rbxassetid://5173222739",
12 "rbxthumb://type=Avatar&w=100&h=100&id=269323",
13}
14
15-- Preload the content and time it
16local startTime = os.clock()
17ContentProvider:PreloadAsync(assets)
18local deltaTime = os.clock() - startTime
19print(("Preloading complete, took %.2f seconds"):format(deltaTime))