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
Use this method to add communicators during the initialization phase.
GetCommunicator
Network:
GetCommunicator
(
...:
{
communicator:
string
,
remote:
string
}
) →
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> }