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

How do i make this script print after multiple keys are pressed?

I'm trying to make it so that this script will print after a certain sequence of keys have been pressed. If the player doesn't press the combinations fast enough then it'll reset.

Local Script

--//Services
local UIS = game:GetService('UserInputService')

--//Variables
local player = game.Players.LocalPlayer
local char = player.Character
local timer = tick()

--//Checks
local check = false
local check2 = false

--//Inputs
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.B then
		check = true
		if tick() - timer >= 0.5 then
			check = false
		end
		timer = tick()
		
		if check == true then
			UIS.InputBegan:Connect(function(input2)
				if input2.KeyCode == Enum.KeyCode.A then
					check2 = true
					if tick() - timer >= 0.5 then
						check2 = false
					end
					timer = tick()
					
					if check2 == true then
						UIS.InputBegan:Connect(function(input3)
							if input3.KeyCode == Enum.KeyCode.X then
								print('BAX')
								check = false
								check2 = false
							end
						end)
					end
				end
			end)
		end
	end
end)

2 answers

✓ Accepted answer

######I expected this to be another... "You're doing this wrong so do this like this because of this", but I also wanted to attempt another method in input key-sequences.

One of the problems to your code is how you're using a second InputBegan event inside of an existing one. This causes it to create more connections each time the if-checks get to that part (Line 32).

#####If we were to simplify your code down to this to analyze it better:

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		UIS.InputBegan:Connect(function(input2)
			print(input2.KeyCode)
		end)
	end
end)

The above creates InputBegan events everytime you press the E key. If you click on another key at the same time it would print that message once. Do this same thing a few more times and it'll have multiple events triggering this print statement.

I want you to learn about the :IsKeyDown() method which lets us know if a previous key is being held down. Just incase the mechanic you're trying to achieve can use this. This is a function of UserInputService and requires the KeyCode for it to look for.

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		if UIS:IsKeyDown(Enum.KeyCode.LeftShift) then
			print("SHIFT + E fired")
		else
			print("Only E was pressed")
		end
	end
end)

Just by reading the above code, if only the E key is pressed and the LeftShift key is held down beforehand then it will print the first message.


On the topic of key sequences. You can have some if-checks to see which was the last time and key in which input was given.

#####Simply done as:

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		if tick() - sinceLastInput <= 0.5 then
			if lastInputKey == Enum.KeyCode.R then
				print("Combo R into E")
			end
		end
		sinceLastInput = tick()
		lastInputKey = input.KeyCode
	end
	
	if input.KeyCode == Enum.KeyCode.R then
		if tick() - sinceLastInput <= 0.5 then
			if lastInputKey == Enum.KeyCode.E then
				print("Combo E into R")
			end
		end
		sinceLastInput = tick()
		lastInputKey = input.KeyCode
	end
end)

The prints fire if the time interval between these two keys were under half a second. You'll need to change the Enums to your desired keys obviously.

I wanted to get IsKeyDown() somehow used in this but I couldn't really see the use case. I just wanted to share these two methods just incase you're desiring a certain mechanic.


###Full code:

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

local player = Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local sinceLastInput = tick()
local lastInputKey = nil

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		if tick() - sinceLastInput <= 0.5 then
			if lastInputKey == Enum.KeyCode.R then
				print("Combo R into E")
			end
		end
		sinceLastInput = tick()
		lastInputKey = input.KeyCode
	end
	
	if input.KeyCode == Enum.KeyCode.R then
		if tick() - sinceLastInput <= 0.5 then
			if lastInputKey == Enum.KeyCode.E then
				print("Combo E into R")
			end
		end
		sinceLastInput = tick()
		lastInputKey = input.KeyCode
	end
end)

######Let me know if you need anything explained or if I had missed something.

1 comment

0

Thanks!


----- Only edit these --------------------------------------
local mintime = 1 -- Time required to do the combo
local keystomatch = {'b', 'a', 'x'}
-- Table incase you want to use multiple letter keys
	-- like shift 
------------------------------------------------------------

local UIS = game:GetService('UserInputService')
local player = game.Players.LocalPlayer
local char = player.Character
local starttime = false
local combo = ''
local keyspressed = 0
local matchthis = table.concat(keystomatch, '')
-- string together to match against

local function getkey(key)
	-- key = Enum.KeyCode.keypressedhere
	-- We only need keypressedhere 
	local keystring = tostring(key):lower()
	local lastWord =  string.find( string.reverse(keystring), '%.' )  - 1
	-- reverse string and get first word before the .
	return keystring:sub(-lastWord) 
end
	
UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then 
		if not starttime then starttime = tick() end
		-- We need a time to subtract from
		local key = getkey(input.KeyCode)
		combo = combo..key
		keyspressed = keyspressed + 1
		-- string together
		if keyspressed == #keystomatch then 
			-- if we hit the number of keys to match
			local total = tick() - starttime <= mintime
			-- subtract now from the first keypress time
				-- is it less than mintime?
			if total then 
				if combo == matchthis then
					-- if it matches the combo
					print (matchthis)
				else
					print('Not the combo: ', combo)
				end
			else
				print( 'Press them keys faster m8')
			end
			-- reset
			combo = ''
			starttime = false
			keyspressed = 0
		end
	end
end)

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