My Input Buffer Doesn't Execute Properly

I planned on adding Input Buffer (in case you don’t know, it is a feature that allows you to perform abilities by requiring multiple inputs, instead of single one) to my melee system.

Using and modifying codes from this post, I managed to get it working. However, the problem is when I built a list of patterns like this:

--input order is from top to bottom
local Abilities = {	
	{
		AbilityName = "Test Combo 1";
		AbilityInputPattern = {
			"Down",
			"Right",
			{["Keyboard"] = "Z", ["Controller"] = "ButtonB"},
		};	
		AbilityServerFunc = function(Player, Tool, MouseHit)
			warn("Combo 1 Activated")
		end;
	};
	{
		AbilityName = "Test Combo 2";
		AbilityInputPattern = {
			"Left",
			"Left",
			{["Keyboard"] = "X", ["Controller"] = "ButtonA"},
		};
		AbilityServerFunc = function(Player, Tool, MouseHit)
			warn("Combo 2 Activated")
		end;
	};
	{
		AbilityName = "Long Combo";
		AbilityInputPattern = {
			"Left",
			"Down",
			"Right",
			"Left",
			"Up",
			"Right",
			{["Keyboard"] = "C", ["Controller"] = "ButtonX"},
		};
		AbilityServerFunc = function(Player, Tool, MouseHit)
			warn("Long Combo Activated")
		end;
	};
	{
		AbilityName = "Konami";
		AbilityInputPattern = {
			"Up",
			"Up",
			"Down",
			"Down",
			"Left",
			"Right",
			"Left",
			"Right",
			{["Keyboard"] = "Z", ["Controller"] = "ButtonB"},
			{["Keyboard"] = "X", ["Controller"] = "ButtonA"},
			{["Keyboard"] = "C", ["Controller"] = "ButtonX"},
		};
		AbilityServerFunc = function(Player, Tool, MouseHit)
			warn("Konami Code Activated")
		end;
	};
};

and performed an ability with long, complex input commands, the script did not execute specific action, but often one with shorter pattern. For examples:

-Konami Code:

-Konami Code with arrows only (with additional bug which executed random ability with short inputs even when you did not even form combo pattern)

The code:
local function CheckSequence()
	local BufferIndex = 0
	local LastMatchIndex = 0
	local MatchedInputCount = 0
	for i = 1, #InputPatterns do
		BufferIndex = (#Combo - 1) + 1 --Always stays at 1
		MatchedInputCount = 0
		for ii = #InputPatterns[i], 1, -1 do
			while BufferIndex >= 0 do
				if typeof(InputPatterns[i][ii]) == "table" then --Detects if one of inputs is a table
					local Matched = false
					for name, value in pairs(InputPatterns[i][ii]) do
						if value == Combo[BufferIndex] then
							Matched = true
							break
						end
					end
					if Matched then
						MatchedInputCount += 1
						break
					end
				else
					if InputPatterns[i][ii] == Combo[BufferIndex] then
						MatchedInputCount += 1
						break
					end
				end
				BufferIndex -= 1
			end
		end
		if (MatchedInputCount == #InputPatterns[i]) then
			LastMatchIndex = i
			--Debug
			print("")
			warn("<<<<<RESULT BEGIN>>>>>")
			print("SEQUENCES:")
			print(unpack(Combo))
			print("BEST MATCHING PATTERN:")
			print(unpack(InputPatterns[LastMatchIndex]))
			warn(">>>>>RESULT END<<<<<")
			print("")
			--
			Combo = {}
			return InputPatterns[LastMatchIndex] --Returns best matching combo pattern
		end
	end
	return nil --Returns nil if fails
end

OnComboPerformed = function(Input, Name)
	--Debug
	print("")
	print("INPUT PRESSED: "..Name)
	--
	AddToCombo(Name)
	local BestMatchingCombo = CheckSequence()
	if BestMatchingCombo then					
        --Do something with this pattern
	end
end

Any help or advice is appreciated

Thank you in advance.

Ok so I decided to replace with my own code, and it is finally working now

local function CheckSequence()
	local BestMatchingPattern
	for i = 1, #InputPatterns do
		for ii = 1, #InputPatterns[i] do
			--Break internal loop and move to next index if matched or not (or combo index doesn't exist)
			if not ComboSequence[ii] then
				break
			end
			if ii == 1 or ii < #InputPatterns[i] then
				if typeof(InputPatterns[i][ii]) == "table" then --If input is a table
					local Matched = false
					for n, v in pairs(InputPatterns[i][ii]) do
						if v == ComboSequence[ii] then
							Matched = true
							break
						end
					end
					if not Matched then
						break
					end
				else
					if InputPatterns[i][ii] ~= ComboSequence[ii] then
						break
					end
				end
			else
				if typeof(InputPatterns[i][ii]) == "table" then --Same as above
					local Matched = false
					for n, v in pairs(InputPatterns[i][ii]) do
						if v == ComboSequence[ii] then
							Matched = true
							break
						end
					end
					if Matched then
						if #InputPatterns[i] == #ComboSequence then --If matched and player made full combo
							BestMatchingPattern = InputPatterns[i]
						end
					end
					break
				else
					if InputPatterns[i][ii] == ComboSequence[ii] then
						if #InputPatterns[i] == #ComboSequence then --Same thing
							BestMatchingPattern = InputPatterns[i]
						end
					end
					break
				end
			end
		end
		if BestMatchingPattern then --First iteration
			break
		end
	end
	if BestMatchingPattern then --Second iteration
		ComboSequence = {} --Reset after matching
		return BestMatchingPattern
	end
	return nil --Return nil if fails
end

The code seems to be longer (and a bit hacky), but better than old one