Efficient IAS Context Changing - What's the Best Way to Implement?

Hello everyone,

This is my first time creating a context based user input system using the new InputActionSystem, and I’m looking for feedback on how I implemented it.
image
In a nutshell, there is an idle context which is always enabled, allowing the user to change context/states by pressing 1,2,3,4, etc. Once the context is changed on the client, it fires a remote letting the server know that the user is attempting to change their context/state.

--------------------------------------------------------------------------------------------------------------------------------
-- [Services] --
--------------------------------------------------------------------------------------------------------------------------------
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--------------------------------------------------------------------------------------------------------------------------------
-- [References] --
--------------------------------------------------------------------------------------------------------------------------------
local Remotes = ReplicatedStorage.Remotes
local StateChange = Remotes.StateChange
local StateClick = Remotes.StateClick

local Contexts = 
	{
		script.Parent.Punch,
		script.Parent.Defense,
		script.Parent.Meditation,
		script.Parent.Sword
	}
local IdleContext = script.Parent.Idle
local IdleEquipPunch = IdleContext.EquipPunch
local IdleEquipDurability = IdleContext.EquipDurability
local IdleEquipMeditation = IdleContext.EquipMeditation
local IdleEquipSword = IdleContext.EquipSword

local PunchContext = script.Parent.Punch	
local Punch = PunchContext.Punch

local DurabilityContext = script.Parent.Defense
local Click = DurabilityContext.Click

local MeditationContext = script.Parent.Meditation

local SwordContext = script.Parent.Sword
local Swing = SwordContext.Swing	
--------------------------------------------------------------------------------------------------------------------------------
-- [Settings] --
--------------------------------------------------------------------------------------------------------------------------------
STATECHANGECOOLDOWN = 1.5
lastStateChange = 0
currentState = 0
--------------------------------------------------------------------------------------------------------------------------------
-- [Idle State] --
--------------------------------------------------------------------------------------------------------------------------------

-- Makes sure all other contexts are disabled
local function closeOtherContext()
	for _,v in ipairs(Contexts) do
		v.Enabled = false
	end
end

-- Implements changing context logic; Debounce
local function changeContext(context,id)
	if(time() - lastStateChange) > STATECHANGECOOLDOWN then
		lastStateChange = time()
		if currentState == 0 then
			-- if idle then change to selected state
			closeOtherContext()
			context.Enabled = true
			currentState = id
			StateChange:FireServer(id)
		elseif currentState == id then
			-- if unequipping then change to idle
			closeOtherContext()
			context.Enabled = false
			currentState = 0
			StateChange:FireServer(0)
		else
			-- if changing from state to state
			closeOtherContext()
			context.Enabled = false
			currentState = id
			StateChange:FireServer(id)
		end
	end
end

-- Runs functions above when keypressed in idle state
IdleEquipPunch.Pressed:Connect(function() changeContext(PunchContext,1) end)
IdleEquipDurability.Pressed:Connect(function() changeContext(DurabilityContext,2) end)
IdleEquipMeditation.Pressed:Connect(function() changeContext(MeditationContext,3) end)
IdleEquipSword.Pressed:Connect(function() changeContext(SwordContext,4) end)

--------------------------------------------------------------------------------------------------------------------------------
-- [Punch State] --
--------------------------------------------------------------------------------------------------------------------------------

Punch.Pressed:Connect(function()
	StateClick:FireServer(1)
end)

--------------------------------------------------------------------------------------------------------------------------------
-- [Durability State] --
--------------------------------------------------------------------------------------------------------------------------------
Click.Pressed:Connect(function()
	StateClick:FireServer(2)
end)

--------------------------------------------------------------------------------------------------------------------------------
-- [Meditation State] --
--------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------
-- [Sword State] --
--------------------------------------------------------------------------------------------------------------------------------
Swing.Pressed:Connect(function()
	StateClick:FireServer(4)
end)

From what I gathered, there is no reason to say context.Enabled = false after calling closeOtherContext.
Also, I think the function to change contexts should only take in the id parameter, because all contexts already have their own unique index in the Contexts table, meaning you can get the context object by simply saying Contexts[id].
This would reduce the amount of code needed to achieve the same functionality you have right here.
Other than that, there’s nothing I’d criticize, since the code looks pretty neat.
I’d like to ask though is there a specific reason you’re using time() instead of tick()?

Thank you for the reply!

That’s a good catch, I should definitely add some kind of table for each context and its corresponding id to reduce unnecessary code. I feel like that’d be a good foundation to build all of the functions around. I plan on modulating this whole process, so I’m going to recode based around a Contexts[id] table.

As for your question about time and tick, a few years ago Roblox stated that they’re going to sunset tick(), so I figured I’d make the switch now. Also, time() feels much more intuitive and useful than tick() since it keeps track of the total time your game instance has been running.