Should I use WaitForChild for module scripts?

I recently started trying to include more module scripts in my workflow, but I’m having a tiny difficulty.
This is an example code:

local Module = require(script:WaitForChild("ExampleModule"))
Module. --and here something from inside the module

But when I use the WaitForChild() function, the scripting editor doesn’t autofill after the dot when I try to access something inside the Module. When I don’t use WaitForChild(), it does autofill.

So I am wondering:
On the roblox API and in tutorials, it always says you should use WaitForChild() because sometimes a script is running before all objects are loaded in (like Parts and GUI), but so should this still be used with other scripts in serverscriptservice?

3 Likes

Wait for child basically wait for the child to be created if it havnt created it will yield the thread till it does thats why its important as game may lag and the script/part havnt created yet then when the game run normally it will create the script/part.

1 Like

There are certain instances when you may want to use :WaitForChild, such as a module script physically replicating on the client and you want to reference the replicated module inside of the player. Since replicated modules are not physically inside of the workspace, using a dot operator could just throw up an error.

An example of when you don’t really need to use ModuleScripts is in a case similar to this:

Let’s follow this hierarchy and let’s consider that a local script is inside of the StarterGui and a module script is already INSIDE of that said local script,

  • Local Script
    • Module Script

Unless the Module Script is being moved around or isn’t already inside of the Local Script to begin with, you don’t really need to use WaitForChild because the script along with all of it’s descendants will replicate to the player.

Of course, module scripts do not ALWAYS need to be inside of scripts that are requiring it, but generally, it’s important to use WaitForChild when you’re dealing with client-sided modules or anything in general.

1 Like

I do not have an in-depth understanding of when to use WaitForChild, but I use it all the time so that I can ensure that my scripts will not fail by children that do not load on time.

1 Like

Most of the time WaitForChild isn’t needed and shouldn’t be used. You will only really need it when your dealing with ReplicatedFirst or Client side replication. There’s a good few articles on this which I will link below a few might be outdated but should still generally apply.

Unless you are adding in a instance at a later time you won’t need to use WaitForChild with anything in ServerScriptStorage/ServerScriptService as they are instantly available as soon as the server starts and because only the server has access to it there’s no need to deal with replicating to the client.

Another good resource

3 Likes

Thank you so much! That’s all the information I needed!
And I’m glad that it’s this way, this will for sure speed up my scripting haha