2 buttons pressed to activate a script

I’m not quite sure how to but I want to make it so that if for example you press keycode E and then keycode Q is pressed roughly 0.5-2 seconds after then the script activates. I know how to make it so when both are pressed but not a gap between the press of both. If anyone knows how to do this, please let me know.

2 Likes

Credit to @NyrionDev for writing the original code in this post.

local UserInputService = game:GetService("UserInputService")

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		local function waitEvent(event, timeout)
			print("player pressed E, waiting for Q")
			local start = os.clock() 
			local data = {}
			local done = false 

			local connection 
			connection = event:Connect(function(input) -- temporary event that will last until the player presses Q or for 2 seconds.
				if input.KeyCode == Enum.KeyCode.Q then
					print("player pressed Q")
					-- your code here...
					data = {}
					done = true 
					connection:Disconnect() -- if player presses Q, there is no more purpose for the event
				end
			end)
			repeat task.wait() until done or (os.clock()-start > timeout) -- waits for Q to be pressed, or until the 2 seconds pass.
			connection:Disconnect() -- player didn't press Q in time, so the event is disconnected.
			return table.unpack(data)
		end

		local timeout = 2 -- time given to press Q
		local response = waitEvent(UserInputService.InputBegan, timeout)
		print(response)
	end
end)

Let me know if it doesn’t work, or if you don’t understand something! :smiley:

2 Likes

Okey try this:

local timer = 0
local UserInputService = game:GetService("UserInputService")
local checker = false
local TimerMinValue = 0.5 --Put here MinValue of your cooldown--
local TimerMaxValue = 2 --Put here MaxValue of your cooldown--
UserInputService.InputBegan:Connect(function(input1)
	if input1.KeyCode == Enum.KeyCode.E then
		while true do
			if checker == true then
				checker = false
				break
			end
			if timer == TimerMaxValue then
				timer = 0
				checker = false
				break
			end
			wait()
			timer = timer + wait()
			timer = math.clamp(timer,0,TimerMaxValue)
			UserInputService.InputBegan:Connect(function(input2)
				if input2.KeyCode == Enum.KeyCode.Q and timer >= TimerMinValue and timer <= TimerMaxValue then
					timer = 0
					checker = true
					--Put here code you want--
				elseif timer == TimerMaxValue then
					timer = 0
					checker = true
				end
			end)
		end
	end
end)

Also i made this code for more than 1 hour :skull: :skull: :skull:

1 Like

This kind of works, however, it’s a bit inefficient and flawed. I tested your code and I was able to press Q twice, which shouldn’t happen. And after pressing E again, I couldn’t press Q because I had pressed it twice before.

What I am saying is it isn’t spam-proof.
image

2 Likes

I think i fixed it:

local timer = 0
local UserInputService = game:GetService("UserInputService")
local checker = false
local TimerMinValue = 0.5 --Put here MinValue of your cooldown--
local TimerMaxValue = 2 --Put here MaxValue of your cooldown--
local KeyAbleToPress1 = true
local KeyAbleToPress2 = true
UserInputService.InputBegan:Connect(function(input1)
	if input1.KeyCode == Enum.KeyCode.E and KeyAbleToPress1 == true then
		KeyAbleToPress1 = false
		while true do
			if checker == true then
                timer = 0
				checker = false
				KeyAbleToPress1 = true
				KeyAbleToPress2 = true
				break
			end
			if timer == TimerMaxValue then
				timer = 0
				checker = false
				KeyAbleToPress1 = true
				KeyAbleToPress2 = true
				break
			end
			wait()
			timer = timer + wait()
			timer = math.clamp(timer,0,TimerMaxValue)
			
			UserInputService.InputBegan:Connect(function(input2)
				if input2.KeyCode == Enum.KeyCode.Q and timer >= TimerMinValue and timer <= TimerMaxValue and KeyAbleToPress2 == true then
					KeyAbleToPress2 = false
					timer = 0
					checker = true
				elseif timer == TimerMaxValue then
					timer = 0
					checker = true
				end
			end)
		end
	end
end)

1 Like

I tested it and it still remains flawed. The E key can’t be pressed for another 4 seconds after being pressed first. (in this example I did not press Q at all)
image

And I’m still able to press Q and trigger the Q event twice.
image

What I am trying to say is that this code is inefficient and has too many things that can go wrong.

Please check out my first post where I wrote a detailed explanation of the code.

I don’t see a point in continuing to try to fix the code you wrote using the same method.

1 Like

Hey guys, thanks for the help! I’m not sure who to give the solution. I mean, Marko had the least flaws but I appreciate your effort!

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