Skip to main content

NetFunction

Wrapper for Roblox's RemoteFunctions

Functions

Load

NetFunction.Load(optionsOrIdLoadOptions | string) → NetFunction

Types

interface LoadOptions {
idstring--

the unique idenitifier

}

Load a Roket NetFunction

Read more on the docs.

Define

NetFunction:Define(rawFunction(
resolve(...any) → ...any,
playerPlayer?,
...any
) → ...any) → ()

Define the function on the client, the server, or both

In the passed function, the player is nil only when the function is being called locally by the server.

Examples

See Examples in the docs.

Errors

TypeDescription
ALREADY_DEFINEDThe function has already been defined by the runtime

CallLocal

NetFunction:CallLocal(...any) → ...any

Call the raw defined function locally.

Can only be called on the client. If you meant to locally call the function on the server, just call the function without passing the first "player(s)" arg myFunction("my data")

Errors

TypeDescription
NOT_CLIENTThe function didn't get called on the client

NetFunction()

NetFunction(
any_orServerPlayerOrPlayersPlayer | {Player} | Players | any,
...any
) → ...any | Promise<...any> | Promise<{[Player]{...any}}>

Calls the NetFunction either remotely or locally.

  • Remotely being from the Client to the Server or from the Server to the Client(s)
  • Locally being ran client-side if executed on the Client or ran server-side if executed on the Server

When calling the function locally, actual return values are returned instead of a Promise.

When calling the function remotely, a Promise is returned, containing either the actual return values (if the call was targeted towards a single entity - that is either from Client to Server or from Server to specific Client) or a map of Player -> {...rets} if the call was targeted to multiple entities (that is from Server to multiple Clients).

Below is a cheatsheet illustrating when a function is being called remotely or locally, and what are it's return values for any given call.

Cheatsheet

--#server
	func(...)              -- local call,                      returns ...rets
	func(player, ...)      -- remote call to specific client,  returns Promise<...rets>
	func([plr1, plr2], ...)-- remote call to specific clients, returns Promise<{[Player]: {...rets}}>
	func(game.Players, ...)-- remote call to all clients,      returns Promise<{[Player]: {...rets}}>
--#client
	func(...)           -- remote call to the server, returns Promise<...rets>
	func:CallLocal(...) -- local call, returns ...rets

Examples

local MathService = {
	Add = Roket.NetFunction "Add"
}

function MathService.RoketStart()
--#server
	MathService.Add:Define(function(resolve, player, a, b)
		resolve(a + b)
	end)
--#client
	task.wait(3)
	MathService.Add(3, 5):andThen(print)
--#end
end

return MathService

See more Examples in the docs.

Show raw api
{
    "functions": [
        {
            "name": "Load",
            "desc": "Load a Roket NetFunction\n\nRead more on the [docs](/docs/core_concepts/network_event/network_function).",
            "params": [
                {
                    "name": "optionsOrId",
                    "desc": "",
                    "lua_type": "LoadOptions | string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "NetFunction"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 33,
                "path": "src/roket/module/NetFunction.luau"
            }
        },
        {
            "name": "Define",
            "desc": "Define the function on the client, the server, or both\n\nIn the passed function, the `player` is `nil` only when the function is being called locally by the server.\n\n#### Examples\n\nSee [Examples](/docs/core_concepts/network_event/network_function#examples) in the docs.",
            "params": [
                {
                    "name": "rawFunction",
                    "desc": "",
                    "lua_type": "(resolve: (...any) -> ...any, player: Player?, ...any) -> ...any"
                }
            ],
            "returns": [],
            "function_type": "method",
            "errors": [
                {
                    "lua_type": "ALREADY_DEFINED",
                    "desc": "The function has already been defined by the runtime"
                }
            ],
            "source": {
                "line": 66,
                "path": "src/roket/module/NetFunction.luau"
            }
        },
        {
            "name": "CallLocal",
            "desc": "Call the raw defined function locally.\n\nCan only be called on the client.\nIf you meant to locally call the function on the server, just call the function without passing the first \"player(s)\" arg `myFunction(\"my data\")`",
            "params": [
                {
                    "name": "...",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "...any\n"
                }
            ],
            "function_type": "method",
            "errors": [
                {
                    "lua_type": "NOT_CLIENT",
                    "desc": "The function didn't get called on the client"
                }
            ],
            "source": {
                "line": 111,
                "path": "src/roket/module/NetFunction.luau"
            }
        },
        {
            "name": "__call",
            "desc": "Calls the NetFunction either remotely or locally.\n* *Remotely* being from the Client to the Server or from the Server to the Client(s)\n* *Locally* being ran client-side if executed on the Client or ran server-side if executed on the Server\n\nWhen calling the function locally, actual return values are returned instead of a Promise.\n\nWhen calling the function remotely, a [Promise](https://eryn.io/roblox-lua-promise/api/Promise) is returned,\ncontaining either the actual return values\n(if the call was targeted towards a single entity - that is either from Client to Server or from Server to specific Client)\nor a map of `Player -> {...rets}` if the call was targeted to multiple entities (that is from Server to multiple Clients).\n\nBelow is a cheatsheet illustrating when a function is being called remotely or locally,\nand what are it's return values for any given call.\n\n#### Cheatsheet\n\n```lua\n--#server\n\tfunc(...)              -- local call,                      returns ...rets\n\tfunc(player, ...)      -- remote call to specific client,  returns Promise<...rets>\n\tfunc([plr1, plr2], ...)-- remote call to specific clients, returns Promise<{[Player]: {...rets}}>\n\tfunc(game.Players, ...)-- remote call to all clients,      returns Promise<{[Player]: {...rets}}>\n--#client\n\tfunc(...)           -- remote call to the server, returns Promise<...rets>\n\tfunc:CallLocal(...) -- local call, returns ...rets\n```\n\n#### Examples\n\n```lua\nlocal MathService = {\n\tAdd = Roket.NetFunction \"Add\"\n}\n\nfunction MathService.RoketStart()\n--#server\n\tMathService.Add:Define(function(resolve, player, a, b)\n\t\tresolve(a + b)\n\tend)\n--#client\n\ttask.wait(3)\n\tMathService.Add(3, 5):andThen(print)\n--#end\nend\n\nreturn MathService\n```\n\nSee more [Examples](/docs/core_concepts/network_event/network_function#examples) in the docs.",
            "params": [
                {
                    "name": "any_orServerPlayerOrPlayers",
                    "desc": "",
                    "lua_type": "Player | {Player} | Players | any"
                },
                {
                    "name": "...",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "...any | Promise<...any> | Promise<{[Player]: {...any}}>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 170,
                "path": "src/roket/module/NetFunction.luau"
            }
        }
    ],
    "properties": [],
    "types": [
        {
            "name": "LoadOptions",
            "desc": "",
            "fields": [
                {
                    "name": "id",
                    "lua_type": "string",
                    "desc": "the unique idenitifier"
                }
            ],
            "source": {
                "line": 22,
                "path": "src/roket/module/NetFunction.luau"
            }
        }
    ],
    "name": "NetFunction",
    "desc": "Wrapper for Roblox's RemoteFunctions",
    "source": {
        "line": 14,
        "path": "src/roket/module/NetFunction.luau"
    }
}