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.
Model File (Roblox Studio)
- Download the
rbxm
model file attached to the latest release from the GitHub releases page. - Insert the model into Studio into a place like
ReplicatedStorage
Filesystem
- Copy the
src
directory into your codebase - Rename the folder to
import
- Use a plugin like Rojo to sync the files into a place
local import = require(game.ReplicatedStorage.Import)
-- local module = require(script.Module)
local module = import "script/Module"
-- local module = require(script.Parent.Module)
local module = import "./Module"
-- local module = require(script.Parent.Folder.Module)
local module = import "./Folder/Module"
-- local module = require(script.Parent.Parent.Module)
local module = import "../Module"
-- local module = require(script.Parent.Parent.Parent.Module)
local module = import "../../Module"
If you only care about a few members of a module, you can import them individually:
-- local foo = require(script.Parent.Module).foo
local foo = import("./Module", { "foo" })
-- local module = require(script.Parent.Module)
-- local foo = module.foo
-- local bar = module.bar
local foo, bar = import("./Module", { "foo", "bar" })
If your datamodel is set to game, children of Roblox services can be imported by starting the path with a name of a service:
-- local module = require(game:GetService("ReplicatedStorage").module)
local module = import "ReplicatedStorage/module"
-- local module = require(game:GetService("ServerStorage").module)
local module = import "ServerStorage/module"
Works for any Roblox instance, so you can use this to import assets as well:
-- local sound = script.Parent:FindFirstChild("Sound")
local sound = import "./Sound"
-- local sound = script.Parent:FindFirstChild("Part")
local part = import "./Part"
You can set aliases to define starting points for your paths:
import.setConfig({
aliases = {
shared = game.ReplicatedStorage.Shared
}
})
-- local module = require(game.ReplicatedStorage.Shared.Module)
local module = import "shared/Module"
You can also configure the module to use WaitForChild, with a configurable timeout
import.setConfig({
useWaitForChild = true
waitForChildTimeout = 1
})
You can configure the alias which you use to represent script
in your paths.
import.setConfig({
currentScriptAlias = "@"
})
-- local module = require(script.Module)
local module = import "@/Module"
By default, import
will throw an error when modulescripts attempt to import eachother in a recursive loop (which would otherwise silently fail). This feature was designed with the assumption the user only has a singular script or localscript as the entry point to the codebase, and you can disable it if the feature causes problems.
import.setConfig({
detectRequireLoops = false
})
Install Rojo and then run the following commands:
$ rojo build -o place.rbxlx
$ rojo serve
Open the newly generated place file and start the Rojo plugin.
From here you can modify anything under src/
and your changes will be synced in.
When you're ready to test, simply press F5 to play.