RbxUtil Documentation

repository·main·Indexed 19 days ago

https://github.com/sleitnick/rbxutil

A comprehensive suite of utility libraries for Roblox developers. RbxUtil provides specialized modules for networking (Net, Comm, TypedRemote), data manipulation (BufferUtil, Ser, TableUtil), physics (PID, Spring, Quaternion), and lifecycle management (Trove). The libraries are available via Wally and npm (@rbxutil) and are compatible with Rojo and roblox-ts.

Tokens
8.9K
Snippets
43
Records
53
Agent score
65%

What's inside RbxUtil

  1. Overview of RbxUtil modules

    main
    RbxUtil is a collection of specialized utility modules for Roblox development. Each module addresses a specific technical need, ranging from networking and buffer manipulation to physics (springs/PID) and object lifecycle management (Trove).
  2. Configure TypeScript types for @rbxutil

    main

    To enable type checking and autocompletion for @rbxutil modules, add the @rbxutil directory to the typeRoots array in your tsconfig.json file. The @rbxts directory should also be present in this list.

    "typeRoots": ["node_modules/@rbxts", "node_modules/@rbxutil"]
  3. Sync Wally packages into Roblox Studio using Rojo

    main

    After installing dependencies with Wally, you must configure Rojo to sync the Packages folder into your Roblox DataModel (typically ReplicatedStorage). Add a mapping for the Packages directory in your default.project.json file.

    {
    	"name": "rbx-util-example",
    	"tree": {
    		"$className": "DataModel",
    		"ReplicatedStorage": {
    			"$className": "ReplicatedStorage",
    			"Packages": {
    				"$path": "Packages"
    			}
    		}
    	}
    }
  4. Install @rbxutil modules via npm

    main

    RbxUtil modules are published under the @rbxutil NPM organization and can be installed like any other roblox-ts package. To install a specific library, such as the quaternion library, use npm install with the package name prefixed by @rbxutil.

    $ npm install @rbxutil/quaternion
  5. Install RbxUtil modules via Wally

    main

    RbxUtil modules are distributed via Wally, a package manager for Roblox.

    1. Run wally init in your project directory to initialize a wally.toml file.
    2. Add the desired utility modules to your [dependencies] section in wally.toml.
    3. Run wally install to download the dependencies into a Packages folder.
    [package]
    name = "your_name/your_project"
    version = "0.1.0"
    registry = "https://github.com/UpliftGames/wally-index"
    realm = "shared"
    
    [dependencies]
    Signal = "sleitnick/signal@^1"
    TableUtil = "sleitnick/table-util@^1"
  6. Configure Rojo for @rbxutil modules

    main

    To ensure that @rbxutil modules are correctly included in your Roblox project, you must add the @rbxutil directory to your default.project.json file. It is recommended to place it in ReplicatedStorage alongside the @rbxts directory.

    "node_modules": {
    	"$className": "Folder",
    	"@rbxts": {
    		"$path": "node_modules/@rbxts"
    	},
    	"@rbxutil": {
    		"$path": "node_modules/@rbxutil"
    	}
    }
  7. How Sequents work

    main

    A Sequent is a signal-like structure that executes connected callbacks in a serial manner. Unlike standard signals where all callbacks run concurrently or independently, each connection in a Sequent must fully complete before the next one begins.

    Key characteristics:

    • Serial Execution: Connections are run one after another.
    • Prioritization: Connections can be assigned a priority to control the order of execution.
    • Cancellable Events: If a Sequent is created with cancellable set to true, a callback can call event:Cancel() to prevent the event from propagating to any subsequent connections in the sequence.
    • Yielding: The Fire method yields until all connections have completed.
    local sequent = Sequent.new(true) -- true enables event cancellation
    
    sequent:Connect(function(event) 
        print("First connection")
        event:Cancel() -- Stops subsequent connections from running
    end, Sequent.Priority.Highest)
    
    sequent:Connect(function(event) 
        print("This won't print!")
    end, Sequent.Priority.Lowest)
    
    sequent:Fire("Test")
  8. Create procedural shake effects with Shake

    main

    The Shake module allows you to create realistic procedural shake effects for cameras or objects.

    To use it:

    1. Create a new instance with Shake.new().
    2. Configure properties like Amplitude, Frequency, and FadeInTime.
    3. Call shake:Start().
    4. Bind the shake to a loop using shake:OnSignal(signal, callback) or shake:BindToRenderStep(name, priority, callback).

    Shakes automatically stop and clean up their connections once they complete. If you need to run multiple shakes with the same configuration simultaneously, use shake:Clone() to create a new instance from a preset.

    Note: shake:Start() must be called before Update, OnSignal, or BindToRenderStep.

    local priority = Enum.RenderPriority.Last.Value
    
    local shake = Shake.new()
    	shake.FadeInTime = 0
    	shake.Frequency = 0.1
    	shake.Amplitude = 5
    	shake.RotationInfluence = Vector3.new(0.1, 0.1, 0.1)
    
    shake:Start()
    	shake:BindToRenderStep(Shake.NextRenderName(), priority, function(pos, rot, isDone)
    		camera.CFrame *= CFrame.new(pos) * CFrame.Angles(rot.X, rot.Y, rot.Z)
    	end)
  9. Create a Stream

    main

    You can initialize a Stream using several methods depending on your source data:

    • stream.create(size): Creates a new stream with a fresh buffer of the specified size in bytes.
    • stream.frombuffer(buf): Wraps an existing buffer in a stream.
    • stream.fromstring(str): Converts a string into a buffer and wraps it in a stream.
    local s = stream.create(4)
    local s_from_buf = stream.frombuffer(existing_buffer)
    local s_from_str = stream.fromstring("hello")
  10. Create a new Signal

    main

    Use Signal.new() to create a fresh Signal instance for event-driven programming. This implementation is batched and yield-safe, meaning handlers can yield without significant performance overhead.

    local signal = Signal.new()
    
    -- Subscribe to a signal:
    signal:Connect(function(msg)
    	print("Got message:", msg)
    end)
    
    -- Dispatch an event:
    signal:Fire("Hello world!")
  11. Use RbxUtil modules in Luau scripts

    main

    To use the installed modules, reference the Packages folder (usually located in ReplicatedStorage) and use require() on the specific module.

    Example workflow:

    1. Locate the Packages folder in ReplicatedStorage.
    2. require the module.
    3. Call the module's API methods.
    -- Reference folder with packages:
    local Packages = game:GetService("ReplicatedStorage").Packages
    
    -- Require the utility modules:
    local Signal = require(Packages.Signal)
    local TableUtil = require(Packages.TableUtil)
    
    -- Use the modules:
    local signal = Signal.new()
    signal:Connect(function(data) 
    	local randomizedData = TableUtil.Shuffle(data)
    	print(randomizedData)
    end)
    signal:Fire({"A", "B", "C"})
  12. Configure Shake properties

    main

    Use the following properties to customize the behavior of a Shake instance:

    • Amplitude (number): The overall magnitude of the shake. Defaults to 1.
    • Frequency (number): The speed of the shake. Defaults to 1.
    • FadeInTime (number): Duration in seconds to fade in to max amplitude. Defaults to 1.
    • FadeOutTime (number): Duration in seconds to fade out after sustain/completion. Defaults to 1.
    • SustainTime (number): How long the shake stays at full amplitude before fading out. Defaults to 0.
    • Sustain (boolean): If true, the shake lasts indefinitely until shake:StopSustain() is called. Defaults to false.
    • PositionInfluence (Vector3): Multiplier for the position offset vector. Defaults to Vector3.one.
    • RotationInfluence (Vector3): Multiplier for the rotation offset vector. Defaults to Vector3.one.
    • TimeFunction (function): A function returning the current time (defaults to time or os.clock).