Skip to main content

TypedRemote

Simple networking package that helps create typed RemoteEvents and RemoteFunctions.

-- ReplicatedStorage.Network (ModuleScript)

local TypedRemote = require(ReplicatedStorage.Packages.TypedRemote)

-- Get the RF and RE instance creators, which create RemoteEvents/RemoteFunctions
-- within the given parent (the script in this case):
local RF, RE = TypedRemote.parent(script)

-- Redeclare the TypedRemote types for simplicity:
type RF<T..., R...> = TypedRemote.Function<T..., R...>
type RE<T...> = TypedRemote.Event<T...>

-- Define network table:
return {
	-- RemoteEvent that takes two arguments - a string and a number:
	MyEvent = RE("MyEvent") :: RE<string, number>,

	-- RemoteFunction that takes two arguments (boolean, string) and returns a number:
	MyFunc = RF("MyFunc") :: RF<(boolean, string), (number)>,
}
-- Example usage of the above Network module:

local Network = require(ReplicatedStorage.Network)

-- If you type this out, intellisense will help with what the function signature should be:
Network.MyEvent.OnClientEvent:Connect(function(player, str, num)
	-- Foo
end)

In most cases, the TypedRemote.parent() function will be used to create the memoized RemoteFunction and RemoteEvent builder functions. From there, call the given functions with the desired name per remote.

The TypedRemote.func and TypedRemote.event functions can also be used, but the parent must be supplied to each call, hence the helpful parent() memoizer.

Types

Event<T...>

interface Event<T...> {
OnClientEventSignal<T...>,
OnServerEventPlayerSignal<T...>,
FireClient(
selfEvent<T...>,
playerPlayer,
T...
) → (),
FireAllClients(
selfEvent<T...>,
T...
) → (),
FireServer(
selfEvent<T...>,
T...
) → (),
}

Function<T..., R...>

interface Function<T..., R...> {
InvokeServer(
selfFunction<T...,
R...>,
T...
) → R...,
OnServerInvoke(
playerPlayer,
T...
) → R...,
}

Functions

parent

TypedRemote.parent(parentInstance) → (
(namestring) → RemoteFunction,
(namestring) → RemoteEvent
)

Creates a memoized version of the func and event functions that include the parent in each call.

-- Create RF and RE functions that use the current script as the instance parent:
local RF, RE = TypedRemote.parent(script)

local remoteFunc = RF("RemoteFunc")

func

TypedRemote.func(
namestring,
parentInstance
) → RemoteFunction

Creates a RemoteFunction with name and parents it inside of parent.

If the parent argument is not included or is nil, then it defaults to the parent of this TypedRemote ModuleScript.

event

TypedRemote.event(
namestring,
parentInstance?
) → RemoteEvent

Creates a RemoteEvent with name and parents it inside of parent.

If the parent argument is not included or is nil, then it defaults to the parent of this TypedRemote ModuleScript.

Show raw api
{
    "functions": [
        {
            "name": "parent",
            "desc": "Creates a memoized version of the `func` and `event` functions that include the `parent`\nin each call.\n\n```lua\n-- Create RF and RE functions that use the current script as the instance parent:\nlocal RF, RE = TypedRemote.parent(script)\n\nlocal remoteFunc = RF(\"RemoteFunc\")\n```",
            "params": [
                {
                    "name": "parent",
                    "desc": "",
                    "lua_type": "Instance"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "((name: string) -> RemoteFunction, (name: string) -> RemoteEvent)"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 108,
                "path": "modules/typed-remote/init.luau"
            }
        },
        {
            "name": "func",
            "desc": "Creates a RemoteFunction with `name` and parents it inside of `parent`.\n\nIf the `parent` argument is not included or is `nil`, then it defaults to the parent of\nthis TypedRemote ModuleScript.",
            "params": [
                {
                    "name": "name",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "parent",
                    "desc": "",
                    "lua_type": "Instance"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "RemoteFunction\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 122,
                "path": "modules/typed-remote/init.luau"
            }
        },
        {
            "name": "event",
            "desc": "Creates a RemoteEvent with `name` and parents it inside of `parent`.\n\nIf the `parent` argument is not included or is `nil`, then it defaults to the parent of\nthis TypedRemote ModuleScript.",
            "params": [
                {
                    "name": "name",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "parent",
                    "desc": "",
                    "lua_type": "Instance?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "RemoteEvent\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 141,
                "path": "modules/typed-remote/init.luau"
            }
        }
    ],
    "properties": [],
    "types": [
        {
            "name": "Event<T...>",
            "desc": "",
            "fields": [
                {
                    "name": "OnClientEvent",
                    "lua_type": "Signal<T...>,",
                    "desc": ""
                },
                {
                    "name": "OnServerEvent",
                    "lua_type": "PlayerSignal<T...>,",
                    "desc": ""
                },
                {
                    "name": "FireClient",
                    "lua_type": "(self: Event<T...>, player: Player, T...) -> (),",
                    "desc": ""
                },
                {
                    "name": "FireAllClients",
                    "lua_type": "(self: Event<T...>, T...) -> (),",
                    "desc": ""
                },
                {
                    "name": "FireServer",
                    "lua_type": "(self: Event<T...>, T...) -> (),",
                    "desc": ""
                }
            ],
            "source": {
                "line": 26,
                "path": "modules/typed-remote/init.luau"
            }
        },
        {
            "name": "Function<T..., R...>",
            "desc": "",
            "fields": [
                {
                    "name": "InvokeServer",
                    "lua_type": "(self: Function<T..., R...>, T...) -> R...,",
                    "desc": ""
                },
                {
                    "name": "OnServerInvoke",
                    "lua_type": "(player: Player, T...) -> R...,",
                    "desc": ""
                }
            ],
            "source": {
                "line": 40,
                "path": "modules/typed-remote/init.luau"
            }
        }
    ],
    "name": "TypedRemote",
    "desc": "Simple networking package that helps create typed RemoteEvents and RemoteFunctions.\n\n```lua\n-- ReplicatedStorage.Network (ModuleScript)\n\nlocal TypedRemote = require(ReplicatedStorage.Packages.TypedRemote)\n\n-- Get the RF and RE instance creators, which create RemoteEvents/RemoteFunctions\n-- within the given parent (the script in this case):\nlocal RF, RE = TypedRemote.parent(script)\n\n-- Redeclare the TypedRemote types for simplicity:\ntype RF<T..., R...> = TypedRemote.Function<T..., R...>\ntype RE<T...> = TypedRemote.Event<T...>\n\n-- Define network table:\nreturn {\n\t-- RemoteEvent that takes two arguments - a string and a number:\n\tMyEvent = RE(\"MyEvent\") :: RE<string, number>,\n\n\t-- RemoteFunction that takes two arguments (boolean, string) and returns a number:\n\tMyFunc = RF(\"MyFunc\") :: RF<(boolean, string), (number)>,\n}\n```\n\n```lua\n-- Example usage of the above Network module:\n\nlocal Network = require(ReplicatedStorage.Network)\n\n-- If you type this out, intellisense will help with what the function signature should be:\nNetwork.MyEvent.OnClientEvent:Connect(function(player, str, num)\n\t-- Foo\nend)\n```\n\nIn most cases, the `TypedRemote.parent()` function will be used to create the memoized\nRemoteFunction and RemoteEvent builder functions. From there, call the given functions\nwith the desired name per remote.\n\nThe `TypedRemote.func` and `TypedRemote.event` functions can also be used, but the\nparent must be supplied to each call, hence the helpful `parent()` memoizer.",
    "source": {
        "line": 93,
        "path": "modules/typed-remote/init.luau"
    }
}