Will a bunch of If's and elseif's cause delay/performance downgrade?

So, I want to use one remote for couple of situations instead of making 200 remotes, how I want to do it is that the remote will send a string and according to the string, a script will run.
So for example:

game.ReplicatedStorage.ExampleRemote.OnServerEvent:Connect(function(player,string)
if string == "Hey" then
print("Hey")
elseif string == "Bye" then
print("Bye")
end
end)

However, multiply the amount of elseif’s by 100 different situations.
I also thought of making through a module like this:

local ExampleModule = game.ReplicatedStorage.ExampleModule
game.ReplicatedStorage.ExampleRemote.OnServerEvent:Connect(function(player,word)
ExampleModule:..word -- But it didn't seem possible.
end)

I don’t think it would be causing delaytions, but it will cause inefficient. You can’t try to use a for loop and make an index of the conditions, then loop through every single element in that table where the value will be the condition.

1 Like

I usually use 1 table for this situation.

local condition={}
function condition.Hey()
	-- doing something
end

if condition[key] then
	condition[key]()
end
1 Like

I don’t think it will cause a lot of delays, but you will notice some. But the biggest problem is it will lower its readability. In the end, you are causing the script to be less understandable. Instead of having a lot of elseifs statement, have a table where the key sf the requirement and the value is the result. For example:

local randomTable = {
    ["Hello"] = function()
        print("Hi")
    end,
    ["Number"] = 15
}

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player, Key)
    if randomTable[Key] then
        -- index the table then do the rest like adding "()" which means its a function
    end
end)
1 Like