Optimizing amount of scripts & remote events

So I want to know if there’s any method optimizing this.

I’ve tried making it through Instance.new() but it almost always stated that it’s either nil, argument or this doesn’t exist, also tried finding any solutions in other posts accross devforum, found only detailed info on Instance.new()

So, is there any solutions compressing this?

You can use only one remote with an argument saying what function you want to trigger, that’s what I’m doing for my game.
Furthermore, you should be using only one local script/ script that handles every remotes triggers instead of having many, that would be much much easier and more optimized, if that interests you, I can provide you some script of my ongoing game which uses a single remote architecture which is (in my opinion at least) much easier as you don’t need to bother with adding new receivers everytime.

Yeah you can improve this.

First of all, don’t use ‘s’ as a variable.

Then, you don’t need all those scripts for this. Use 1 remote event, and 1 local script. The remote event should send the information, so you are not hard coding your labels.

Read this documentation: Remote Events and Callbacks | Documentation - Roblox Creator Hub

1 Like

For example, here is my RemotesManager I use for server-side communications:
(don’t mind the token system)

local module = {}

local replicatedStorage = game:GetService("ReplicatedStorage")
local firewall = script.Parent.Firewall

function module.OnServerEvent(player, func_name:string, token, ...)
	if func_name then
		if require(firewall).checkToken(player, token) then
			module.functions[func_name](player, ...)
		else
			warn("invalid token")
		end
	end
end

function module.OnServerInvoke(player, func_name:string, token, ...)
	if require(firewall).checkToken(player, token) then
		return module.functions[func_name](player, ...)
	else
		warn("invalid token")
	end
end

function module:FireClient(player, context:string, ...)
	local remote = replicatedStorage.Remotes:FindFirstChildWhichIsA("RemoteEvent", true)
	remote:FireClient(player, context, ...)
end

function module:InvokeClient(player, context:string, ...)
	local remote = replicatedStorage.Remotes:FindFirstChildWhichIsA("RemoteFunction", true)
	remote:InvokeServer(player, context, ...)
end

module.functions = {
	ping = function() return true end,
	GetData = require(script.Parent.Datastores).returnData,
	changeMap = require(script.Parent.Datastores).changeMap,
	cutscenePlayed = require(script.Parent.Datastores).saveCutscene,
	new_note = require(script.Parent.Notes).newNote,
	get_notes = require(script.Parent.Notes).retrieveNotes,
}


return module

and simply a normal script which contains those lines:

replicatedStorage.Remotes:FindFirstChildWhichIsA("RemoteEvent").OnServerEvent:Connect(function(...) require(remotesManager).OnServerEvent(...) end)
replicatedStorage.Remotes:FindFirstChildWhichIsA("RemoteFunction").OnServerInvoke = require(remotesManager).OnServerInvoke

That way, you only have one remote, and you just have to add the path to your function without any other modification!

1 Like

Hey, thanks.

Helped me to visualize script & remotes compression.
Yet I was a little confused on module.functions that you’ve made, is it just from yours game or it is required to be in module script?

1 Like

It’s basically a table with the list of functions,
for example, let’s say you want to fire a remote when a player presses a button, you’ll fire the remote with button_pressed (or any name) and the rest of your arguments, then, in your table you will put button_pressed = your_function.
so whenever the remote is triggered with “button_pressed” argument, it’ll trigger that function with the other arguments.
In conclusion, those names are just for my game, those are the functions triggered from remotes.

Okay, that was actually useful for me.
Thanks for solution, appreciated.

I might rework on the way I made my module script tho, it works well and is a bit faster than my old receiver hell.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.