How to make sure script doesnt run until game is FULLY loaded

Hello.

For some time now, I have had a problem with scripts.

Because the game loading time can change based on lag or poor performance, it’s possible that the script runs before everything is loaded.

is there anyway to make it wait() until it’s fully loaded?

here is my current method, however this is also bad because randomly, it might not work.

repeat wait(1) until game:IsLoaded()

4 Likes

You can use ContentProvider Service and it’s function :PreloadAsync() probably
By that, I mean you can loop until all of the assets are ready, and then it triggeres the main code

6 Likes

Alright, i’ll look into it and see if that works.

5 Likes

You should not use wait() as it is not accurate, be sure to use task.wait(). If that does not work, you can just manually wait a reasonable amount of time before running that script.

2 Likes

How would I do this?

Can you give me an example because what I need to accomplish is to use preloadAsync to check if workspace has loaded, I haven’t been able to find anything on using content provider to wait until workspace has loaded.

I’m suspecting I could use game:Getdescendants

1 Like

try this maybe

contentProvider = game:GetService("ContentProvider")
assets = game.Workspace:GetDescendants()

for i, v in pairs(assets) do
     contentProvider:PreloadAsync({v})
end
1 Like

This script stays at for i until everything is loaded, right?

Couldn’t you just use something like

game.Loaded

?

2 Likes

Yes it will. :PreloadAsync() loads the asset given, and for i, v in pairs loops through all of the instances in a table. And game.Workpace:GetDescendants() returns a table

1 Like

probably, not sure. Never used it before lmao

Right now, when I put your code in my script, it never gets out of the for i loop.

How long do you leave it for? Depending on how many parts there is It could take forever.

This is what I found online, It looks like the exact same except you might need to fire an event to enable a bool value if you want it for server side, not sure this tends to only be used for when doing loading screens and I dread doing them but I just know it should work if you experiment with it.

I break after one second, it seems to work. It’s weird because even with the repeat wait() until game:isloaded method, it only fails sometimes.

This new method might work.

I reduced the wait time to the default wait time, so just wait() and it seems to work.

1 Like

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