What is the best way to preload all assets?

So I have a game that has runs mostly on audio and decals, but I want all of them to load before even starting the game.

I found out that the preload api is deprecated so I came here to ask

8 Likes

PreloadAsync is not deprecated, you could pass {game.Workspace} as the contentIdList argument and it will preload all content which are descendants of it. Audios, decals etc. all added to the request queue.

3 Likes

Sorry if I wasn’t clear, as I assume you heard that from me. PreloadAsync is separate from Preload.

Something like this

spawn(function()
	game:GetService('ContentProvider'):PreloadAsync({workspace,game:GetService('ReplicatedStorage'),game:GetService('PlayerGui')})
end)

And if you want to make a loading bar

repeat PercentTxt.Text = '('..math.floor(((RequestQueueSize-CP.RequestQueueSize)/RequestQueueSize)* 100)..'%)' LoadingBar:TweenSize(UDim2.new(1 - string.format('%.3f',CP.RequestQueueSize/RequestQueueSize),0,1,0),1,0,.5,true) wait(1) until 
CP.RequestQueueSize <= 0

CP = ContentProvider

It’s just an Example code, don’t forget to assign the missing Variables.

70 Likes

I don’t recommend relying on RequestQueueSize for something like a load screen, as assets like shirts from characters will add to this. You may want to use PreloadAsync with 1 asset at a time, like this code snippet from Ultimate Boxing’s preload screen:

for Id,Asset in pairs(OtherAssets) do
	local AssetsLeft = #OtherAssets - Id + 1
	if AssetsLeft == 1 then
		BottomText.Text = "Loading... (1 Asset Left)"				
	else
		BottomText.Text = "Loading... ("..AssetsLeft.." Assets Left)"
	end
			
	ContentProvider:PreloadAsync({Asset})
end
21 Likes

to be honest that’s a really horrible idea, passing workspace to PreloadAsync

first, why are you using game.Workspace. the global variable workspace exists and if your workspace gets renamed somehow then your scripts die

second, preloading should only be used on things that need to be seen or heard immediately. it’s not at all recommended you preload your whole game.

1 Like

Was only using it as an example, no need critisise it like that, if the game is largely asset based then gameplay may be bad without all content loading before entering, I don’t think it’s a bad idea at all. I don’t like using workspace for some reason, I’d use game:GetService(“Workspace”) just because of preference.

EDIT: I agree long loading times are bad UX but if it’s to improve gameplay it can be worth it. Take GTA V for example.

8 Likes

Adding on to General’s preference, the GetService method operates on class names, not names. If you were to rename any service, getting it by class name still works.

3 Likes

Indexing using service names is not a big deal within places you have control over. “if your workspace gets renamed somehow” is not a serious concern. Just rename it back. With FilteringEnabled on, exploiters can’t rename it on the server either so that’s not a concern. Service names should be locked anyway, but they’re not probably for backwards compatibility reasons.

Even in situations where you don’t have control over service names – such as in public Modules – you can still use game.Workspace. It’s a property and it’s not deprecated. You can’t do this with other services though.

I personally believe that using GetService and using the built in workspace global variable are good habits, but choosing not to do those things is not a big deal unless you’re writing public modules.

9 Likes

If that is the case then why not just put everything that needs loading into preload first?

And then preload again once loading is done.

@General_Scripter Even in that scenario though, if your game is largely asset based, you should only be preloading what you want immediately visible. Preloading isn’t necessarily meant for entire games. You can do what you want with your code, of course, but I’m just saying based on what I hear and have tried out. I hear your preference, sure, but I do feel that simply writing “workspace” is easier than going the long way to index it in GetService; same goes with game.Workspace for the legacy people (found out it’s a reference property and doesn’t directly get the descendant WS, thanks @Corecii).

@Kiansjet I’m aware. I was speaking in direct relation to game.Workspace because I forgot/didn’t know it was a reference property.

@Corecii That’s why I’m speaking from an all-around point of view, not just owned places. Sometimes even you have to call your own external code in your places. - referring to modules.

Cheers for the replies though.

3 Likes

Yes thats what I exactly what I want to do!

I don’t think I have every known someone to have renamed Workspace so it seems pretty irrelevant for you to bring it up. Even if someone DID rename their Workspace, it goes without saying that you can’t reference it in scripts anymore, so why you are criticizing his/her comment for this…?

2 Likes

Jailbreak renames workspace (and a bunch of other stuff) (Security?)

also:

I don’t think it was a criticism, more a clash of preferences that came off in a rude manner (which I apologize for, @General_Scripter, if this is what it seemed like).

1 Like

Perhaps I wasn’t clear — my point is that bringing it up is mostly irrelevant and not that people don’t do it. Please private message me if you want to continue discussing this.

So wait…

Let me make this clear I could just make a folder and insert all of my important assets and just call them using

game:GetService(‘ContentProvider’):PreloadAsync({game.Workspace.AssetThatNeedToBeLoaaded})

??

2 Likes

yeah that’s right!

3 Likes