My RemoteEvent and RemoteFunction abstraction doesn't work! Please help!1!11!

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

On the client, does Communicator.init return after being called? It’s not waiting infinitely? If you were to print the contents of self at the end, what would it contain?

It displays correctly. Read the text; I told, that the parameter doesn’t go and that’s the main issue. Because I told that the parameter doesn’t go trough, it means that it is all correct except for some issue, which makes the name parameter disapper. Furthermore I printed and all seemed to be well.

I was under the impression that the first thing you would mention would be the more important issue.

Are you expecting name to be passed to functions that you bind? or is the problem that name not being passed altogether?

Do you see in the Module, the self._remote.OnServerEvent?

When I print out the Player, name and … parameters, I get the Player then the … and lastly nil.

That is the issue. The other issue, which I mentioned first, is that the module on the client-side errors, when I call Methods like FireServer and InvokeServer on self._remoteEvent and self._remoteFunction and stops erroring, when I replaced them with script:FindFirstChildWhichIsA.

I am making food in hope someone will resolve my game-breaking issue. A.F.K.

I am back at being online. :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:

I tried running your code and didn’t run across any errors (not even the clientside problem using WaitForChild). Is there anything else that might be causing these problems?

It should be mentioned that whatever you bind using your module, name will not get passed by design.

Are you making sure you’re using method syntax to call the module?

Try to print in the module what it is getting.

You know the .OnServerEvent, before looping trough the Binds. @Denneisk Yes I use :

test-forum-4196599.rbxl (61.0 KB)
image

Scripts in ReplicatedStorage, ServerScriptStorage, StarterPlayer.StarterPlayerScripts.

wth how???