Forest Logo
search
package_2

import

By @vocksel

Roblox

Mirrored

import

CI

This module allows you to write paths for Roblox instances like you do on the filesystem.

Having to constantly type script.Parent with varying levels of .Parent is tedious and can produce excessively long lines. This module aims to fix this by providing a concise syntax for writing import paths that closely resembles what's used on the filesystem.

Download

Download the latest version from the releases page or from the asset library

Usage

local import = require(game.ReplicatedStorage.Import)

local module = import("./ModuleScript")
-- local module = require(script.Parent.ModuleScript)

local module = import("./Folder/ModuleScript)"
-- local module = require(script.Parent.Folder.ModuleScript)

local module = import("../ModuleScript")
-- local module = require(script.Parent.Parent.ModuleScript)

local module = import("../../ModuleScript")
-- local module = require(script.Parent.Parent.Parent.ModuleScript)

To get easy access to the children of Roblox services, you can use absolute paths by starting a path with a slash /.

local module = import("/ReplicatedStorage/ModuleScript")
-- local module = require(game.ReplicatedStorage.ModuleScript)

The import function also provides a destructuring syntax that allows you to import individual members of a module.

local foo = import("./ModuleScript", { "foo" })
-- local foo = require(script.Parent.ModuleScript).foo

local foo, bar = import("./ModuleScript", { "foo", "bar" })
-- local module = require(script.Parent.ModuleScript)
-- local foo = module.foo
-- local bar = module.bar

And this function isn't just for ModuleScripts! You can import any Instance with the path syntax.

local sound = import("./Sound")
-- local sound = script.Parent.Sound

local part = import("./Part")
-- local part = script.Parent.Part

Aliases

Aliases are a powerful method of defining custom starting points for paths.

A common usecase for this is to define entrypoints to your server, client, and shared code. For example:

local import = require(game.ReplicatedStorage.import)

import.setAliases({
	server = game.ServerScriptService.ServerModules,
	client = game.StarterPlayer.StarterPlayerScripts.ClientModules,
	shared = game.ReplicatedStorage.SharedModules,
})

local module = import("shared/ModuleScript")
-- local module = require(game.ReplicatedStorage.SharedModules.ModuleScript)

There is also a built-in script alias that allows you to import the descendants of the script that is calling import().

local module = import("script/ModuleScript")
-- local module = require(script.ModuleScript)

Config

There are several configuration values you can customize to fit your needs.

NameDescriptionDefault
rootControls the root Instance for absolute paths. This is especially helpful when using this module in a package or plugingame
useWaitForChildBy default, FindFirstChild is used when traversing the hierarchy. Set to true to use WaitForChild insteadfalse
waitForChildTimeoutWhen useWaitForChild is set to true, this controls how long (in seconds) to yield before resolving1
scriptAliasControls the name of the alias that is reserved for the current script"script"
local import = require(game.ReplicatedStorage.import)

import.setConfig({
	useWaitForChild = true,
	scriptAlias = "@",
})

local module = import("@/ModuleScript")
-- local module = require(script:WaitForChild("ModuleScript"))

Development

You will need Rust 1.41.0+ and the Rojo plugin. If you use VS Code, you can install the Rojo extension which manages the plugin for you and makes it easier to serve the project.

It is also recommended that you add ~/.foreman/bin to your PATH to make the tools that Foreman installs for you accessible on your system.

# Cargo is Rust's package manager, Foreman is our toolchain manager
cargo install foreman

# Install Rojo and other tools we use
foreman install

# Install dependencies
wally install

# Serve the project. Use the Rojo plugin in Roblox Studio to connect
rojo serve dev.project.json

# Or build the project. Drag and drop into Roblox Studio to insert it
rojo build -o import.rbxmx

License

MIT

Package Details

Install command (Click to copy)


Version

2.0.0

License

MIT

check_circle

Safe for commercial use

Automated license review — not legal advice.