Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I use two keys to activate?

Hey, just wondering how to use two keys to activate this instead of one? I want it to activate by pressing "Left Alt" key and the letter "Q" at the same time. However, the ways I have tried will not work.. This script on it's own works but I can't seem to add the Q as well. Please help, Thank you.

local U = game:GetService("UserInputService")
Player = game.Players.LocalPlayer
local Gui = false
U.InputBegan:connect(function(input, gEP)
if input.KeyCode == Enum.KeyCode.LeftAlt then
	
	if Gui == false then
		Gui = true
		script.Parent.Enabled = true
elseif Gui == true then
	Gui = false
	script.Parent.Enabled = false
end
end
end)

2 comments

0

I'm thinking you put if input.KeyCode == Enum.KeyCode.LeftAlt and input.KeyCode == Enum.KeyCode.Q then

0

Tried that already and nothing happened.

1 answer

✓ Accepted answer

This is a sort of difficult thing to do. I actually wrote a module that, among other things, handles this. Check it out here

Here's a demo:

local UserInputService = require(MODULESCRIPT) -- "Overwrites" default UIS
local Keys = UserInputService.Keys

local LeftAlt = Keys.LeftAlt
local Q = Keys.Q

local Parent = script.Parent

local Connection = (LeftAlt + Q):Connect(function()
	Parent.Enabled = not Parent.Enabled
end)

If you really want to do it without using a ModuleScript, there is a way. I recommend using the way above, because it is much cleaner, and I wrote it to be as efficient as the following way (as far as calling the function goes). If you write a game with a lot of different Keys that trigger things, it's easier to use the above syntax. If you want to do it the long way, here is how you might go about it:

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local Q = Enum.KeyCode.Q
local LeftAlt = Enum.KeyCode.LeftAlt

local Player = Players.LocalPlayer
local Parent = script.Parent
local GuiEnabled

UserInputService.InputBegan:Connect(function(Input, GuiProcessed)
	if not GuiProcessed and Input.KeyCode == Q then
		local KeysBeingHeldDown = UserInputService:GetKeysPressed()

		for i = 1, #KeysBeingHeldDown do
			if KeysBeingHeldDown[i].KeyCode == LeftAlt then
				if GuiEnabled then
					GuiEnabled, Parent.Enabled = false
				else
					GuiEnabled, Parent.Enabled = true, true
				end
				
				break
			end
		end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.