How would you use ContentProvderService

I have a basic question of what does the ContentProviderService let me do. Mainly, can I load things on the server that is being cloned or do I use it locally in StarterPlayerScripts or something. I need to know this because I made the following module and unsure if its right.

local ContentProvider = game:GetService("ContentProvider")
local loadMap = {}

function loadMap:updateLoading(Total,Loaded)
	local decimal = math.ceil(Loaded/Total)
	--somethings here
end
function loadMap:Yield(Map)
	local AssetsLoaded = 0 
	local everyHund = 50
	local New=Map:Clone() New.Parent = game:GetService("Workspace"):WaitForChild("CurrentMap")
local AssetsToLoad =  New:GetDescendants()

for _, e in ipairs(AssetsToLoad) do
	ContentProvider:PreloadAsync({e})
		AssetsLoaded += 1 
		everyHund+=1
		if everyHund==50 then
			everyHund=0
			loadMap:updateLoading(#AssetsToLoad,AssetsLoaded)
		end
	end
	end
return loadMap
1 Like

The content provider is used to preload assets (things from URLs–like decals, sounds, audio, meshes, and animations) and to know when assets are loaded.

I believe your code would work, but I’m not sure if the :PreloadAsync function errors when you try to preload something that doesn’t need to be loaded. You could also use the second parameter by changing your code from:

for _, e in ipairs(AssetsToLoad) do
	ContentProvider:PreloadAsync({e})
		AssetsLoaded += 1 
		everyHund+=1
		if everyHund==50 then
			everyHund=0
			loadMap:updateLoading(#AssetsToLoad,AssetsLoaded)
		end
	end
end

to:

ContentProvider:PreloadAsync(AssetsToLoad, function()
	AssetsLoaded += 1 
	everyHund+=1
	if everyHund==50 then
		everyHund=0
		loadMap:updateLoading(#AssetsToLoad,AssetsLoaded)
	end
end)

This is because the next parameter is a call back function that runs each time an instance’s assets are fully loaded. Either is fine though.

I believe the :Clone function fully clones the instance it’s called on then returns the clone, so you don’t have to worry about the instance not being fully copied/loaded.

3 Likes