How to do combo-keys, e.g. R3 + X on roblox?
So someone press R3 + X on their XBOX to perform an action in a game.
How to do combo-keys, e.g. R3 + X on roblox?
So someone press R3 + X on their XBOX to perform an action in a game.
there might be a more efficient way but this is what i came up with
local uis = game:GetService("UserInputService")
local xHeld = false
local r3Held = false
uis.InputBegan:Connect(function(i, gpe)
if gpe then return end
if i.UserInputType == Enum.UserInputType.Gamepad1 then
if i.KeyCode == Enum.KeyCode.ButtonX then
xHeld = true
end
end
end)
uis.InputEnded:Connect(function(i, gpe)
if gpe then return end
if i.UserInputType == Enum.UserInputType.Gamepad1 then
if i.KeyCode == Enum.KeyCode.ButtonX then
xHeld = false
end
end
end)
uis.InputBegan:Connect(function(i, gpe)
if gpe then return end
if i.UserInputType == Enum.UserInputType.Gamepad1 then
if i.KeyCode == Enum.KeyCode.Thumbstick2 then
r3Held = true
end
end
end)
uis.InputEnded:Connect(function(i, gpe)
if gpe then return end
if i.UserInputType == Enum.UserInputType.Gamepad1 then
if i.KeyCode == Enum.KeyCode.Thumbstick2 then
r3Held = false
end
end
end)
local attacking = false
local function attack()
attacking = true
-- do attack
delay(3, function()
-- 3 second delay till can attack again
attacking = false
end)
end
game:GetService("RunService").RenderStepped:Connect(function()
if attacking then return end
if r3Held and xHeld then
attack()
end
end)
A better alternative to tracking the state of every button on every frame is to just check the state of a specific set of buttons when one of them is pressed. For example, here’s a sample that lets you easily add more combinations as you need:
--!strict
local UserInputService = game:GetService("UserInputService")
type Callback = () -> nil
local function onCombo1Pressed()
print("Combo 1 pressed")
end
local inputCombinations: { [Callback]: { Enum.KeyCode }} = {
[onCombo1Pressed] = {
Enum.KeyCode.ButtonX,
Enum.KeyCode.ButtonR3,
},
}
local function checkAreAllKeysPressed(keys: { Enum.KeyCode }): boolean
for _, key in keys do
if not UserInputService:IsKeyDown(key) then
return false
end
end
return true
end
local function getPressedCombinationsContainingKey(keyCode: Enum.KeyCode): { Callback }
local pressedCombinationsContainingKey = {}
for callback, keyCodes in inputCombinations do
if not table.find(keyCodes, keyCode) then
continue
end
if checkAreAllKeysPressed(keyCodes) then
table.insert(pressedCombinationsContainingKey, callback)
end
end
return pressedCombinationsContainingKey
end
UserInputService.InputBegan:Connect(function(inputObject: InputObject, isProcessed: boolean)
if isProcessed then
return
end
if inputObject.UserInputType == Enum.UserInputType.Gamepad1 then
local pressedCombinationsContainingKey = getPressedCombinationsContainingKey(inputObject.KeyCode)
for _, callback in pressedCombinationsContainingKey do
callback()
end
end
end)