CAS inputs in a modulescript. Any issues?

Hi guys. I have an idea for my input system to clean up my code a bit and wanted to run it through the forum first. So my idea is to (in a module script) take all the CAS BindActions, bind them to functions that basically just set a bool whether the key is pressed or not, and expose a table containing these bools in the modules return function. This way, I can perform actions on these keybinds in many different scripts by just requiring one input module. Would this work, or would there be noticeable input lag? Are there better ways to do this? Thank you in advance for input :slight_smile:

So I tested it, and it works pretty flawlessly. Since tables are passed by reference, I can literally just do local inputs = inputModule.inputs in the requiring script, and the table will always contain the up-to-date values from the module :smiley:
Here’s the module:

local ContextActionService = game:GetService("ContextActionService")

local InputModule = {}

InputModule.Cmds = {
	upMove = false;
	downMove = false;

	leftMove = false;
	rightMove = false;

	lastUp = false;
	lastLeft = false;

	jump = false;

	sprint = false;

	leftClick=false;
	rightClick=false;
	rightLastClick=false;

	dodge = false;


	QAbil=false;
	EAbil=false;
	Interact=false;

}

local function onUp(actionName, inputState)

	if inputState == Enum.UserInputState.Begin then
		InputModule.Cmds.upMove = true
		InputModule.Cmds.lastUp = true
	elseif inputState == Enum.UserInputState.End then
		InputModule.Cmds.upMove = false
	end

end

local function onLeft(actionName, inputState)

	if inputState == Enum.UserInputState.Begin then
		InputModule.Cmds.leftMove = true
		InputModule.Cmds.lastLeft = true
	elseif inputState == Enum.UserInputState.End then
		InputModule.Cmds.leftMove = false
	end

end

local function onDown(actionName, inputState)

	if inputState == Enum.UserInputState.Begin then
		InputModule.Cmds.downMove = true
		InputModule.Cmds.lastUp = false
	elseif inputState == Enum.UserInputState.End then
		InputModule.Cmds.downMove = false
	end

end

local function onRight(actionName, inputState)

	if inputState == Enum.UserInputState.Begin then
		InputModule.Cmds.rightMove = true
		InputModule.Cmds.lastLeft = false
	elseif inputState == Enum.UserInputState.End then
		InputModule.Cmds.rightMove = false
	end

end

local function onJump(actionName, inputState)

	if inputState == Enum.UserInputState.Begin then
		InputModule.Cmds.jump = true
		InputModule.Cmds.jumpJustPressed = true
	elseif inputState == Enum.UserInputState.End then
		InputModule.Cmds.jump = false
	end

end

local function onSprint(actionName, inputState)

	if inputState == Enum.UserInputState.Begin then
		InputModule.Cmds.sprint = true
	elseif inputState == Enum.UserInputState.End then
		InputModule.Cmds.sprint = false
	end

end

local function onLeftClick (actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		InputModule.Cmds.leftClick=true;
		InputModule.Cmds.rightLastClick=false;
	elseif inputState == Enum.UserInputState.End then
		InputModule.Cmds.leftClick=false;
	end
end

local function onRightClick (actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		InputModule.Cmds.rightClick=true;
		InputModule.Cmds.rightLastClick=true;
	elseif inputState == Enum.UserInputState.End then
		InputModule.Cmds.rightClick=false;
	end
end
local function onDodge (actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		InputModule.Cmds.dodge=true;
	elseif inputState == Enum.UserInputState.End then
		InputModule.Cmds.dodge=false;
	end
end

local function onQAbil(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		InputModule.Cmds.QAbil=true;
	elseif inputState == Enum.UserInputState.End then
		InputModule.Cmds.QAbil=false;
	end
end

local function onEAbil(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		InputModule.Cmds.EAbil=true;
	elseif inputState == Enum.UserInputState.End then
		InputModule.Cmds.EAbil=false;
	end
end

local function onInteract(actionName,inputState)
	if inputState == Enum.UserInputState.Begin then
		InputModule.Cmds.Interact=true;
	elseif inputState == Enum.UserInputState.End then
		InputModule.Cmds.Interact=false;
	end
end

--Action Bindings--

ContextActionService:BindAction("Up", onUp, false, Enum.KeyCode.W)
ContextActionService:BindAction("Left", onLeft, false, Enum.KeyCode.A)
ContextActionService:BindAction("Down", onDown, false, Enum.KeyCode.S)
ContextActionService:BindAction("Right", onRight, false, Enum.KeyCode.D)
ContextActionService:BindAction("Jump", onJump, false, Enum.KeyCode.Space)
ContextActionService:BindAction("Sprint", onSprint, false, Enum.KeyCode.LeftShift)
ContextActionService:BindAction("LeftMouse",onLeftClick,false,Enum.UserInputType.MouseButton1)
ContextActionService:BindAction("RightMouse",onRightClick,false,Enum.UserInputType.MouseButton2)
ContextActionService:BindAction("Dodge",onDodge,false,Enum.KeyCode.V)
ContextActionService:BindAction("QAbil",onQAbil,false,Enum.KeyCode.Q)
ContextActionService:BindAction("EAbil",onEAbil,false,Enum.KeyCode.E)
ContextActionService:BindAction("Interact",onInteract,false,Enum.KeyCode.F)

return InputModule

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.