Key Combinations

I’m trying to make a fighting game with notation for moves; however it’s proving difficult to make this notation. The current way I’m doing this is as following:

UIS.InputBegan:Connect(function(key)
    if UIS:IsKeyDown(Enum.KeyCode.R) and key.KeyCode == Enum.KeyCode.A then
        --stuff
    end
end)

However, this is pretty inconsistent. I’m wondering if there is a more conventional way of doing this, or perhaps using ContextActionService?

1 Like

You can prepare functions that handling keydowns for your own use

Each key press is an independent action: there are no built-in methods to detect this kind of multi-press combo.

This /r/gamedev thread explores the “standard” method that fighting games especially use to process commands: a circular input buffer with rules for pattern matching and “clearing” the buffer.

How would I handle more than two input combinations? I’ve gotten a working prototype using this method.

You could do something like

local contextActionService = game:GetService("ContextActionService")
local userInputService = game:GetService("UserInputService")

local function checkKeys(action, inputState, inputObject)
	if inputState ~= Enum.UserInputState.Begin then return end
	
	if inputObject.KeyCode == Enum.KeyCode.R and userInputService:IsKeyDown(Enum.KeyCode.A) then
		print("holding r and a")
	elseif inputObject.KeyCode == Enum.KeyCode.A and userInputService:IsKeyDown(Enum.KeyCode.R) then
		print("holding r and a")
	end
end

contextActionService:BindAction("CheckKeys", checkKeys, false, Enum.KeyCode.R, Enum.KeyCode.A)
2 Likes