ClientComm
Types
ClientMiddlewareFn
type
ClientMiddlewareFn =
(
args:
{
any
}
)
→
(
shouldContinue:
boolean
,
...:
any
)
The middleware function takes 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.
ClientMiddleware
Array of middleware functions.
Functions
new
Constructs a ClientComm object.
If usePromise
is set to true
, then GetFunction
will generate a function that returns a Promise
that resolves with the server response. If set to false
, the function will act like a normal
call to a RemoteFunction and yield until the function responds.
local clientComm = ClientComm.new(game:GetService("ReplicatedStorage"), true)
-- If using a unique namespace with ServerComm, include it as second argument:
local clientComm = ClientComm.new(game:GetService("ReplicatedStorage"), true, "MyNamespace")
GetFunction
ClientComm:
GetFunction
(
) →
(
...:
any
)
→
any
Generates a function on the matching RemoteFunction generated with ServerComm. The function
can then be called to invoke the server. If this ClientComm
object was created with
the usePromise
parameter set to true
, then this generated function will return
a Promise when called.
-- Server-side:
local serverComm = ServerComm.new(someParent)
serverComm:BindFunction("MyFunction", function(player, msg)
return msg:upper()
end)
-- Client-side:
local clientComm = ClientComm.new(someParent)
local myFunc = clientComm:GetFunction("MyFunction")
local uppercase = myFunc("hello world")
print(uppercase) --> HELLO WORLD
-- Client-side, using promises:
local clientComm = ClientComm.new(someParent, true)
local myFunc = clientComm:GetFunction("MyFunction")
myFunc("hi there"):andThen(function(msg)
print(msg) --> HI THERE
end):catch(function(err)
print("Error:", err)
end)
GetSignal
Returns a new ClientRemoteSignal that mirrors the matching RemoteSignal created by
ServerComm with the same matching name
.
local mySignal = clientComm:GetSignal("MySignal")
-- Listen for data from the server:
mySignal:Connect(function(message)
print("Received message from server:", message)
end)
-- Send data to the server:
mySignal:Fire("Hello!")
GetProperty
Returns a new ClientRemoteProperty that mirrors the matching RemoteProperty created by
ServerComm with the same matching name
.
Take a look at the ClientRemoteProperty documentation for more info, such as understanding how to wait for data to be ready.
local mapInfo = clientComm:GetProperty("MapInfo")
-- Observe the initial value of mapInfo, and all subsequent changes:
mapInfo:Observe(function(info)
print("Current map info", info)
end)
-- Check to see if data is initially ready:
if mapInfo:IsReady() then
-- Get the data:
local info = mapInfo:Get()
end
-- Get a promise that resolves once the data is ready (resolves immediately if already ready):
mapInfo:OnReady():andThen(function(info)
print("Map info is ready with info", info)
end)
-- Same as above, but yields thread:
local success, info = mapInfo:OnReady():await()
BuildObject
Returns an object which maps RemoteFunctions as methods and RemoteEvents as fields.
-- Server-side:
serverComm:BindFunction("Test", function(player) end)
serverComm:CreateSignal("MySignal")
serverComm:CreateProperty("MyProperty", 10)
-- Client-side
local obj = clientComm:BuildObject()
obj:Test()
obj.MySignal:Connect(function(data) end)
obj.MyProperty:Observe(function(value) end)
Destroy
ClientComm:
Destroy
(
) →
(
)
Destroys the ClientComm object.