Skip to main content

Network

A package written for easy server and client communication.

Package Attribute Value
__singleton true
__domain shared

Communicators

Communicators can be used to configure and setup communication between the server and client.

To create commuincators, insert a new private Folder anywhere inside your project and name it "Communicators." To create a new commuincator, insert a new ModuleScript inside the folder and name the script, for example "Coins" or "Shop." This will be the name of the communicator.

Communicator script format:
return {
	
	-- Configuration for RemoteEvents
	events = {

		-- List of events
		remotes = {
			'Event'
		},

		-- List of actions for the events
		actions = {

			-- An action that is triggered whenever the remote is fired by the client
			Event = function(client)
				print(`This remote was fired by {client.Name}!`)
			end,
		},
	},
	
	-- Configuration for RemoteFunctions
	functions = {
		
		-- List of functions
		remotes = {
			'Function'
		},

		-- List of actions for the functions
		actions = {

			-- An action that is triggered whenever the remote is invoked by the client
			Event = function(client)
				return `{client.Name} invoked this remote!`
			end,
		},

	}
	
}
Naming Remotes

When naming remotes, make sure to avoid having remotes with the same name.

Using this format introduces a limitation however. If your scripts are structured this way, communicators would not be able to access the Silk singleton object. To fix this, instead of returning a table directly, wrap it with a function with Silk as its first parameter.

Accessing Silk inside communicators:
return function(silk)
	return {
		events = { ... },
		functions = { ... },
	}
end

Adding Communicators

Communicators can be added in using the initializer script. Add the communicators in using the Network.AppendCommunicators method.

Adding communicators during the initialization phase:
-- || initializer.server.lua ||

silk.Packages.Network:AppendCommunicators{ 
	silk.ServerStorage:WaitForChild('Communicators'),
}

Accessing Communicators

To access a communicator, use the method Network.GetCommunicator or call the package itself.

Accessing a communicator
-- Retrieve the Network package
local network = silk.Packages.Network

-- Similar to using Network.GetCommunicator
local remote = network.Communicator{ <Communicator>, <Remote> }

Functions

AppendCommunicators

Network:AppendCommunicators(commDirectories{Folder}) → ()

Use this method to add communicators during the initialization phase.

GetCommunicator

Network:GetCommunicator(...{
communicatorstring,
remotestring
}) → NetworkRemote

Using the communicator and remote name, you can obtain access to the remote. If called by the server, a RemoteEvent or RemoteFunction is returned directly. For clients however, instead of returning the remote Instance directly, only the remote methods are exposed through a table.

Communicators can also be obtained by calling the Network package directly with the same parameters.

Getting a communicator by calling the package
local remote = network{ <Communicator>, <Remote> }
Show raw api
{
    "functions": [
        {
            "name": "AppendCommunicators",
            "desc": "Use this method to add communicators during the initialization phase.",
            "params": [
                {
                    "name": "commDirectories",
                    "desc": "",
                    "lua_type": "{Folder}"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 34,
                "path": "src/essentials/Network.lua"
            }
        },
        {
            "name": "GetCommunicator",
            "desc": "Using the communicator and remote name, you can obtain access to the remote. If called by the server, a [RemoteEvent] or [RemoteFunction] is returned directly. For clients however, instead of returning the remote Instance directly, only the remote methods are exposed through a table.\n\nCommunicators can also be obtained by calling the Network package directly with the same parameters.\n\n##### Getting a communicator by calling the package\n```lua\nlocal remote = network{ <Communicator>, <Remote> }\n```",
            "params": [
                {
                    "name": "...",
                    "desc": "",
                    "lua_type": "{communicator: string, remote: string}"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "NetworkRemote"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 104,
                "path": "src/essentials/Network.lua"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "Network",
    "desc": "A package written for easy server and client communication.\n\n| Package Attribute | Value |\n| --- | --- |\n| __singleton | true |\n| __domain | shared |\n\n---\n\n### Communicators\n\nCommunicators can be used to configure and setup communication between the server and client.\n\nTo create commuincators, insert a new private [Folder] anywhere inside your project and name it \"Communicators.\" To create a new commuincator, insert a new [ModuleScript] inside the folder and name the script, for example \"Coins\" or \"Shop.\" This will be the name of the communicator.\n\n##### Communicator script format:\n```lua\nreturn {\n\t\n\t-- Configuration for RemoteEvents\n\tevents = {\n\n\t\t-- List of events\n\t\tremotes = {\n\t\t\t'Event'\n\t\t},\n\n\t\t-- List of actions for the events\n\t\tactions = {\n\n\t\t\t-- An action that is triggered whenever the remote is fired by the client\n\t\t\tEvent = function(client)\n\t\t\t\tprint(`This remote was fired by {client.Name}!`)\n\t\t\tend,\n\t\t},\n\t},\n\t\n\t-- Configuration for RemoteFunctions\n\tfunctions = {\n\t\t\n\t\t-- List of functions\n\t\tremotes = {\n\t\t\t'Function'\n\t\t},\n\n\t\t-- List of actions for the functions\n\t\tactions = {\n\n\t\t\t-- An action that is triggered whenever the remote is invoked by the client\n\t\t\tEvent = function(client)\n\t\t\t\treturn `{client.Name} invoked this remote!`\n\t\t\tend,\n\t\t},\n\n\t}\n\t\n}\n```\n\n:::caution Naming Remotes\nWhen naming remotes, make sure to avoid having remotes with the same name.\n:::\n\nUsing this format introduces a limitation however. If your scripts are structured this way, communicators would not be able to access the [Silk] singleton object. To fix this, instead of returning a table directly, wrap it with a function with [Silk] as its first parameter.\n\n##### Accessing [Silk] inside communicators:\n```lua\nreturn function(silk)\n\treturn {\n\t\tevents = { ... },\n\t\tfunctions = { ... },\n\t}\nend\n```\n\n---\n\n### Adding Communicators\n\nCommunicators can be added in using the initializer script. Add the communicators in using the `Network.AppendCommunicators` method.\n\n##### Adding communicators during the initialization phase:\n```lua\n-- || initializer.server.lua ||\n\nsilk.Packages.Network:AppendCommunicators{ \n\tsilk.ServerStorage:WaitForChild('Communicators'),\n}\n```\n\n---\n\n### Accessing Communicators\n\nTo access a communicator, use the method [Network.GetCommunicator] or call the package itself.\n\n##### Accessing a communicator\n```lua\n-- Retrieve the Network package\nlocal network = silk.Packages.Network\n\n-- Similar to using Network.GetCommunicator\nlocal remote = network.Communicator{ <Communicator>, <Remote> }\n\n```",
    "source": {
        "line": 248,
        "path": "src/essentials/Network.lua"
    }
}