Skip to main content

ServerComm

This item only works when running on the server. Server

Types

ServerMiddlewareFn

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

The middleware function takes the client player and the arguments (as a table array), and should return true|false to indicate if the process should continue.

If returning false, the optional varargs after the false are used as the new return values to whatever was calling the middleware.

ServerMiddleware

type ServerMiddleware = {ServerMiddlewareFn}

Array of middleware functions.

Functions

new

ServerComm.new(
parentInstance,
namespacestring?
) → ServerComm

Constructs a ServerComm object. The namespace parameter is used in cases where more than one ServerComm object may be bound to the same object. Otherwise, a default namespace is used.

local serverComm = ServerComm.new(game:GetService("ReplicatedStorage"))

-- If many might exist in the given parent, use a unique namespace:
local serverComm = ServerComm.new(game:GetService("ReplicatedStorage"), "MyNamespace")

BindFunction

ServerComm:BindFunction(
namestring,
fn(
playerPlayer,
...any
) → ...any,
inboundMiddlewareServerMiddleware?,
outboundMiddlewareServerMiddleware?
) → RemoteFunction

Creates a RemoteFunction and binds the given function to it. Inbound and outbound middleware can be applied if desired.

local function GetSomething(player: Player)
	return "Something"
end

serverComm:BindFunction("GetSomething", GetSomething)

WrapMethod

ServerComm:WrapMethod(
tbltable,
namestring,
inboundMiddlewareServerMiddleware?,
outboundMiddlewareServerMiddleware?
) → RemoteFunction

Binds a function to a table method. The name must match the name of the method in the table. The same name will be used on the client to access the given function.

local MyObject = {
	_Data = 10,
}

function MyObject:GetData(player: Player)
	return self._Data
end

serverComm:WrapMethod(MyObject, "GetData")

CreateSignal

ServerComm:CreateSignal(
namestring,
unreliableboolean?,
inboundMiddlewareServerMiddleware?,
outboundMiddlewareServerMiddleware?
) → RemoteSignal

Creates a signal that can be used to fire data to the clients or receive data from the clients.

By default, signals use RemoteEvents internally. However, if the unreliable argument is set to true, then an UnreliableRemoteEvent will be used instead.

local mySignal = serverComm:CreateSignal("MySignal")

-- Examples of firing in different ways (see docs for RemoteSignal for further info):
mySignal:Fire(somePlayer, "Hello world")
mySignal:FireAll("Hi there")
mySignal:FireExcept(somePlayer, "Hello everyone except " .. somePlayer.Name)
mySignal:FireFilter(function(player) return player.Team == someCoolTeam end, "Hello cool team")

-- Example of listening for clients to send data:
mySignal:Connect(function(player, message)
	print("Got a message from " .. player.Name .. ":", message)
end)

CreateProperty

ServerComm:CreateProperty(
namestring,
initialValueany,
inboundMiddlewareServerMiddleware?,
outboundMiddlewareServerMiddleware?
) → RemoteProperty

Create a property object which will replicate its property value to the clients. Optionally, specific clients can be targeted with different property values.

local comm = Comm.ServerComm.new(game:GetService("ReplicatedStorage"))

local mapInfo = comm:CreateProperty("MapInfo", {
	MapName = "TheAwesomeMap",
	MapDuration = 60,
})

-- Change the data:
mapInfo:Set({
	MapName = "AnotherMap",
	MapDuration = 30,
})

-- Change the data for one player:
mapInfo:SetFor(somePlayer, {
	MapName = "ASpecialMapForYou",
	MapDuration = 90,
})

-- Change data based on a predicate function:
mapInfo:SetFilter(function(player)
	return player.Team == game.Teams.SomeSpecialTeam
end, {
	MapName = "TeamMap",
	MapDuration = 20,
})

Destroy

ServerComm:Destroy() → ()

