How to prevent local script from firing before game is fully loaded

Hello, I am attempting to rewrite code I created for a stud replacer script. Originally it was located in StarterGui, however I’d like to avoid that as much as possible and instead use StarterPlayerScripts as it is only replicated once, not upon death, and it would make my life easier. One problem I’m running into however is that local scripts seem to run before any assets are loaded in even when using game:IsLoaded()

repeat task.wait() until game:IsLoaded()

print("Loaded.")
print(workspace.Baseplate)

This is the output code I recieve when booting up a play test.
Baseplate is not a valid member of Workspace "Workspace"

When I insert a task.wait(5) to wait five seconds (waiting until the game is fully loaded manually) it works perfectly fine, with no issues whatsoever.

I have looked for other devforum posts of users with the seemingly the same issues, however they all use this method and claim it works absolutely fine. I’d like to avoid using content service if possible.

3 Likes

Have you tried doing this maybe?

game.Loaded:Wait()
-- rest of the code

If that doesnt work , try-

if not game:IsLoaded() then
	game.Loaded:Wait()
end
2 Likes

Yes I have, it seems to just give an infinite yield however, or at least no code will run after the game.Loaded:Wait()

if not game:IsLoaded() then
	game.Loaded:Wait()
end

gives the same error code,
Baseplate is not a valid member of Workspace "Workspace"

2 Likes
local isLoaded = game:IsLoaded() or game.Loaded:Wait()
if isLoaded then
....
end

What about this?

1 Like

Where did you print the stuff? Within or without the if statement?

2 Likes

For that example, out of the if statement, like this

if not game:IsLoaded() then
	game.Loaded:Wait()
end

print("Loaded.")
print(workspace.Baseplate)

As for your other reply,

What about this?

Using this code

local isLoaded = game:IsLoaded() or game.Loaded:Wait()
if isLoaded then
print(workspace.Baseplate)
end

gives the same results as before.

here’s the rbxl file, if you have the time to open it up and check it out.
it’s nothing but a baseplate and the script.

2 Likes

Could it possibly be because of streaming enabled? Try to turn it off and test again

3 Likes

That was it! Thank you, are you able to elaborate as to why disabling streaming enabled fixes this problem, or… for that matter… what streaming enabled is in the first place? Are there any downsides to disabling it?

Edit: I viewed the creator docs on it, interesting. Thankfully my game won’t be too big, so streaming enabled wont really benefit me that much. Thanks a lot again!

1 Like

Starting from your last 2 questions:

And personally I think it happens because the client first loads all the things needed for game.IsLoaded to be true and then stream in all the parts inside the range (including baseplate), or game.IsLoaded doesn’t check if all the parts that need to be streamed in are streamed in, if at all.

From my personal experience streaming enabled is good for performance but very buggy in almost every other aspect of it

3 Likes

Alright, I’m starting to understand it better. I’m assuming to make something like this really work with streaming enabled I’d need to use content service, yeah?

1 Like

Im not familiar with ContentService, and after a quick google search I only found of ContentProvider which isnt related to Steaming Enabled as far as I’m aware… Could you enlighten me?

2 Likes

Sorry, was referencing this ContentProvider | Documentation - Roblox Creator Hub

Saw it mentioned in a old thread I found whilst looking for answers.

1 Like

No. ContentProviderService should not be used. That is used to load assets, such as, sounds, images, videos, decals etc. where an asset is needed to be downloaded from roblox.com to the active server, before it is used, (Often Assets in the format of: rbxassetid://[ID] (Not always, but just often)).

What you really should do is make sure that the block exists on the client, before attempting to use it.

3 Likes

Alright, understood. Thanks to everyone for your help.

4 Likes

This doesn’t make any sense, Scripts run after the game is loaded.

1 Like

Not always. Scripts run the second that they are loaded individually, (like if you have a local script in ReplicatedFirst, Its going to run while everything else is loading.)

The issue was the OP was using streamingEnabled which takes additional time to load BaseParts as the Client streams the parts in closest etc.

In other words, the Client was loaded, and everything was loaded, like all the scripts etc to run, but streaming enabled renders the parts after loading all other instances besides BaseParts. So in this case, workspace.Baseplate didn’t exist when game:IsLoaded() was ran

(This is what I understand from this post)

2 Likes

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