I can only make it closer to working on the client, by replacing all self._remoteEvent and self._remoteFunction by script:FindFirstChildwhichIsA("RemoteEvent") and for the RemoteFunction the same. But I feel like it would be a hack and I thought, maybe someone smarter can figure it out. Furthermore after getting the Events correctly, in the .OnServerEvent part of the code, instead of getting the name parameter, it is replaced by the agruments after it. Although the player argument is passed correctly.
If you see any additional bugs, please notify me!
Here is the code:
local Players = game:GetService("Players")
local isServer = Players.LocalPlayer == nil
local Communicator = {} :: Communicator
function Communicator.init(self: Communicator, Modules)
if isServer then
self._AsyncBindsServer = {} :: { [string]: { (Player, ...any) -> () } }
self._SyncBindsServer = {} :: { [string]: { (Player, ...any) -> any } }
self._remoteEvent = Instance.new("RemoteEvent", script)
self._remoteFunction = Instance.new("RemoteFunction", script)
self._remoteEvent.OnServerEvent:Connect(function(player: Player, name: string, ...)
local Binds = self._AsyncBindsServer[name]
if not Binds then
return
end
for _, bind in Binds do
bind(player, ...)
end
end)
self._remoteFunction.OnServerInvoke = function(player: Player, name: string, ...)
local Binds = self._SyncBindsServer[name]
if not Binds then
return
end
for _, bind in Binds do
return bind(player, ...)
end
return
end
else
self._AsyncBindsClient = {} :: { [string]: { (...any) -> () } }
self._remoteEvent = script:WaitForChild("RemoteEvent")
self._remoteFunction = script:WaitForChild("RemoteFunction")
self._remoteEvent.OnClientEvent:Connect(function(name: string, ...)
local Binds = self._AsyncBindsClient[name]
if not Binds or #Binds < 1 then
return
end
for _, bind in Binds do
bind(...)
end
end)
end
end
if isServer then
function Communicator.sendAsyncToClient(self: Communicator, Receivers: { Players }, name: string, ...)
for _, player in Receivers do
self._remoteEvent:FireClient(player, name, ...)
end
end
function Communicator.bindAsyncOnServer(self: Communicator, name: string, func: (Player, ...any) -> ())
if not self._AsyncBindsServer[name] then
self._AsyncBindsServer[name] = {}
end
table.insert(self._AsyncBindsServer[name], func)
end
function Communicator.bindSyncOnServer(self: Communicator, name: string, func: (Player, ...any) -> any)
if not self._SyncBindsServer[name] then
self._SyncBindsServer[name] = {}
end
table.insert(self._SyncBindsServer[name], func)
end
else
function Communicator.sendAsyncToServer(self: Communicator, name: string, ...)
self._remoteEvent:FireServer(name, ...)
end
function Communicator.bindAsyncOnClient(self: Communicator, name: string, func: (...any) -> ())
if not self._AsyncBindsClient[name] then
self._AsyncBindsClient[name] = {}
end
table.insert(self._AsyncBindsClient[name], func)
end
function Communicator.sendSyncToServer<T>(self: Communicator, name: string, ...): T
return self._remoteFunction:InvokeServer(name, ...)
end
end
type Communicator = typeof(Communicator)
return Communicator
