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.
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)
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’.
I would make a string and whenever a player presses a button it adds that button to the string, then use string.sub to see if “print” for example is inside of the string.
local UserInputService = game:GetService("UserInputService")
local Word = "Hello, world"
local InputCount = 0
local size = #Word
local debounce = false
UserInputService.InputBegan:Connect(function(InputObject, _)
if debounce or not (InputObject.UserInputType == Enum.UserInputType.Keyboard) then
return
end
debounce = true
local NextLetter = string.sub(Word,InputCount+1, InputCount+1)
local IsLetter,PressedLetter = pcall(function()
-- I used pcall because string.char errors if it's not a normal letter, like pressing shift
return string.char(InputObject.KeyCode.Value)
end)
if NextLetter and PressedLetter == NextLetter:lower() then
InputCount += 1
if InputCount == #Word then
print("Correct sequence")
-- If the sequence is correct, this runs
return
else
-- this runs every time a letter is correct
print("Correct letter")
end
elseif IsLetter then
InputCount = 0
print("Incorrect letter, correct letter was "..NextLetter.." but you pressed "..PressedLetter)
end
debounce = false
end)
At the top there’s a variable named “Word”, you can change that out for whatever you need
Animations = {}
function Create_Animations(ids)
for i = 1, #ids do
Animation = Instance.new("Animation")
Animation.AnimationId = ids[i]
Animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(Animation)
Animation.Looped = false
Animations[#Animations + 1] = Animation
end
end
Create_Animations({'http://www.roblox.com/asset/?id=507770677','http://www.roblox.com/asset/?id=507770818','http://www.roblox.com/asset/?id=507765000'})
game:GetService("UserInputService").InputBegan:Connect(function(keycode)
if keycode.KeyCode == Enum.KeyCode.Unknown then
return
elseif keycode.KeyCode ~= Enum.KeyCode.T and firstbuttonpressed then
Reset()
elseif keycode.KeyCode ~= Enum.KeyCode.R and secondbuttonpressed then
Reset()
elseif keycode.KeyCode == Enum.KeyCode.T and not firstbuttonpressed then
Animations[1]:Play()
firstbuttonpressed = true
print("First T pressed")
elseif keycode.KeyCode == Enum.KeyCode.T and firstbuttonpressed and not secondbuttonpressed then
Animations[2]:Play()
Animations[1]:Stop()
secondbuttonpressed = true
firstbuttonpressed = false
print("Second T pressed")
elseif keycode.KeyCode == Enum.KeyCode.R and secondbuttonpressed then
Animations[3]:Play()
Animations[2]:Stop()
print("Congrats, you've pressed the keys in the correct order. Here's a trophy!")
firstbuttonpressed = false
secondbuttonpressed = false
end
end)
function Reset()
firstbuttonpressed = false
secondbuttonpressed = false
print("Incorrect order")
end
Method 2:
local ContextActionService = game:GetService("ContextActionService")
local firstLetterPressed
local secondLetterPressed
local keyCodesToIgnore = {Enum.KeyCode.W,Enum.KeyCode.A,Enum.KeyCode.D,Enum.KeyCode.S,Enum.KeyCode.Up,Enum.KeyCode.Left,Enum.KeyCode.Right,Enum.KeyCode.Backspace,Enum.KeyCode.Space}
local function Reset()
print("Incorrect Order")
firstLetterPressed = false
secondLetterPressed = false
end
local function function1(actionName, inputState, inputObject)
if actionName == "T" and inputState == Enum.UserInputState.Begin then
if secondLetterPressed then
Reset()
elseif not firstLetterPressed then
firstLetterPressed = true
elseif firstLetterPressed then
secondLetterPressed = true
end
elseif actionName == "R" and inputState == Enum.UserInputState.Begin then
if secondLetterPressed then
print("YOU WON")
firstLetterPressed = false
secondLetterPressed = false
elseif firstLetterPressed then
Reset()
end
elseif firstLetterPressed and inputState == Enum.UserInputState.Begin then
Reset()
end
end
for i,v in pairs(Enum.KeyCode:GetEnumItems()) do
if not table.find(keyCodesToIgnore, v) then
ContextActionService:BindAction(string.split(tostring(v), "Enum.KeyCode.")[2], function1, true, v)
end
end
Here’s my quick thing for it, pretty interesting challenge, nice.
There’s an option for if you want it to be case sensitive.
If the player exceeds the length of the string (“Combination”), it will call OnFailed() and reset the sequence.
However if they succeed, it will call OnSuccess() and reset the sequence.
You can also forcefully reset the sequence by setting _currentInput to an empty string - this also means you can edit the player’s input, for whatever reason you may have.
local UserInputService = game:GetService("UserInputService")
local CASE_SENSITIVE = false
--// What the user will have to type.
local CombinationText = "TTR"
local function OnSuccess()
print("Player typed it successfully!")
end
local function OnFailed()
print("Player failed to type it.")
end
local function ConvertToENGChar(Code)
local ValidCharacter, Character = pcall(string.char, Code.Value)
if ValidCharacter then
return Character
end
end
local _currentInput = ""
UserInputService.InputBegan:Connect(function(Object, Processed)
if Processed then
return
end
if Object.UserInputType ~= Enum.UserInputType.Keyboard then
return
end
local actualInput = ConvertToENGChar(Object.KeyCode)
if not actualInput then
return
end
local inputtedKey = if CASE_SENSITIVE then actualInput else actualInput:lower()
_currentInput ..= inputtedKey
local combinationSuccess = _currentInput == (if CASE_SENSITIVE then CombinationText else CombinationText:lower())
if combinationSuccess then
OnSuccess()
_currentInput = ""
elseif #_currentInput >= #CombinationText then
OnFailed()
_currentInput = ""
end
end)
After lots of trial and error, I did it: LocalScript.rbxm (1.5 KB)
Code:
local contextActionService = game:GetService("ContextActionService")
local keyCodesToIgnore = {Enum.KeyCode.W,Enum.KeyCode.A,Enum.KeyCode.D,Enum.KeyCode.S,Enum.KeyCode.Up,Enum.KeyCode.Left,Enum.KeyCode.Right,Enum.KeyCode.Backspace,Enum.KeyCode.Space}
local pattern = "TTR"
local animations = {"http://www.roblox.com/asset/?id=507770818","http://www.roblox.com/asset/?id=507770453","http://www.roblox.com/asset/?id=507770677"}
local lastAnimationPlaying = false
local keysPressed = ""
local function animate(i)
if lastAnimationPlaying then
lastAnimationPlaying:Stop()
end
if i > 1 then
animations[i - 1]:Stop()
end
local animation = Instance.new("Animation")
animation.AnimationId = animations[i]
animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animation)
animation.Looped = false
animation:Play()
animations[i] = animation
end
local function reset()
animations = {"http://www.roblox.com/asset/?id=507770818","http://www.roblox.com/asset/?id=507770453","http://www.roblox.com/asset/?id=507770677"}
keysPressed = ""
print(keysPressed)
end
local function function1(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
local i = string.len(keysPressed) + 1
if actionName == string.sub(pattern, i, i) then
animate(i)
keysPressed = keysPressed .. actionName
if keysPressed == pattern then
print(keysPressed)
print("You got the correct order which is " .. keysPressed .. ". Congratulations!")
lastAnimationPlaying = animations[i]
task.spawn(function()
task.wait(lastAnimationPlaying.Length)
lastAnimationPlaying = false
end)
reset()
else
print(keysPressed)
end
else
reset()
end
end
end
for i,v in pairs(Enum.KeyCode:GetEnumItems()) do
if not table.find(keyCodesToIgnore, v) then
contextActionService:BindAction(string.split(tostring(v), "Enum.KeyCode.")[2], function1, true, v)
end
end
Good point. I’m not that familiar with memory usage for Roblox Studio haha. I guess something like this would be better:
local contextActionService = game:GetService("ContextActionService")
local keyCodesToIgnore = {Enum.KeyCode.W,Enum.KeyCode.A,Enum.KeyCode.D,Enum.KeyCode.S,Enum.KeyCode.Up,Enum.KeyCode.Left,Enum.KeyCode.Right,Enum.KeyCode.Down,Enum.KeyCode.Space,Enum.KeyCode.Unknown}
local pattern = "TTR"
local animations = {"http://www.roblox.com/asset/?id=507770818","http://www.roblox.com/asset/?id=507770453","http://www.roblox.com/asset/?id=507770677"}
local lastAnimationPlaying = false
local keysPressed = ""
local function animate(i)
if lastAnimationPlaying then
lastAnimationPlaying:Stop()
end
if i > 1 then
animations[i - 1]:Stop()
end
local animation = Instance.new("Animation")
animation.AnimationId = animations[i]
animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animation)
animation.Looped = false
animation:Play()
animations[i] = animation
end
local function reset()
animations = {"http://www.roblox.com/asset/?id=507770818","http://www.roblox.com/asset/?id=507770453","http://www.roblox.com/asset/?id=507770677"}
keysPressed = ""
print(keysPressed)
end
game:GetService("UserInputService").InputBegan:Connect(function(keycode)
if table.find(keyCodesToIgnore, keycode.KeyCode) then
return
end
local i = string.len(keysPressed) + 1
local actionName = string.split(tostring(keycode.KeyCode), "Enum.KeyCode.")[2]
if actionName == string.sub(pattern, i, i) then
animate(i)
keysPressed = keysPressed .. actionName
if keysPressed == pattern then
print(keysPressed)
print("You got the correct order which is " .. keysPressed .. ". Congratulations!")
lastAnimationPlaying = animations[i]
task.spawn(function()
task.wait(lastAnimationPlaying.Length)
lastAnimationPlaying = false
end)
reset()
else
print(keysPressed)
end
else
reset()
end
end)