Skip to content

YogurtRamune/AnnotateRBXSignalInstance

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

AnnotateRBXSignalInstance

Overview

This Luau module provides type annotations for Roblox's signal-based instances, including RemoteEvent, RemoteFunction, BindableEvent, and BindableFunction. These annotations enhance type safety when working with client-server communication and event handling in Roblox experiences. By using these exported types, you can leverage Luau's strict typing to catch errors at development time, such as mismatched parameters in event firing or invocation.

The module is written in strict mode (--!strict) and freezes the exported table to prevent modifications.

Key Features

  • Type-Safe Remote Events: Separate parameter types for client-to-server and server-to-client communication.
  • Type-Safe Remote Functions: Handles invocation parameters and return types for both directions.
  • Bindable Events and Functions: Annotations for local (non-networked) signals with parameter and return typing.
  • Compatible with Roblox's Luau type checker.

Installation

  1. Clone the Repository:

    git clone https://github.com/yourusername/your-repo-name.git
    
  2. Sync to Roblox Studio:

    • Use Rojo or another syncing tool to import the module into your Roblox project.
    • Place the AnnotateRBXSignalInstance.luau file in a shared module directory (e.g., ReplicatedStorage.Modules).
  3. Require the Module: In your Luau scripts, require the module like so:

    local Annotate = require(ReplicatedStorage.Modules.AnnotateRBXSignalInstance)

Usage

Import the module and use the exported types to annotate your signal instances. This provides autocompletion and type checking for methods like FireClient, InvokeServer, etc.

Exported Types

  • AnnotateRemoteEvent<ToClient..., ToServer...>:

    • For RemoteEvent instances.
    • ToClient...: Parameters sent from server to client(s).
    • ToServer...: Parameters sent from client to server.
  • AnnotateRemoteFunction<ToClient..., FromClient..., ToServer..., FromServer...>:

    • For RemoteFunction instances.
    • ToClient... / FromClient...: Parameters and returns for server-to-client invocation.
    • ToServer... / FromServer...: Parameters and returns for client-to-server invocation.
  • AnnotateBindableEvent<FiredParameter...>:

    • For BindableEvent instances.
    • FiredParameter...: Parameters fired with the event.
  • AnnotateBindableFunction<Parameter..., Return...>:

    • For BindableFunction instances.
    • Parameter...: Invocation parameters.
    • Return...: Return values from invocation.

Examples

RemoteEvent Example

local Annotate = require(ReplicatedStorage.Modules.AnnotateRBXSignalInstance)

local myEvent: Annotate.AnnotateRemoteEvent<string, number> = Instance.new("RemoteEvent")  -- ToClient: string, ToServer: number

-- Server-side
myEvent:FireAllClients("Hello, world!")  -- Valid: string
myEvent.OnServerEvent:Connect(function(player: Player, value: number)
    print(player.Name, value)  -- Type-checked: expects number
end)

-- Client-side
myEvent:FireServer(42)  -- Valid: number
myEvent.OnClientEvent:Connect(function(message: string)
    print(message)  -- Type-checked: expects string
end)

RemoteFunction Example

local Annotate = require(ReplicatedStorage.Modules.AnnotateRBXSignalInstance)

local myFunc: Annotate.AnnotateRemoteFunction<string, boolean, number, string> = Instance.new("RemoteFunction")  -- ToClient: string, FromClient: boolean, ToServer: number, FromServer: string

-- Server-side
function myFunc.OnServerInvoke(player: Player, value: number): string
    return "Processed: " .. tostring(value)  -- Type-checked
end

-- Client-side
local result: string = myFunc:InvokeServer(100)  -- Valid invocation and return type

BindableEvent Example

local Annotate = require(ReplicatedStorage.Modules.AnnotateRBXSignalInstance)

local myBindable: Annotate.AnnotateBindableEvent<boolean, string> = Instance.new("BindableEvent")

myBindable:Fire(true, "Test")  -- Valid parameters
myBindable.Event:Connect(function(success: boolean, message: string)
    print(success, message)  -- Type-checked
end)

BindableFunction Example

local Annotate = require(ReplicatedStorage.Modules.AnnotateRBXSignalInstance)

local myBindableFunc: Annotate.AnnotateBindableFunction<number, string, boolean> = Instance.new("BindableFunction")  -- Parameters: number, string; Returns: boolean

function myBindableFunc.OnInvoke(value: number, text: string): boolean
    return value > 0 and text ~= ""  -- Type-checked
end

local result: boolean = myBindableFunc:Invoke(5, "Hello")  -- Valid

Requirements

  • Roblox Studio with Luau type checking enabled.
  • Luau version compatible with Roblox (Lua 5.1-based).

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Built for the Roblox developer community to improve type safety in Luau scripts.
  • Inspired by Roblox's evolving type system.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages