How to detect if player has pressed two keybinds at once?

Basically, I want to make a pretty advanced combat system right now,

I thought about making it so when the players punches and presses space it would do a different attack aka fire a different remote.

How would I go about that?

Script:

local function Punch(animationnumber)
	if animationnumber == 1 or animationnumber == 3 or animationnumber == 5 or animationnumber == 7 or animationnumber == 9 then
	PunchAnim.AnimationId = 'rbxassetid://5207617104'
	local PunchLoad = Char.Humanoid:LoadAnimation(PunchAnim)
	PunchLoad:Play()
		
	elseif animationnumber == 2 or animationnumber == 4 or animationnumber == 6 or animationnumber == 8 then
	PunchAnim.AnimationId = 'rbxassetid://5212788793'	
	local PunchLoad = Char.Humanoid:LoadAnimation(PunchAnim)
	PunchLoad:Play()

	elseif animationnumber == 10 then
	PunchAnim.AnimationId = 'rbxassetid://5212789739'
	local PunchLoad = Char.Humanoid:LoadAnimation(PunchAnim)
	PunchLoad:Play()

	end
end

Mouse.Button1Down:Connect(function()
    if ready and not Char:FindFirstChildWhichIsA("Tool") and InAMove == false then
        InAMove = true
        -- // Counting
        PunchCount = PunchCount + 1
        -- // Animations & Cooldown
        Remote4:FireServer(PunchCount,Mouse.Hit)
        spawn(function()
        if PunchCount >= 10 then
            PunchCount = 0
        end
        end)
        
        Punch(PunchCount)        
        if PunchCount < 10 then
        ready = false
        wait(0.15) -- cooldown
        ready = true
        InAMove = false    
        else
        ready = false
        wait(1)
        ready = true
        InAMove = false
        end        
        end
end) 

So u mean when a player presses the space bar and for example the key T then it would fire a remote for each key? again it would fire a remote for the space bar and a different remote for the key T?

Have a table of keys pressed, and insert a value into it when a player presses a keybind, then remove it right after, like in .1 or .03 seconds. Every time a value is inserted into the table, check to see if another value is already in there. If there is, then that means they’ve pressed the two keybinds very close together or at the same time

You could have a tick() variable set to determine how long it has been since the last key press. For example:

local PreviousKeyTick = tick()
local PreviousKeyPressed = nil;
local DoubleKeyThreshold= 0.2;

if (KeyPress == KeyPress.Space) then
    if (PreviousKeyTick - tick() < DoubleKeyThreshold and PreviousKeyPressed ~= KeyPress) then -- If the time between the previous key and this key is lower than a set threshold, and is not the same key
        -- This is a double key press, do something
   end
end

PreviousKeyTick = tick() -- Set the new previous tick time
PreviousKeyPressed = KeyPress -- Set the new previous key pressed

This is just pseudo code, but the logic should be straight forward. You’d have to allow a small gap (threshold) to determine if a player is pressing two keys. Or, you could fire two different remote events, and have the server check the time gap between both fired events. If they are within a certain threshold, you can fire one single attack.

1 Like