How to get LocalScripts to wait for workspace to completely load

This is a continous problem I’ve been having forever and it’s starting to drive me nuts. So, whenever I play the game, I get something like this
[Infinite yield possible on ‘Workspace.Gifts.Gift7:WaitForChild(“Box”)’]
Anytime I basically use WaitForChild() on a workspace object, it 50% of the time returns this. Now, a lot of the time, I never get that, and the game runs fine, but it’s the small percentage (like 20-30% of the time) that just makes testing a pain in the …

So is there anyway for the client to wait for workspace objects to be loaded in?


Image to show that Gifts.Gift7.Box is an actual thing. So it is in the game

More testing. Set each models PrimaryPart to be Box, however, as you can see, apparently Gift4 PrimaryPart is nil, even tho you can see Gift4>PrimaryPart == Box

More testing For some reason, Gift7, Gift8 and Gift9 just stop loading. Their parts don’t load

2 Likes

Yes, this is how you wait for workspace objects to load in. :WaitForChild's warning doesn’t stop it from waiting, it’s just a warning.

There is a function of the DataModel called :IsLoaded which checks when the game is loaded. game:IsLoaded()

You can use this with game.Loaded event as well to check for when the game does finish loading, and have a function to use.

This might help

Problem with it tho is it’s causing other scripts to not run

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

Put this in the LocalScript which requires all the modules. Even with this, I still get it waiting for objects to load in workspace

1 Like

To add to this, as you can see in the image I attached in the post, you can Gifts1-8. It works for Gifts 1-6, then stops at 7, even tho 7’s children have the same name/properties as all the others

image

for i, v in pairs(workspace.Gifts:GetChildren()) do
    print(v, v.Box)
end

Error
[Gift3 Box]

[Gift2 Box]

[Gift1 Box]

[09:16:47.217 - Box is not a valid member of Model]

Contrary to the name it sports, the Loaded bool/IsLoaded function actually returns a bool that determines whether the server has finished replicating instances to the client or not. It doesn’t actually do anything load wise.

That being said, there is no way to accomplish what’s being asked in the OP. I do know that this has been asked several times over to no avail.

2 Likes

You can take advantage of the network queue to determine this. Add a RemoteFunction somewhere and invoke it from the client, when it returns the workspace should have loaded.

2 Likes

I figured it out in the end. Turns out, StreamingEnabled was set to true (a property I never touch)

I took over the project from a previous scripter, so they must have turned it on (not sure why?)

2 Likes

SE allows you to make the game less laggy for the client. There are some caveats to it, for instance we have to use :WaitForChild() for most instances in workspace.

This is probably because the game is using a large open world or something, SE is usually recommended for large open-world maps.

1 Like

Unless network streaming is enabled, LocalScripts outside of ReplicatedFirst can instantly access all instances that are parented to the DataModel prior to runtime. WaitForChild is only needed when accessing:

  • Instances parented during runtime
  • Instances outside of ReplicatedFirst from a LocalScript inside ReplicatedFirst before DataModel:IsLoaded returns true

Anaminus explains this in further detail:

@woot3 @colbert2677 @rooboo14 @posatta

3 Likes

This is what I meant by my post and I was in fact referencing that very diagram. LocalScripts don’t run until IsLoaded fires. When IsLoaded fires, the server has finished replicating instances to the client - LocalScripts start running afterward. Naturally they’d also have access to instances replicated to them from the server snapshot.

1 Like

I assumed that he either had streaming enabled or parented the instances during run-time considering the fact they don’t exist in the models in his third screenshot. I’m aware that it’s not always necessary to use it, sorry for any confusion with my original post.

1 Like