@hn-studios/script
Roblox TypeScript Script Package

A TypeScript package for Roblox that allows easy creation and management of different script types (Server Scripts, Local Scripts, and Module Scripts) with a clean, type-safe API.
Features
- Create Server Scripts, Local Scripts, and Module Scripts
- Type-safe script creation and management
- Automatic context validation (Server/Client)
- Error handling and lifecycle management
- Easy script control (Start/Stop/Destroy)
- Module script support with require functionality
Important Configuration
-
Update tsconfig.json
Add @hn-studios
to your typeRoots
:
{
"compilerOptions": {
"typeRoots": [
"node_modules/@rbxts",
"node_modules/@hn-studios",
"node_modules/@types"
]
}
}
-
Update default.project.json
Add the @hn-studios
scope to your Rojo configuration:
{
"ReplicatedStorage": {
"$className": "ReplicatedStorage",
"rbxts_include": {
"$path": "include",
"node_modules": {
"$className": "Folder",
"@rbxts": {
"$path": "node_modules/@rbxts"
},
"@hn-studios": {
"$path": "node_modules/@hn-studios"
}
}
}
}
}
Installation
-
Install the package using npm:
npm install @hn-studios/script
-
Add it to your Roblox TypeScript project:
import { Script } from "@hn-studios/script";
Usage
Basic Script Creation
import { Workspace, ServerStorage, ReplicatedStorage, StarterGui, Players } from "@rbxts/services";
const serverStorage = ServerStorage;
const replicatedStorage = ReplicatedStorage;
const playerGui = Players.LocalPlayer.WaitForChild("PlayerGui");
const serverScript = new Script(() => {
print("Server script running!");
}, serverStorage, {
Type: "Script",
Name: "MyServerScript",
});
const localScript = new Script(() => {
print("Client script running!");
}, playerGui, {
Type: "LocalScript",
Name: "MyLocalScript",
});
const moduleScript = new Script(() => {
const MyModule = {};
function MyModule.doSomething() {
print("Module function called!");
}
return MyModule;
}, replicatedStorage, {
Type: "ModuleScript",
Name: "MyModule",
});
Script Control
serverScript.Start();
serverScript.Stop();
serverScript.Destroy();
const myModule = moduleScript.Require() as { doSomething: () => void };
myModule.doSomething();
Script Options
interface ScriptOptions {
Name?: string;
Disabled?: boolean;
Type?: ScriptType;
RunContext?: "Server" | "Client" | "Both";
}
API Reference
Script Class
Constructor
constructor(callback: () => void, parent?: Instance, options?: ScriptOptions)
Methods
SetParent(parent: Instance)
: Sets the parent of the script instance.
RemoveParent()
: Removes the parent of the script instance (sets it to nil).
GetName()
: Gets the script's name.
SetName(value: string)
: Sets the script's name.
GetParent()
: Gets the script's parent instance.
GetInstance()
: Gets the underlying Roblox instance.
GetScriptType()
: Gets the script type.
Start()
: Starts script execution.
Stop()
: Stops script execution.
Destroy()
: Cleans up the script and destroys the instance.
Require()
: Requires a module script (ModuleScript only). Returns the module's exported content.
Example Use Cases
Server-Side Logic
import { Script } from "@hn-studios/script";
import { Players, ServerScriptService } from "@rbxts/services";
const serverScript = new Script(() => {
Players.PlayerAdded.Connect((player) => {
print(`${player.Name} joined the game!`);
});
}, ServerScriptService, {
Type: "Script",
Name: "PlayerManager",
});
serverScript.Start();
Client-Side UI
import { Script } from "@hn-studios/script";
import { StarterGui, Players } from "@rbxts/services";
const LocalPlayer = Players.LocalPlayer;
const playerGui = LocalPlayer.WaitForChild("PlayerGui");
const uiScript = new Script(() => {
const button = new Instance("TextButton");
button.Text = "Click Me!";
button.Size = new UDim2(0.5, 0, 0.2, 0);
button.Position = new UDim2(0.25, 0, 0.4, 0);
button.Parent = playerGui;
button.MouseButton1Click.Connect(() => {
print("Button clicked!");
});
}, playerGui, {
Type: "LocalScript",
Name: "UIHandler",
});
uiScript.Start();
Shared Module
import { Script } from "@hn-studios/script";
import { ReplicatedStorage } from "@rbxts/services";
const utilityModule = new Script(() => {
const UtilityModule = {};
function UtilityModule.formatTime(seconds: number) {
const minutes = math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
return `${minutes}:${remainingSeconds < 10 ? "0" : ""}${remainingSeconds}`;
}
return UtilityModule;
}, ReplicatedStorage, {
Type: "ModuleScript",
Name: "UtilityModule",
});
const utils = utilityModule.Require() as { formatTime: (seconds: number) => string };
const formattedTime = utils.formatTime(125);
print(formattedTime);
Best Practices
- Always specify the script type explicitly in
options
.
- Use appropriate parent containers for each script type:
ServerScriptService
or ServerStorage
for Server Scripts
StarterPlayerScripts
or StarterGui
for Local Scripts
ReplicatedStorage
for shared Module Scripts
- Handle errors in your callbacks to prevent unexpected behavior.
- Clean up scripts using
Destroy()
when they're no longer needed to avoid memory leaks.
License
MIT License - Feel free to use this package in your Roblox games and modify it as needed.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.