The @game require-by-string alias has just recently gone live, and with it, we move one step closer towards a singular uniform import pattern. Alas, it leaves more to be desired. I propose that @game be expanded to allow services to be imported via require.
TL;DR
For consistency’s sake, allow services to be imported via the new @game require-by-string alias, ie. local Players = require("@game/Players"). Today, the @game alias is already analogous to game:GetService in that @game/ServerScriptService will always point to ServerScriptService regardless of what ServerScriptService may have been renamed to – this same behavior should be preserved when importing services via @game.
Why?
Before the introduction of @game, importing at worst would look something like:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local foo = require(ReplicatedStorage.foo)
local bar = require(ReplicatedStorage.bar)
local sibling = require("./sibling")
local child = require("@self/child")
Having three different ways to do ultimately the same thing (import outside behaviors) is not the end of the world, but it does leave a lot to be desired. The introduction of the builtin @game alias acknowledges this and affords us a considerably more consistent alternative:
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local foo = require("@game/ReplicatedStorage/foo")
local bar = require("@game/ServerScriptService/bar")
local sibling = require("./sibling")
local child = require("@self/child")
Allowing services to be imported via the @game alias further improves consistency and removes the need to use game:GetService, thus allowing us to route all imports through the same directive (require):
local TweenService = require("@game/TweenService")
local Players = require("@game/Players")
local foo = require("@game/ReplicatedStorage/foo")
local bar = require("@game/ServerScriptService/bar")
local sibling = require("./sibling")
local child = require("@self/child")
With the upcoming addition of user-defined require-by-string aliases, we should expect users to adopt a more require-by-string-reliant style in pursuit of convenience. Being able to import services via @game feels like a natural change to go along with this:
local TweenService = require("@game/TweenService")
local Players = require("@game/Players")
local squash = require("@shared/squash")
local jecs = require("@shared/jecs")
local lyra = require("@server/lyra")
Further, such an addition would allow users to define their own aliases for services, which I am almost certain will be something users will want:
local TweenService = require("@TweenService")
local Players = require("@Players")
Consistency aside, such a change does well to better align scripting in ROBLOX with the rest of the SWE world: it’s not a particularly common convention to have many different ways to import non-local behaviors. An argument can also be made that this can make interoperability with Roblox-agnostic runtimes more trivial to implement/achieve.
Implementation
Today, the services navigable via @game use an implementation akin to game:GetService, in that if ServerScriptService has been renamed, require("@game/ServerScriptService/foo") will still resolve as if it had not been renamed. Should this feature be considered, similar behavior should be implemented, in that require("@game/Players") would be analogous to (if not literally shorthand for) game:GetService("Players") rather than to game.Players.
Trivial to implement
The specialized behavior of @game implies that adding such a feature should be fairly trivial. Even if ServerScriptService is renamed to server, require("@game/server/foo") is not a valid require path. If we optimistically assume that this is because the Roblox runtime’s C++ side implementation of require has a special case for handling module paths that start with @game (as opposed to something like @game being a true Luau alias pointing to some hidden set of mirrors of services), then to add this feature is just to add a little extra behavior to the existing handler: if the input is akin to @game/service, return the service, else if the input is akin to @game/service/..., traverse the path and require/return the module.
Psuedocode
The Roblox runtime is obviously not written in Luau, but I am not a C++ dev, so allow me to use Luau to psuedocode:
local function rbs( path: string )
if type(path) ~= "string" then
error("Attempted to call require with invalid argument(s).")
end
local tree = path:split("/")
local root = tree[1]
if root:sub(1, 1) == "@" then
-- root is an alias
if root:sub(2, #root) == "game" then
-- root alias is `@game`
if tree[2] == nil then
-- `path` is just `@game`
error("Unable to require module from given path '@game'")
elseif tree[3] == nil then
-- `path` is akin to `@game/service`
return game:GetService(tree[2])
else
-- `path` is akin to `@game/service/...`
local instance = game:GetService(tree[2])
for i = 3, #tree do
instance = instance:FindFirstChild(tree[i])
end
return require(instance)
end
else
-- pretend this handles other aliases
return require(path)
end
else
-- pretend this handles relative-path requires
return require(path)
end
end
Though I don’t recommend it, you can even use this psuedocode today! Just don’t do anything potentially erroneous. ![]()
print(rbs("@game/Players")) -- prints `Players`
print(rbs("@game/ServerScriptService/ModuleScript")) -- prints its contents, assuming you have such a file
The catch(?): types
It remains unclear to me whether or not such behavior is feasible to represent within the type solver. I am under the assumption that the runtime implementation is magnitudes easier to implement than its type solver counterpart. The internal workings of the type solver and to what degree of control its embedders (Roblox, luau-lsp) are afforded is not my forte, so I must bite my tongue on it. To anyone more knowledgeable on this: please disprove my assumption.