How can I wait until all the assets in my game are loaded in a script?
repeat wait() until game:IsLoaded()
There isnt really a way to do that sadly.
An easy way would be to wait for the game to load like the example above me
Hey just a small tip. Hope you dont mind me giving some quick advice:
Dont use wait() wait is deprecated. Use the task library instead. It has a new updated version of wait called “task.wait()”
Alright, I was just in a rush
Yes but task.wait()
will keep going even after you destroy the script (from what I heard)
I’m not sure, but even if it does, it wouldn’t matter much anyways since that script doesn’t even exist to do anything anymore lol
Yeah you’re right
You can use a method that’s called PreloadAsync within the ContentProvider
service, what this will do is take a table
of Instances to yield for, and will resume the script once it’s done loading all the assets
Something to keep note of, is that this method has 2 parameters:
- A
table
of Instances to store and yield/wait for - An optional callback function when each individual asset has been loaded by the method
local CP = game:GetService("ContentProvider")
local AssetsToLoad = {workspace.Baseplate, workspace.RandomPart}
CP:PreloadAsync(AssetsToLoad) -- This is a yielding function, so the print statement under this will NOT fire
print("Done loading all assets!")
Even the API reference gives you a code sample on how to use it, so make sure to keep it into consideration
-- Code Sample from "PreloadAsync"
local ContentProvider = game:GetService("ContentProvider")
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
local decal = Instance.new("Decal")
decal.Texture = "rbxassetid://5447528495"
local assets = {
decal,
sound,
"rbxassetid://5173222739",
"rbxthumb://type=Avatar&w=100&h=100&id=269323",
}
-- Preload the content and time it
local startTime = os.clock()
ContentProvider:PreloadAsync(assets)
local deltaTime = os.clock() - startTime
print(("Preloading complete, took %.2f seconds"):format(deltaTime))
@Herobrinekd1 You should consider researching a tad bit first before assuming that there’s “no way” to load assets, refer to what I just said earlier before making your claim
Would the code be task.wait() game:IsLoaded()?
repeat task.wait() until game:IsLoaded()
Yeah my bad i should have worded that better. That wasn error on my end
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.