How should I handle events?

When creating remote events and functions for a game, are there any performance differences / issues that may arise if you were to only use one of each type of event, (only one remote event, one remote function, etc.) and instead of determining what the instance does via it’s name, determining it through argument checks?
Ex:
Usually people do:

game:GetService("ReplicatedStorage").KillEvent:Connect(function()
    -- kill event function stuff
end)

vs.

game:GetService("ReplicatedStorage").Event:Connect(function(action)
    if action == "Kill" then
        -- kill event function stuff
    elseif action == "AnotherEvent" then
        -- another event function stuff
    end
end)

Thank you for any input on this!

2 Likes

Neither od these would work. Correct examples should be the following (can change OnServerEvent to OnClientEvent if necessary):

game:GetService("ReplicatedStorage").KillEvent.OnServerEvent:Connect(function()
    -- kill event function stuff
end)

vs.

game:GetService("ReplicatedStorage").Event.OnServerEvent:Connect(function(action)
    if action == "Kill" then
        -- kill event function stuff
    elseif action == "AnotherEvent" then
        -- another event function stuff
    end
end)

Personally, I’d use different RemoteEvents for different tasks, and if you’re working as a team, for decent readability, but it’s all up to you in the end. You can attempt to baffle exploiters with using only one event, but they can use RemoteSpy and easily decipher your intentions.

The 2nd one is better.

I noticed a problem with your code. I noticed you got the error in the output Event is not a valid member of ReplicatedStorage "ReplicatedStorage". To fix this, you need to put a BindableEvent in ReplicatedStorage. Replace all instances of game:GetService("ReplicatedStorage").Event:Connect(function(action) with game:GetService("ReplicatedStorage").BindableEvent.Event:Connect(function(action)

2nd option will use more bandwidth, players have to send remote fire + strings. This should be a negligible difference though. If you have to send a remote event often, say every frame, stick to the first option have a remote event just for that function. If you do anything every frame you should reduce if statements where you can.

Consider this question a matter of style rather than performance dependent.

5 Likes

I don’t think the performance difference is noticeable in most scenarios. This post will better explain it.

2 Likes