Destroy the ServerComm object.

Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "Constructs a ServerComm object. The `namespace` parameter is used\nin cases where more than one ServerComm object may be bound\nto the same object. Otherwise, a default namespace is used.\n\n```lua\nlocal serverComm = ServerComm.new(game:GetService(\"ReplicatedStorage\"))\n\n-- If many might exist in the given parent, use a unique namespace:\nlocal serverComm = ServerComm.new(game:GetService(\"ReplicatedStorage\"), \"MyNamespace\")\n```",
            "params": [
                {
                    "name": "parent",
                    "desc": "",
                    "lua_type": "Instance"
                },
                {
                    "name": "namespace",
                    "desc": "",
                    "lua_type": "string?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "ServerComm"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 44,
                "path": "modules/comm/Server/ServerComm.luau"
            }
        },
        {
            "name": "BindFunction",
            "desc": "Creates a RemoteFunction and binds the given function to it. Inbound\nand outbound middleware can be applied if desired.\n\n```lua\nlocal function GetSomething(player: Player)\n\treturn \"Something\"\nend\n\nserverComm:BindFunction(\"GetSomething\", GetSomething)\n```",
            "params": [
                {
                    "name": "name",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "fn",
                    "desc": "",
                    "lua_type": "(player: Player, ...: any) -> ...: any"
                },
                {
                    "name": "inboundMiddleware",
                    "desc": "",
                    "lua_type": "ServerMiddleware?"
                },
                {
                    "name": "outboundMiddleware",
                    "desc": "",
                    "lua_type": "ServerMiddleware?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "RemoteFunction"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 76,
                "path": "modules/comm/Server/ServerComm.luau"
            }
        },
        {
            "name": "WrapMethod",
            "desc": "Binds a function to a table method. The name must match the\nname of the method in the table. The same name will be used\non the client to access the given function.\n\n```lua\nlocal MyObject = {\n\t_Data = 10,\n}\n\nfunction MyObject:GetData(player: Player)\n\treturn self._Data\nend\n\nserverComm:WrapMethod(MyObject, \"GetData\")\n```",
            "params": [
                {
                    "name": "tbl",
                    "desc": "",
                    "lua_type": "table"
                },
                {
                    "name": "name",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "inboundMiddleware",
                    "desc": "",
                    "lua_type": "ServerMiddleware?"
                },
                {
                    "name": "outboundMiddleware",
                    "desc": "",
                    "lua_type": "ServerMiddleware?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "RemoteFunction"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 108,
                "path": "modules/comm/Server/ServerComm.luau"
            }
        },
        {
            "name": "CreateSignal",
            "desc": "Creates a signal that can be used to fire data to the clients\nor receive data from the clients.\n\nBy default, signals use RemoteEvents internally. However, if\nthe `unreliable` argument is set to `true`, then an\nUnreliableRemoteEvent will be used instead.\n\n```lua\nlocal mySignal = serverComm:CreateSignal(\"MySignal\")\n\n-- Examples of firing in different ways (see docs for RemoteSignal for further info):\nmySignal:Fire(somePlayer, \"Hello world\")\nmySignal:FireAll(\"Hi there\")\nmySignal:FireExcept(somePlayer, \"Hello everyone except \" .. somePlayer.Name)\nmySignal:FireFilter(function(player) return player.Team == someCoolTeam end, \"Hello cool team\")\n\n-- Example of listening for clients to send data:\nmySignal:Connect(function(player, message)\n\tprint(\"Got a message from \" .. player.Name .. \":\", message)\nend)\n```",
            "params": [
                {
                    "name": "name",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "unreliable",
                    "desc": "",
                    "lua_type": "boolean?"
                },
                {
                    "name": "inboundMiddleware",
                    "desc": "",
                    "lua_type": "ServerMiddleware?"
                },
                {
                    "name": "outboundMiddleware",
                    "desc": "",
                    "lua_type": "ServerMiddleware?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "RemoteSignal"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 146,
                "path": "modules/comm/Server/ServerComm.luau"
            }
        },
        {
            "name": "CreateProperty",
            "desc": "Create a property object which will replicate its property value to\nthe clients. Optionally, specific clients can be targeted with\ndifferent property values.\n\n```lua\nlocal comm = Comm.ServerComm.new(game:GetService(\"ReplicatedStorage\"))\n\nlocal mapInfo = comm:CreateProperty(\"MapInfo\", {\n\tMapName = \"TheAwesomeMap\",\n\tMapDuration = 60,\n})\n\n-- Change the data:\nmapInfo:Set({\n\tMapName = \"AnotherMap\",\n\tMapDuration = 30,\n})\n\n-- Change the data for one player:\nmapInfo:SetFor(somePlayer, {\n\tMapName = \"ASpecialMapForYou\",\n\tMapDuration = 90,\n})\n\n-- Change data based on a predicate function:\nmapInfo:SetFilter(function(player)\n\treturn player.Team == game.Teams.SomeSpecialTeam\nend, {\n\tMapName = \"TeamMap\",\n\tMapDuration = 20,\n})\n```",
            "params": [
                {
                    "name": "name",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "initialValue",
                    "desc": "",
                    "lua_type": "any"
                },
                {
                    "name": "inboundMiddleware",
                    "desc": "",
                    "lua_type": "ServerMiddleware?"
                },
                {
                    "name": "outboundMiddleware",
                    "desc": "",
                    "lua_type": "ServerMiddleware?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "RemoteProperty"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 195,
                "path": "modules/comm/Server/ServerComm.luau"
            }
        },
        {
            "name": "Destroy",
            "desc": "Destroy the ServerComm object.",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 207,
                "path": "modules/comm/Server/ServerComm.luau"
            }
        }
    ],
    "properties": [],
    "types": [
        {
            "name": "ServerMiddlewareFn",
            "desc": "The middleware function takes the client player and the arguments (as a table array), and should\nreturn `true|false` to indicate if the process should continue.\n\nIf returning `false`, the optional varargs after the `false` are used as the new return values\nto whatever was calling the middleware.",
            "lua_type": "(player: Player, args: {any}) -> (shouldContinue: boolean, ...: any)",
            "source": {
                "line": 25,
                "path": "modules/comm/Server/ServerComm.luau"
            }
        },
        {
            "name": "ServerMiddleware",
            "desc": "Array of middleware functions.",
            "lua_type": "{ServerMiddlewareFn}",
            "source": {
                "line": 30,
                "path": "modules/comm/Server/ServerComm.luau"
            }
        }
    ],
    "name": "ServerComm",
    "desc": "",
    "realm": [
        "Server"
    ],
    "source": {
        "line": 13,
        "path": "modules/comm/Server/ServerComm.luau"
    }
}