ContentProvider RequestQueueSize with PreloadAsync()

So I began rewriting the preloading system that my game has since :Preload() is now deprecated. The only issue I ran into was that RequestQueueSize will only return 1 if you use :PreloadAsync() and pass in a table of instances as you should.

I was wondering if there was an easier way to bypass this issue instead of iterating through the table and passing in one instance at a time or using IsLoaded on every asset in the table. If there was a way to retrieve how many items in the table still have to be preloaded that would make this so much easier.

local gui = game.Players.LocalPlayer.PlayerGui
local CP = game:GetService("ContentProvider")
local assets = {}

script.Parent:RemoveDefaultLoadingScreen()
script.Loading:Clone().Parent = gui

spawn(function()
	while wait() do
		if gui:FindFirstChild("Loading") ~= nil then
			gui.Loading.Number.Text = math.ceil((#assets - CP.RequestQueueSize)/#assets * 100).."%"
		end
		if CP.RequestQueueSize == 0 then
			wait(.2)
			if gui:FindFirstChild("Loading") ~= nil then
				gui.Loading:Destroy()
				break
			end
		end
	end
end)

function Preload()
	local function iterate(v)
		for _, x in pairs(v:GetChildren()) do
			if x:IsA("Sound") or x:IsA("ImageLabel") or x:IsA("ImageButton") then
				table.insert(assets, x)
			end
			iterate(x)
		end
	end
	
	iterate(workspace)
	iterate(game.StarterGui)
	iterate(game.ReplicatedStorage)
	
	CP:PreloadAsync(assets)
end

Preload()
5 Likes

Not that I know of; however you can (kinda) improve the code by getting rid of RequestQueueSize and just wait for PreloadAsync:

local ready = false
spawn(function()
    contentProvider:PreloadAsync(assets)
    ready = true
end)
repeat wait() until ready

This isn’t your main issue, however it’s relevant to the next part.
As for getting how many things are loaded, you’ll want to do something like this:

local toLoad = {}
-- get stuff to load
local ready = false
local loaded = 0

spawn(function()
    for i = 1,#toLoad do
        contentProvider:PreloadAsync(toLoad[i])
        loaded = i
    end
    ready = true
end)

repeat
    wait(1)
    print(loaded.." / " ..#toLoad)
until ready

This solution adds more code, but seems to fix your problem well enough.

roblocc improve RequestQueueSize pl0x

I’d maybe try putting each request into a spawn call. That way, the queue doesn’t get snagged on a single request. You can increment a ‘remaining’ counter and then have the thread subtract from it as each request finishes.

6 Likes

That’s definitely a smarter way going about it. I implemented that idea and it worked immediately. Thanks.