How to print something if player presses keys in order on the table

local ButtonPresses = {
   [1] = {"T","T","R"}
}

So say i wanted to have a player press keys in that exact order to print something(Like press T then T again then R) like on the table, how would i do that?

1 Like

You can keep track of pressed keys within a time frame and using that find the input you need to print. Services you can use for that are Userinputservice and Contextactionservice.

Im not acting for you to make a script for me but can you give me an example?

Can’t really due to the circumstances I’m helping you in but just using keycodes you can make a string by adding values to it and compare it in an if statement, look up how Userinputservice works and you’ll get what I mean in no time

use the user input service and then use the table.find function and use lots of debounces

Well, i tried this and it somehow didnt work the way i intended

local uis = game:GetService("UserInputService")

local press1 = false
local press2 = false
local press3 = false

uis.InputBegan:Connect(function(obj) -- when theres an input
	if obj.KeyCode == Enum.KeyCode.R then
		
		press1 = true
	end
	if obj.KeyCode == Enum.KeyCode.T then
		if press1 then
			press2 = true
		end
	end
	if obj.KeyCode == Enum.KeyCode.T then
		if press2 then
			print("cool")
		end
	end
end)

For this script it just print when the 1st T is pressed

Make a table called T,T,R. Then do user input began do this,


game:GetService("UserInputService").InputBegan:Connect(function(keycode))

if keycode == Enum.KeyCode.T and not firstbuttonpressed then

firstbuttonpressed = true

elseif keycode == Enum.KeyCode.T and firstbuttonpressed and not secondbuttonpressed then

secondbuttonpressed = true

elseif keycode == Enum.KeyCode.R and firstbuttonpressed and secondbuttonpressed then

print("Congrats, you pressed the keys in the correct order. Here's a trophy!")

Reset()

end

end)

function Reset()

firstbuttonpressed = false
secondbuttonpressed = false

end)

You could make a string value, and each time the player pressed a key, it’d add that key to that string.

Example
Let’s say you have a string, an empty string.
And someone pressed ‘T’ on his keyboard, what you could do is add that key[T] to that string,
and so on for others, now -

eventually, you could check that string, and check if that pattern ‘T’,‘T’,‘R’ is there.

Im sorry, but this script is such a mess idk what to do

Okay, i see you fixed a little it now, thanks

Np, sorry for bad formatting (i’m on mobile).

2 Likes

It is a little wonky still though

how do i fix that?

Obj.keycode for those first underlines and for the second you need to rename the variables + enum.keycode.L otherwise i don’t think it’ll work

2 Likes
local Pattern = "TTR"
local UIS = game:GetService("UserInputService")
local PatternString = script.PatternString

UIS.InputBegan:Connect(function(key)
	if key.UserInputType == Enum.UserInputType.Keyboard then
		PatternString.Value = PatternString.Value..key.KeyCode.Name
		if key.KeyCode == Enum.KeyCode.R then
			PatternString.Value = ""
		end
		if PatternString.Value == Pattern then
			print("success!")
		end
	end
end)

This is an easy way to check if a player pressed any keys in a pattern. There are many other ways to do that.

ok, i also want it so that when you get a key right in the pattern, for each key you press, you do a certain animation, and i also get this error. PatternString is not a valid member of LocalScript “Players.Coolbro11741.Backpack.LocalScript”

If you’re using that script, make sure to put a string named PatternString under that script
[Make sure it’s under StarterPlayerScripts]
P.S,
not a problem, just add the animation part here:

local Pattern = "TTR"
local UIS = game:GetService("UserInputService")
local PatternString = script.PatternString

UIS.InputBegan:Connect(function(key)
	if key.UserInputType == Enum.UserInputType.Keyboard then
		PatternString.Value = PatternString.Value..key.KeyCode.Name
		if key.KeyCode == Enum.KeyCode.R then
			PatternString.Value = ""
		end
		if PatternString.Value == Pattern then
			--Play animation here
		end
	end
end)

I mean, basically, i want T and R to have different animations, so when you press T twice then you do the T animation when you press it each time, same thing for the R key

That’s alright, you can edit that script, and check what the PatternString’s Value is, for e.g:

We made on that script that whenever you press a key, it’d add that key to that string[except G, which will reset it],

so now we can detect when the player pressed ‘T’ twice, [by checking if there are 2 following ‘T’, and if it’s there [in the patternString’s value], then play that anim, same for the ‘R’.