Skip to main content

KnitClient

This item only works when running on the client. Client

Types

Middleware

interface Middleware {}

ClientMiddlewareFn

type ClientMiddlewareFn = (args{any}) → (
shouldContinueboolean,
...any
)

For more info, see ClientComm documentation.

ClientMiddleware

type ClientMiddleware = {ClientMiddlewareFn}

An array of client middleware functions.

PerServiceMiddleware

type PerServiceMiddleware = {[string]Middleware}

ControllerDef

interface ControllerDef {
Namestring
[any]any
}

Used to define a controller when creating it in CreateController.

Controller

interface Controller {
Namestring
[any]any
}

KnitOptions

interface KnitOptions {
ServicePromisesboolean?
MiddlewareMiddleware?
PerServiceMiddlewarePerServiceMiddleware?
}
  • ServicePromises defaults to true and indicates if service methods use promises.
  • Each service will go through the defined middleware, unless the service has middleware defined in PerServiceMiddleware.

Properties

Player

This item is read only and cannot be modified. Read Only
KnitClient.Player: Player

Reference to the LocalPlayer.

Util

This item is read only and cannot be modified. Read Only
KnitClient.Util: Folder

References the Util folder. Should only be accessed when using Knit as a standalone module. If using Knit from Wally, modules should just be pulled in via Wally instead of relying on Knit's Util folder, as this folder only contains what is necessary for Knit to run in Wally mode.

Functions

CreateController

KnitClient.CreateController(controllerDefControllerDef) → Controller

Creates a new controller.

caution

Controllers must be created before calling Knit.Start().

-- Create a controller
local MyController = Knit.CreateController {
	Name = "MyController",
}

function MyController:KnitStart()
	print("MyController started")
end

function MyController:KnitInit()
	print("MyController initialized")
end

AddControllers

KnitClient.AddControllers(parentInstance) → {Controller}

Requires all the modules that are children of the given parent. This is an easy way to quickly load all controllers that might be in a folder.

Knit.AddControllers(somewhere.Controllers)

AddControllersDeep

KnitClient.AddControllersDeep(parentInstance) → {Controller}

Requires all the modules that are descendants of the given parent.

GetService

KnitClient.GetService(serviceNamestring) → Service

Types

interface Service {
[any]any
}

Returns a Service object which is a reflection of the remote objects within the Client table of the given service. Throws an error if the service is not found.

If a service's Client table contains RemoteSignals and/or RemoteProperties, these values are reflected as ClientRemoteSignals and ClientRemoteProperties.

-- Server-side service creation:
local MyService = Knit.CreateService {
	Name = "MyService",
	Client = {
		MySignal = Knit.CreateSignal(),
		MyProperty = Knit.CreateProperty("Hello"),
	},
}
function MyService:AddOne(player, number)
	return number + 1
end

-------------------------------------------------

-- Client-side service reflection:
local MyService = Knit.GetService("MyService")

-- Call a method:
local num = MyService:AddOne(5) --> 6

-- Fire a signal to the server:
MyService.MySignal:Fire("Hello")

-- Listen for signals from the server:
MyService.MySignal:Connect(function(message)
	print(message)
end)

-- Observe the initial value and changes to properties:
MyService.MyProperty:Observe(function(value)
	print(value)
end)
caution

Services are only exposed to the client if the service has remote-based content in the Client table. If not, the service will not be visible to the client. KnitClient.GetService will only work on services that expose remote-based content on their Client tables.

GetController

KnitClient.GetController(controllerNamestring) → Controller

Gets the controller by name. Throws an error if the controller is not found.

GetControllers

KnitClient.GetControllers() → {[string]Controller}

Gets a table of all controllers.

Start

KnitClient.Start(optionsKnitOptions?) → Promise

Starts Knit. Should only be called once per client.

Knit.Start():andThen(function()
	print("Knit started!")
end):catch(warn)

By default, service methods exposed to the client will return promises. To change this behavior, set the ServicePromises option to false:

Knit.Start({ServicePromises = false}):andThen(function()
	print("Knit started!")
end):catch(warn)

OnStart

KnitClient.OnStart() → Promise

Returns a promise that is resolved once Knit has started. This is useful for any code that needs to tie into Knit controllers but is not the script that called Start.

