Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is using WaitForChild to get something from the server necessary in a LocalScript?

Just wondering, does the server actually load everything saved in studio before a client joins? Or does it load both the server and client simultaneously?

Would saying something in a local script like:

LocalScript:

local event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

Really be necessary? Or could I just say local event = game.ReplicatedStorage.RemoteEvent. Do I run a risk of breaking my local script if it runs before that event loads in the server, or does something prevent that?

Custom wait for functions?

I also see some people making their own waitforchild functions in local scripts, which I also don't really understand.

Anyway...

All I really wanna know is, when is it necessary to make the local script wait for something to load, and when isn't it? Cause I don't wanna run the risk of breaking it if something doesn't exist, but I also don't want to keep typing WaitForChild for every line of code...

Anyway, thanks for reading. Hope you can share some insight.

1 answer

✓ Accepted answer

Yes. This is possible, but unlikely.

On particularly slow internet connections, there's not a guarantee of what order things could load. So it's possible that all of ReplicatedStorage isn't done by the time the character begins to spawn.

That is at least much less likely to be a problem for ReplicatedFirst, though I wouldn't be surprised if it could happen still, extremely rarely.


:WaitForChild is a relatively recent addition, so older code (or code based on older code) will sometimes define it themselves.

:WaitForChild also HAD a nasty problem: if an object is already in a model, but gets renamed to the name you're waiting for, :WaitForChild DIDN'T see it.


There are three main reasons why you would have to wait for something:

  • The thing didn't get created until very recently (you have control over this usually!)
  • The script was created alongside the new object, so the order they come in isn't necessarily defined (e.g., in StarterGui or a model cloned into the workspace)
  • For some reason, the server hasn't sent it along to the client yet (or the client to the server). This is hard to control and effects pretty much everything (that isn't created by the same client)

2 comments

2

In my testing WaitForChild catches renamed objects just fine long after they have been parented.

0

Did they finally fix it? Happy day!

This discussion is preserved as it was. New answers, comments, and votes are closed.