How can I fix this move stacking bug?

I’m working on a system that detects when you start holding a key to charge up a skill, and when you let go of the key to release it.
I’m worried about players abusing a possible bug that may allow them to ‘movestack’. Currently, I have a function that detects when the InputBegan, and a function that detects when the InputEnded. Both of them check if the Input is processed, so you don’t accidentally use a skill while typing in chat. Because of this, if you first press the key, then open the chat and let go of the key, it won’t fire the event for when the input ended which allows you to close the chat and press the key again without the original input ending.

Sorry if the explanation was confusing, I’ll attach a video of the bug in action (connected it to print() to give a better understanding in output).

Here’s the code:

local Player = game.Players.LocalPlayer
repeat wait() until game:IsLoaded()
local Character = Player.Character
local UserInputService = game:GetService("UserInputService")

local function KeycodeInputSender(KeyCode, State)
	for i,v in pairs(Character.Values.Inputs:GetChildren()) do
		if v.Name == KeyCode then
			print(State.." pressing "..KeyCode)
			v:FireServer(State)
		end
	end
end

UserInputService.InputBegan:Connect(function(Input, Processed)
	if Processed then return end
	if not Input.KeyCode then return end
	KeycodeInputSender(Input.KeyCode.Name, "Began")
end)
UserInputService.InputEnded:Connect(function(Input, Processed)
	if not Input.KeyCode then return end
	KeycodeInputSender(Input.KeyCode.Name, "Ended")
end)

Solutions I have tried:

  1. Removing the check for processed in the second function, that detects when you stop holding it, results in the same bug except it ends the input twice instead of starting it twice.
  2. Removing the check for processed in the first function, that detects when you start pressing it, same result as “solution” #1.
  3. Super unideal but removing it from both, and results in the same thing as the last two.

If there’s something I should know for future posts or any further information you need please let me know it’s my first time posting :sweat_smile:

I would Recommend Detecting or Writing the move name or move in a Table and check every time there doing a move, then when they do a 2nd skill the server will call the module to check if there doing any move and cancel it.
1st move
Client → Sends the Move Data → Server Detects it → sends move is called to a module that is going to temp save the data in a table

2nd Move
Let say they do move stack this will happen then
Client → Sends the Move Data → Server Detects it → if the Module will check if there doing a move and then Return false or whatever to cancel the move