Time effective way to preload all assets?

Hello,

I am trying to preload the assets that my game uses. I want to use preload async. However, all of my textures were imported to studio via the Game Explorer and I use them in different scripts (so they are not on actual decals).

Is there any way to get all of their IDs in bulk so I can pass them to preload async? I don’t want to manually copy paste 200+ decal ids, unless there is no other way.

Thank you

1 Like

Sorry for the late response, but I took my time to learn string patterns just to solve your issue. Below I wrote a script that searches all scripts in your entire game for asset id results (rbxassetid://0 and rbxgameasset://name name name name). Paste the code below into your command bar, and as soon as it is done a new ModuleScript in ReplicatedStorage named AssetIds gets created which contains all Asset IDs that can be found in your game. You can also require the module since it returns all asset ids as strings via a table.

local results = {}

local function Service(serviceName)
	return game:GetService(serviceName)
end

local function scan(p)
	for i,v in pairs(p:GetDescendants()) do
		if v:IsA("BaseScript") and v.Name ~= "AssetIds" then
			local Source = v.Source
			for match in string.gmatch(Source, "rbxassetid://[0-9]+") do
				table.insert(results, match)
			end
			for match in string.gmatch(Source, "rbxgameasset://[%a%d%s]+") do
				table.insert(results, match)
			end
		end
	end
end

local LocationsToScan = {
	Service "Workspace",
	Service "ReplicatedStorage",
	Service "ServerScriptService",
	Service "ServerStorage",
	Service "StarterGui",
	Service "StarterPack",
	Service "StarterPlayer"
}

for i,v in pairs(LocationsToScan) do
	scan(v)
end

local repr = require(3148021300)
local newSource = repr(results, { pretty = true })

local ResultScript = Service "ReplicatedStorage":FindFirstChild("AssetIds") or Instance.new("ModuleScript")
ResultScript.Parent = Service "ReplicatedStorage"
ResultScript.Name = "AssetIds"

ResultScript.Source = "local assetIds = " .. newSource .. [[ 
 
return assetIds]]

Service "Selection":Set({ ResultScript })

If there is a match missing that you need, feel free to ask and I will try to add it in.

14 Likes

Thank you this is a great way to do it!

It’s worth saying this: DO NOT PRELOAD ALL YOUR ASSETS.

Preloading doesn’t quite work in the way we commonly think it does. Preload simply pushes assets to the front of the download queue so they are the first assets the client downloads and if you pass an id that isn’t used in the game, they’re added to the front of the queue.

If you’re just pushing every asset to the front of the queue, then ultimately there isn’t any point in using preload at all. PreloadAsync is intended for you to load up assets that you need immediately available to the client. It’s much like ReplicatedFirst for instances: the first things the server replicates to clients.

4 Likes

Can I not wait until the queue is empty though?

The only control you have over the queue is pushing assets to the front via PreloadAsync and seeing how many are queued (but not in the process of downloading) via RequestQueueSize.

If you meant waiting for the queue to empty out as in having the loading screen disappear when RequestQueueSize is 0, I don’t recommend doing that. The queue size changes dynamically and long loading screens are bad for user experience.

The most time effective form of preloading/controlling asset downloading is only to preload assets you need immediately seen by the player (e.g. loading screen graphics) and then let the engine stream the rest in the background as usual.

1 Like

Doesn’t preloading also put assets into the queue otherwise only starting to load once needed?

Yes. If you use PreloadAsync on an asset that hasn’t been used before, then it will be added to the download queue and then be pushed to the front. Here there’s an almost guarantee that your first actual use of the asset will instantly render it.

So there would be some advantage of preloading all assets?

There’s zero advantage to preloading all your assets because then you’re diminishing the actual benefits to preloading. Preload only the ones the player needs to see immediately such as loading screen images or main menu assets. Everything else should be streamed in.

See: