UserInputService detects 2 inputs at the same time and ignores debounce

I would like this script to not have multiple functions run if both keys are pressed at the same time. How can this be accomplished?
(ex. pressing E and R at the same time would perform both attacks simultaneously.)

Code snippet:

UIS.InputBegan:Connect(function(input, processed)
	if processed then return end
	if IsActionable() then
		-- ground
		if not IsAerial() and not running then
			if input.KeyCode == Enum.KeyCode.Q then Attack("QGround") return end
			if input.KeyCode == Enum.KeyCode.E then Attack("EGround") return end
			if input.KeyCode == Enum.KeyCode.R then Attack("RGround") return end
		end
    end
end

Pressing one of the keys would disable IsActionable(), but since they are both pressed at the same time, it is not changed until after.

The usual solution would be adding your own input lock, setting it immediately when you decide to accept an input, before calling Attack(). Something like:

local UIS = game:GetService("UserInputService")

local inputLocked = false

UIS.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end

	if inputLocked then
		return
	end

	if not IsActionable() then
		return
	end

	if IsAerial() or running then
		return
	end

	local attack

	if input.KeyCode == Enum.KeyCode.Q then
		attack = "QGround"
	elseif input.KeyCode == Enum.KeyCode.E then
		attack = "EGround"
	elseif input.KeyCode == Enum.KeyCode.R then
		attack = "RGround"
	end

	if attack then
		inputLocked = true -- lock
		Attack(attack)
		inputLocked = false -- unlock when appropriate
	end
end)

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