How would I detect if there are two inputs at once?

So basically I’m making a game, and I need to be able to tell if two inputs are pressed.

Like if I pressed X and W then I do an upwards attack

How would I do this?

and would this work?

elseif keyCode == Enum.KeyCode.X and keyCode == Enum.KeyCode.W then
1 Like

You can do this:

local key1Pressed = false
local key2Pressed = false
local UIS = game:GetService("UserInputService")
UIS.InputEnded:Connect(function(input, gpe)
    if gpe then return end
    if input.KeyCode == Enum.KeyCode.X then
        key1Pressed = true
    elseif input.KeyCode == Enum.KeyCode.W then
        if key1Pressed then
           key2Pressed = true
        end
    end
    if key1Pressed and key2Pressed then
        -- do stuff here
    end
end)
1 Like
local UIS = game:GetService("UserInputService")

local KeysPressed = {}

UIS.InputBegan:Connect(function(Input, Processed)
	if (Input.UserInputType == Enum.UserInputType.Keyboard) and (not Processed) then 
		KeysPressed[Input.KeyCode] = true
		
		if (KeysPressed[Enum.KeyCode.X] == true) and (KeysPressed[Enum.KeyCode.W] == true) then
			print("yay")
		end
	end
end)

UIS.InputEnded:Connect(function(Input)
	if (Input.UserInputType == Enum.UserInputType.Keyboard) then 
		KeysPressed[Input.KeyCode] = false
	end
end)
2 Likes

I tried this. It only registers one. Not both at a time

2 Likes

Hi so I recommend going to a youtube channel called B Ricey he has a good tutorial over multiple inputs using UserInputService.

Um A link to it would be very helpful. There’s a lot of times I cant find tutorials. Lol
Thanks BTW

1 Like

This article could help ig

Tried this nothing printed into the output

			elseif keyCode == Enum.KeyCode.X  then -- I set the value here
				print(keyCode)
				pressingX = true

				
			
				
			elseif keyCode == Enum.KeyCode.W and pressingX then -- this is what I need
				print(keyCode)
				print('UP ATTACK YAS')
			
			end

You can use UserInputService:GetKeysPressed() to get all the current keys pressed and then check if the desired keys are pressed.

1 Like

Oh ok let me find the video
B Ricey multiple input module

1 Like

So I used this and yes it works but is there anyway to do like:

keysPressed:FindKey(Enum.Keycode.W) then

basically FindKey would be to find if there is a certain key that was pressed

No, but you can make your own FindKey function if you would like.

How would I do that?
Cause I have no idea.

You can try using UserInputService:IsKeyDown(keyCode).
It returns a boolean if the key is pressed.

if uis:IsKeyDown(Enum.KeyCode.W) and uis:IsKeyDown(Enum.KeyCode.X) then
-- Action to run
end
2 Likes

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