How to do combinations of keys using User Input Sevice

Here is Information how to do combination of keybinds.

First let’s create a local script at StarterCharacterScripts and name it however you want i’ve named it KeyCombination
image

after that we have to import User Input Service Using
local UserInputService = game:GetService('UserInputService')
then we need to add Input Began command using

UserInputService.InputBegan:Connect(function()
	
end)

UserInputService.InputEnded:Connect(function()

end)

UserInputService.InputChanged:Connect(function()
	
end)

after done we write our code:

local UserInputService = game:GetService('UserInputService')
local r_pressed = false
local T_pressed = false

UserInputService.InputBegan:Connect(function(key) -- call function when input began
	if key.KeyCode == Enum.KeyCode.R then
		r_pressed = true
	elseif key.KeyCode == Enum.KeyCode.T then
		T_pressed = true
		if r_pressed  then
			print('combo')
		end
	end
end)

UserInputService.InputEnded:Connect(function(key) -- when ended
	if key.KeyCode == Enum.KeyCode.R then
		r_pressed = false
	elseif key.KeyCode == Enum.KeyCode.T then
		T_pressed = false
	end
end)

UserInputService.InputChanged:Connect(function(key) -- call function when input changed
	if key.KeyCode == Enum.KeyCode.R then
		r_pressed = true
	elseif key.KeyCode == Enum.KeyCode.T then
		T_pressed = true
		if r_pressed  then
			print('combo')
		end
	end
end)


as you can see it works (dont look at other errors its not from this script)

You can also change the keybinds or add third one just adding another elseif, adding variables and moving if (keybingname_pressed) then one line down

give the feedback replying to this post

1 Like

You have to use UserInputService:IsKeyDown() for that. Ex:

local UserInputService = game:GetService("UserInputService")

local shiftKeyL = Enum.KeyCode.LeftShift

local shiftKeyR = Enum.KeyCode.RightShift

-- Return whether left or right shift keys are down

local function isShiftKeyDown()

return UserInputService:IsKeyDown(shiftKeyL) or UserInputService:IsKeyDown(shiftKeyR)

end

-- Handle user input began differently depending on whether a shift key is pressed

local function input(_input, _gameProcessedEvent)

if not isShiftKeyDown() then

-- Normal input

else

-- Shift input

end

end

UserInputService.InputBegan:Connect(input)

Source

6 Likes

well i tried using method with is keydown and eventually it didnt work
so i decided to use this method

yeah no UserInputService:IsKeyDown() is gonna be what you want to use instead.