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.

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)