So… Whenever I use ContextActionService, I make a function that handles the actions binded with it using if/elseif, but here’s the problem, if I make 4 or more actions, the function will definitely start to get messy.
It’s kind of a serious issue for me since I don’t know and can’t even think of another method other than if/elseif statements.
Here is an example of the function:
local function ActionHandler(ActionName, InputState, InputObject)
if ActionName == "Holding" then
Holding = InputState == Enum.UserInputState.Begin
elseif ActionName == "Toggle" then
ToggleBool = not ToggleBool
elseif ActionName == "FireRemote" then
ActionRemote:FireServer()
end
end
Is there another option that I could use instead of potentially overusing if/elseif statements?
As they said, a remote system or modules could work. You could alternatively bind actions via module names, and do say
local function ActionHandler(ActionName, InputState, InputObject)
local moduleCode = require(game.Workspace.ModulePlace[ActionName])
moduleCode.RunCode()
end
This modularizes code and makes it easy to find stuff.