require("@game/ServerStorage/Module") or require(game:GetService("ServerStorage").Module)?

did recently see about require when it was red squiggly line and I’m interested about why it exists

wait this is wrong lemme uhhh redo the title

there

You mean require by string? It’s just how it’s done almost anywhere outside Roblox.

Instead of typing out:

require(game:GetService(ServiceName):FindFirstChild("child1"):FindFirstChild("child2"):FindFirstChild("child3"):FindFirstChild("child4"))

you can do:

require("@game/ServiceName/child1/child2/child3/child4")

6 Likes

why not

require(game:GetService("WhateverServiceHere"):FindFirstChild("child4", true))

?

Because you want child 4 insid child 3 inside child 2 inside child 1, not in some other folder elsewhere. It also won’t work with code completion.

There’s no need for :WaitForChild or :FindFirstChild unless your using the recursive :FindFirstChild(NAME, true). Futhermore, using :WaitForChild becomes redundant if your using it in the wrong context, and place. There’s a post by this person.

Appropriate use of :WaitForChild(...)

The notations for string path requires support:

  • @self/ equivalent to script.
  • @game/ equivalent to game.
  • ./ equivalent to script.Parent
  • ../ equivalent to script.Parent.Parent.

Some advantages, assuming you don’t use external editors like rojo are the fact its much more shorter and cleaner than individually typing out its path with its FullName. With the obvious downside of ‘structural strictness.’

I only use string notations for ‘short requires’, or requires that don’t require me to always type script[NAME] or script.Parent[NAME]. However for code flexibility, I’d rather declare the paths of the Instances that get to it so, If in future I want to change one of the Instances, I can just change the variable I called to something like local ModulesFolder = ReplicatedStorage.Modules to local ModulesFolder = ReplicatedStorage.Framework.Modules.

2 Likes

I personally prefer string requires but it’s honestly just preference or whatever your team uses

2 Likes

The for sure, for sure way ..

local mod = require(game:GetService("ServerStorage"):Waitforchild("ModuleScript"))
local mod = require(game:GetService("ServerStorage"):Waitforchild("child1")
	:Waitforchild("child2"):Waitforchild("child3"):Waitforchild("ModuleScript"))

I always use GetService for services when possible. I want it to error if the path is wrong, so I can see exactly where to fix it. Laggy loads can be a nightmare sometimes, and chaining WaitForChild makes sure it waits for the hierarchy to load properly. Of course, you can organize that a bit if needed..

local serverStorage=game:GetService("ServerStorage")
local mods=serverStorage:WaitForChild("child1")
	:WaitForChild("child3"):WaitForChild("child3")

local mod=require(mods:WaitForChild("ModuleScript"))