Knit.OnStart():andThen(function()
	local MyController = Knit.GetController("MyController")
	MyController:DoSomething()
end):catch(warn)
Show raw api
{
    "functions": [
        {
            "name": "CreateController",
            "desc": "Creates a new controller.\n\n:::caution\nControllers must be created _before_ calling `Knit.Start()`.\n:::\n```lua\n-- Create a controller\nlocal MyController = Knit.CreateController {\n\tName = \"MyController\",\n}\n\nfunction MyController:KnitStart()\n\tprint(\"MyController started\")\nend\n\nfunction MyController:KnitInit()\n\tprint(\"MyController initialized\")\nend\n```",
            "params": [
                {
                    "name": "controllerDef",
                    "desc": "",
                    "lua_type": "ControllerDef"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Controller\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 180,
                "path": "src/KnitClient.luau"
            }
        },
        {
            "name": "AddControllers",
            "desc": "Requires all the modules that are children of the given parent. This is an easy\nway to quickly load all controllers that might be in a folder.\n```lua\nKnit.AddControllers(somewhere.Controllers)\n```",
            "params": [
                {
                    "name": "parent",
                    "desc": "",
                    "lua_type": "Instance"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "{ Controller }\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 200,
                "path": "src/KnitClient.luau"
            }
        },
        {
            "name": "AddControllersDeep",
            "desc": "Requires all the modules that are descendants of the given parent.",
            "params": [
                {
                    "name": "parent",
                    "desc": "",
                    "lua_type": "Instance"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "{ Controller }\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 218,
                "path": "src/KnitClient.luau"
            }
        },
        {
            "name": "GetService",
            "desc": "Returns a Service object which is a reflection of the remote objects\nwithin the Client table of the given service. Throws an error if the\nservice is not found.\n\nIf a service's Client table contains RemoteSignals and/or RemoteProperties,\nthese values are reflected as\n[ClientRemoteSignals](https://sleitnick.github.io/RbxUtil/api/ClientRemoteSignal) and\n[ClientRemoteProperties](https://sleitnick.github.io/RbxUtil/api/ClientRemoteProperty).\n\n```lua\n-- Server-side service creation:\nlocal MyService = Knit.CreateService {\n\tName = \"MyService\",\n\tClient = {\n\t\tMySignal = Knit.CreateSignal(),\n\t\tMyProperty = Knit.CreateProperty(\"Hello\"),\n\t},\n}\nfunction MyService:AddOne(player, number)\n\treturn number + 1\nend\n\n-------------------------------------------------\n\n-- Client-side service reflection:\nlocal MyService = Knit.GetService(\"MyService\")\n\n-- Call a method:\nlocal num = MyService:AddOne(5) --> 6\n\n-- Fire a signal to the server:\nMyService.MySignal:Fire(\"Hello\")\n\n-- Listen for signals from the server:\nMyService.MySignal:Connect(function(message)\n\tprint(message)\nend)\n\n-- Observe the initial value and changes to properties:\nMyService.MyProperty:Observe(function(value)\n\tprint(value)\nend)\n```\n\n:::caution\nServices are only exposed to the client if the service has remote-based\ncontent in the Client table. If not, the service will not be visible\nto the client. `KnitClient.GetService` will only work on services that\nexpose remote-based content on their Client tables.\n:::",
            "params": [
                {
                    "name": "serviceName",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Service\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 285,
                "path": "src/KnitClient.luau"
            }
        },
        {
            "name": "GetController",
            "desc": "Gets the controller by name. Throws an error if the controller\nis not found.",
            "params": [
                {
                    "name": "controllerName",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Controller\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 301,
                "path": "src/KnitClient.luau"
            }
        },
        {
            "name": "GetControllers",
            "desc": "Gets a table of all controllers.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "{ [string]: Controller }\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 315,
                "path": "src/KnitClient.luau"
            }
        },
        {
            "name": "Start",
            "desc": "Starts Knit. Should only be called once per client.\n```lua\nKnit.Start():andThen(function()\n\tprint(\"Knit started!\")\nend):catch(warn)\n```\n\nBy default, service methods exposed to the client will return promises.\nTo change this behavior, set the `ServicePromises` option to `false`:\n```lua\nKnit.Start({ServicePromises = false}):andThen(function()\n\tprint(\"Knit started!\")\nend):catch(warn)\n```",
            "params": [
                {
                    "name": "options",
                    "desc": "",
                    "lua_type": "KnitOptions?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 338,
                "path": "src/KnitClient.luau"
            }
        },
        {
            "name": "OnStart",
            "desc": "Returns a promise that is resolved once Knit has started. This is useful\nfor any code that needs to tie into Knit controllers but is not the script\nthat called `Start`.\n```lua\nKnit.OnStart():andThen(function()\n\tlocal MyController = Knit.GetController(\"MyController\")\n\tMyController:DoSomething()\nend):catch(warn)\n```",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 412,
                "path": "src/KnitClient.luau"
            }
        }
    ],
    "properties": [
        {
            "name": "Player",
            "desc": "Reference to the LocalPlayer.",
            "lua_type": "Player",
            "readonly": true,
            "source": {
                "line": 102,
                "path": "src/KnitClient.luau"
            }
        },
        {
            "name": "Util",
            "desc": "References the Util folder. Should only be accessed when using Knit as\na standalone module. If using Knit from Wally, modules should just be\npulled in via Wally instead of relying on Knit's Util folder, as this\nfolder only contains what is necessary for Knit to run in Wally mode.",
            "lua_type": "Folder",
            "readonly": true,
            "source": {
                "line": 113,
                "path": "src/KnitClient.luau"
            }
        }
    ],
    "types": [
        {
            "name": "Middleware",
            "desc": "",
            "fields": [
                {
                    "name": "Inbound",
                    "lua_type": "ClientMiddleware?",
                    "desc": ""
                },
                {
                    "name": "Outbound",
                    "lua_type": "ClientMiddleware?",
                    "desc": ""
                }
            ],
            "source": {
                "line": 7,
                "path": "src/KnitClient.luau"
            }
        },
        {
            "name": "ClientMiddlewareFn",
            "desc": "For more info, see [ClientComm](https://sleitnick.github.io/RbxUtil/api/ClientComm/) documentation.",
            "lua_type": "(args: {any}) -> (shouldContinue: boolean, ...: any)",
            "source": {
                "line": 18,
                "path": "src/KnitClient.luau"
            }
        },
        {
            "name": "ClientMiddleware",
            "desc": "An array of client middleware functions.",
            "lua_type": "{ClientMiddlewareFn}",
            "source": {
                "line": 25,
                "path": "src/KnitClient.luau"
            }
        },
        {
            "name": "PerServiceMiddleware",
            "desc": "",
            "lua_type": "{[string]: Middleware}",
            "source": {
                "line": 31,
                "path": "src/KnitClient.luau"
            }
        },
        {
            "name": "ControllerDef",
            "desc": "Used to define a controller when creating it in `CreateController`.",
            "fields": [
                {
                    "name": "Name",
                    "lua_type": "string",
                    "desc": ""
                },
                {
                    "name": "[any]",
                    "lua_type": "any",
                    "desc": ""
                }
            ],
            "source": {
                "line": 40,
                "path": "src/KnitClient.luau"
            }
        },
        {
            "name": "Controller",
            "desc": "",
            "fields": [
                {
                    "name": "Name",
                    "lua_type": "string",
                    "desc": ""
                },
                {
                    "name": "[any]",
                    "lua_type": "any",
                    "desc": ""
                }
            ],
            "source": {
                "line": 51,
                "path": "src/KnitClient.luau"
            }
        },
        {
            "name": "Service",
            "desc": "",
            "fields": [
                {
                    "name": "[any]",
                    "lua_type": "any",
                    "desc": ""
                }
            ],
            "source": {
                "line": 61,
                "path": "src/KnitClient.luau"
            }
        },
        {
            "name": "KnitOptions",
            "desc": "- `ServicePromises` defaults to `true` and indicates if service methods use promises.\n- Each service will go through the defined middleware, unless the service\nhas middleware defined in `PerServiceMiddleware`.",
            "fields": [
                {
                    "name": "ServicePromises",
                    "lua_type": "boolean?",
                    "desc": ""
                },
                {
                    "name": "Middleware",
                    "lua_type": "Middleware?",
                    "desc": ""
                },
                {
                    "name": "PerServiceMiddleware",
                    "lua_type": "PerServiceMiddleware?",
                    "desc": ""
                }
            ],
            "source": {
                "line": 76,
                "path": "src/KnitClient.luau"
            }
        }
    ],
    "name": "KnitClient",
    "desc": "",
    "realm": [
        "Client"
    ],
    "source": {
        "line": 94,
        "path": "src/KnitClient.luau"
    }
}