Konami Code Detector

The Konami Code Detector

(well, it’s not just the Konami code, but)


I wanted to add an easter egg to my game, and I thought, “What could be better than the Konami Code?” But somehow, I couldn’t find a single open-sourced script for it in sight! That’s why I’d like to release this simple detector to you guys.

(the default keyboard binding has changed a little)
It supports Touch, Keyboard, and Gamepads! You can also mix and match the input types if you really wanted to. The combination you have to enter is fully customizable, too! You’re not just limited to the Konami code (although I’d recommend you stick to a sequence that’s the same size across all input types, lest you want to break the script)

Example Place and Code


Wanna try it out for yourself? In this example place, the cube will turn green when you enter it successfully.

konami v2.rbxl (22.7 KB)

Model:

Source
--[[
	KonamiCode
	Written by NobleReign
	Date: 09/07/2020 12:16:47 PM Pacific Time
	Description: Detects the Konami Code. Compatible with keyboard, touch, and gamepad.
]]

local uis = game:GetService("UserInputService")
local gui = game:GetService("GuiService")

local keyIndex = 1
local keys = {
	[Enum.UserInputType.Keyboard] = {
		Enum.KeyCode.Up,
		Enum.KeyCode.Up,
		Enum.KeyCode.Down,
		Enum.KeyCode.Down,
		Enum.KeyCode.Left,
		Enum.KeyCode.Right,
		Enum.KeyCode.Left,
		Enum.KeyCode.Right,
		Enum.KeyCode.B,
		Enum.KeyCode.A,
		Enum.KeyCode.Return,
	},
	[Enum.UserInputType.Gamepad1] = {
		Enum.KeyCode.DPadUp,
		Enum.KeyCode.DPadUp,
		Enum.KeyCode.DPadDown,
		Enum.KeyCode.DPadDown,
		Enum.KeyCode.DPadLeft,
		Enum.KeyCode.DPadRight,
		Enum.KeyCode.DPadLeft,
		Enum.KeyCode.DPadRight,
		Enum.KeyCode.ButtonB,
		Enum.KeyCode.ButtonA,
		Enum.KeyCode.ButtonStart,
	},
	[Enum.UserInputType.Touch] = {
		Enum.SwipeDirection.Up,
		Enum.SwipeDirection.Up,
		Enum.SwipeDirection.Down,
		Enum.SwipeDirection.Down,
		Enum.SwipeDirection.Left,
		Enum.SwipeDirection.Right,
		Enum.SwipeDirection.Left,
		Enum.SwipeDirection.Right,
		"Tap",
		"Tap",
		"LongTap"
	}
}

local function detectKonami(input)
	local keyPressed = input.KeyCode
	if keys[input.UserInputType][keyIndex] == keyPressed then
		keyIndex += 1
		if not keys[input.UserInputType][keyIndex] then
			--Konami win!
			keyIndex = 1
			script.Entered:Fire()
		end
	else
		keyIndex = 1
		--Konami fail.
	end 
end

gui.MenuOpened:Connect(function()
	detectKonami({UserInputType = Enum.UserInputType.Gamepad1, KeyCode = Enum.KeyCode.ButtonStart})
end)

uis.TouchSwipe:Connect(function(swipeDirection, numberOfTouches, gameProcessedEvent)
	detectKonami({UserInputType = Enum.UserInputType.Touch, KeyCode = swipeDirection})
end)

uis.TouchTap:Connect(function(touchPositions, gameProcessedEvent)
	detectKonami({UserInputType = Enum.UserInputType.Touch, KeyCode = "Tap"})
end)

uis.TouchLongPress:Connect(function(touchPositions, state, gameProcessedEvent)
	if state == Enum.UserInputState.Begin then
		detectKonami({UserInputType = Enum.UserInputType.Touch, KeyCode = "LongTap"})
	end
end)

uis.InputEnded:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		detectKonami(input)
	elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
		detectKonami({UserInputType = Enum.UserInputType.Keyboard, KeyCode = "LeftClick"})
	elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
		detectKonami({UserInputType = Enum.UserInputType.Keyboard, KeyCode = "RightClick"})
	elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
		detectKonami(input)
	end
end)

Do I need to credit you?


It’d be appreciated, but I don’t really care that much. Go wild!

Bugs


I’m not sure if I’ll actively maintain this (I can barely even ‘maintain’ my own games as is), but if you find any, it wouldn’t hurt to let me know! (also if you used this in your game I would love to know that too)

Thanks for stopping by, and happy button pressing!

38 Likes

This is great! Right when I kinda needed it also… Very useful for an easter egg situation, “cheat” codes, and code entries!
but it does have the potential…

10 Likes

What are the controls for keyboard? I don’t know if it’s arrow keys or anything else because nothing is being outputted to the console. Edit: I just found out that nothing gets outputted to the console when you do the code, but I still can’t figure out how to do it with keyboard.

@SxarredBehind I don’t get it.

The default code for the keyboard uses the arrow keys. Up, Up, Down, Down, Left, Right, Left, Right, B, A, Space Enter.

2 Likes

Now it’s working. I just forgot the space part. this is definitely going in my game.

1 Like

I did this in my game but bound start to enter instead. It doesn’t do anything if you press it, at least nothing meaningful. All it does is play heavy_laughshort02.wav

It’s a nice touch if you wanna go the easter egg route in your game :stuck_out_tongue:

2 Likes

Out of context that sounds like a demon scoffing at you

@NobleReign I recommend you rebind the defaults on PC,

  1. Nobody uses arrow keys anymore
  2. pressing ‘A’ makes your character move left which is misleading
  3. space doesn’t make sense when you could use enter
  4. the B,A part could just be right click left click
1 Like

I did a quick little update, fixing a bug where the index wouldn’t reset after a success, and changing out Space for Enter. Also, you can now detect left and right clicks by using "LeftClick" and "RightClick" in your combinations respectively.

@Johnnyfireball123, I decided to keep most of the default binding because I just think that it’s simpler to keep the default buttons similar to their controller counterparts (DPadUp being Up, B and A being, well, B and A, etc). It’s not too hard to change the keybindings, so I think it’s likely that if having small, accidental movements is too big of a problem in someone’s game, they’d change it accordingly. Nevertheless, this new update added the support for detecting left and right clicks, so if you want to swap B and A out for them, go ahead!

1 Like

Thank you for doing the kenami code, very cool.

1 Like

will be using this in the menu screen of my civ game to change the music to castlevania