Functions with multiple uses

I don’t like to have 50 RemoteEvents that all do different things, so I usually have one, but this means that I usually have to pull one of these:

game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent").OnServerEvent:Connect(function(player, id, data1, data2, data3)
	if (id == 1) then
		
	elseif (id == 2) then
		
	elseif (id == 3) then

	end
end)

Is there a better way of using a RemoteEvent/RemoteFunction in multiple ways without having to do the whole id thing?

Maybe store it in a table like this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

local functions = {
   [1] = function(...)
   end),
   [2] = function(...)
   end),
   -- So on
}

RemoteEvent.OnServerEvent:Connect(function(player, id, data1, data2, data3)
   if functions[id] then
     functions[id](data1, data2, data3)
   end
end)
2 Likes

That’s a good way of differentiating between contexts although you may want to use a ‘Context’ parameter instead and use string values as those will have more meaning to you.

local function OnRemoteFired(Player, Context, ...)
	if Context == "Print" then
		print(...)
	elseif Context == "Warn" then
		warn(...)
	end
end
2 Likes

This is just me but I came across this post, I really don’t understand the difference. What is wrong with just putting some if statements inside the remote if your not using a module…?

Oh, there’s nothing wrong with it, it’s mainly about readability. I only did that since the OP said if there was a better way to do this and I gave one. Whether you use it or not that’s completely up to you. Hope that helps and have a good day!

1 Like