I ran into a confusing behavior with WaitForChild() while working in a place with Streaming Enabled and wanted to see if anyone else has experienced this or can explain it.
Here’s what’s happening:
I use :WaitForChild("SomePart") on a part that doesn’t exist yet in the hierarchy, due to streaming
At first, I get the expected warning that the part doesn’t exist.
Then, when I move my character near the part, the part loads dynamically because of streaming.
However, my script remains stuck on the WaitForChild() line and never continues.
Why does the script hang here even though the part eventually loads? I have not been able to find much related to this.
Any insights or suggestions on how to handle this situation properly would be much appreciated!
as far as i know, wait for child has a timeout value (default is 5 seconds i think), and if that child does not get found after x seconds of waiting, the whole code stops
Well, when a part is streamed out, the parent is no longer set to the player’s workspace. And until it’s streamed in again you will be waiting for it (unless it times out), instead of stopping the execution of your scripts, you should use Tags, this way you can detect when they come in and out of the workspace.
The 2 events to do this are GetInstanceAddedSignal() and GetInstanceRemovedSignal()
This is much better way to handle interactions/parts with streaming enabled.
Yes. If StreamingEnabled is on all distant parts are not loaded. Only instances like scripts values, click detector, proximity prompts, sound, models, folders etc.
So you need to put your part into a model and when game start do:
while not workspace.model:FindFirstChild('somepart') do wait(1) end
This isn’t a really good way to get a part from the workplace with streaming enabled, as you’re pausing the execution of the script to wait for a part that could possible take forever to load in.
This is why tags are much better, as you can detect when they are being added and removed, so you can handle creating the interaction and destroying the interaction.
However, I’m not sure how this interacts with non-anchored models. It’s possible that it may fall through the map if no floor exists at the time of loading, but perhaps Roblox has thought of that. Somebody can correct this if they know.