Attempting to Preload Assets, Table is empty

Hello! I’ve been toying around with PreloadAsync and attempting to Preload my assets! For some reason, the code below returns an empty table, when these folders DO have descendants. Below is just my debugging code. An empty table: {} gets pushed into the output. Any help?

local clock = os.clock()

-- // Services
local contentProvider = game:GetService("ContentProvider")

-- // Variables
local assets = {}
local tab = {
	game.ReplicatedStorage; 
	game.StarterPack;
	game.Lighting;
}

-- // Loading the assets
for _, v in pairs(tab) do
	for _, z in pairs(v:GetDescendants()) do
		table.insert(assets, z)
	end
end

print(assets)

contentProvider:PreloadAsync(assets)

warn(string.format(' == PRELOADED ASSETS IN: %.2f SECONDS. == ', os.clock() - clock))

I pasted your exact script into a Server Script in ServerScriptService and it seems to be working just fine. Maybe you’re using the wrong type of Script?

OUTPUT:

  17:29:35.719  Baseplate auto-recovery file was created  -  Studio
  17:29:36.456   ▼  {
                    [1] = Tool,
                    [2] = Handle,
                    [3] = Sky,
                    [4] = SunRays,
                    [5] = Atmosphere,
                    [6] = Bloom,
                    [7] = DepthOfField
                 }

I put the script under ReplicatedFirst as a LocalScript as I thought PreloadAsync was to Preload the assets on the client.

It works on local scripts too apparently. But just not in ReplicatedFirst

OUTPUT (localscript in ReplicatedFirst)

  17:35:33.082  Baseplate auto-recovery file was created  -  Studio
  17:35:34.353  {} yo 
1 Like

Do I place it into StarterPlayerScripts then?

If it works then yes

Script I used:

local clock = os.clock()

-- // Services
local contentProvider = game:GetService("ContentProvider")

-- // Variables
local assets = {}
local tab = {
	game.ReplicatedStorage; 
	game.StarterPack;
	game.Lighting;
}

-- // Loading the assets
for _, v in pairs(tab) do
	for _, z in pairs(v:GetDescendants()) do
		table.insert(assets, z)
	end
end

print(assets,'yo')

contentProvider:PreloadAsync(assets)

warn(string.format(' == PRELOADED ASSETS IN: %.2f SECONDS. == ', os.clock() - clock))
1 Like

Yay! It seems to work when I placed it into StarterPlayerScripts. Thank you!

PreloadAsync already handles the descendants of the instances you pass to it so there’s no need for you to collect the instances yourself. Skip the creation of the assets table and just pass tab directly.

local ContentProvider = game:GetService("ContentProvider")

local now = os.clock()

ContentProvider:PreloadAsync({
    game:GetService("ReplicatedStorage"),
    -- ...
})

print(` == PRELOADED ASSETS IN {os.clock() - now} SECONDS} == `)

Depending on where you’re running this though, the assets table being blank is expected. LocalScripts in ReplicatedFirst run before the DataModel finishes fully replicating, so instances can be invisible unlike another container (e.g. StarterPlayerScripts as suggested above) where LocalScripts start running during general replication.

Sidenote: I don’t recommend preloading massive amounts of instances unless you realistically need these assets to be seen immediately by the player. PreloadAsync only prioritises existing assets in the client download queue (and adds them to the queue if not in there due to not being rendered).

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.