Keybind Sequences Script

I created a keybind sequence script, feel free to use it. (Place in game.StarterPack)

Settings

Local Variable sequence: table - The sequence of Enum.KeyCode.Name’s (lowercase)
Local Variable callback: function - The function to execute after the sequence is complete
Attribute onlyOnce: boolean - If the sequence can only be called once

You can also extend the script by changing the sequence at runtime and calling the newSequence function to rebuild the sequence.

Source (GitHub isn't necessary for this)
--// CONFIG	\\--

local sequence = {'up', 'down', 'left', 'right'}
local callback = function()
	-- do something
    print("Finished!")
end

--// CODE \\--

local newTbl = {}

local function newSequence()
	newTbl = {}
	for k, v in ipairs(sequence) do
		newTbl[k] = {
			['Key'] = v,
			['IsDone'] = false
		}
	end
end

local UIS = game:GetService("UserInputService")

newSequence()

UIS.InputBegan:Connect(function(input)
	if #newTbl == 0 then return end
	
	local currentKey, currentIndex
	for k, v in pairs(newTbl) do
		if v['IsDone'] == false then
			currentKey = v['Key']
			currentIndex = k
			break
		end
	end
	
	if string.lower(input.KeyCode.Name) == currentKey then
		newTbl[currentIndex]['IsDone'] = true
	else
		for k, _ in pairs(newTbl) do
			newTbl[k]['IsDone'] = false
		end
	end
	
	for _, v in pairs(newTbl) do
		if v['IsDone'] == false then return end
	end
	
	if script:GetAttribute("workOnce") then
		newTbl = {}
	end
	
	callback()
end)

Have fun!

5 Likes

Would be nicer to have this as a module instead of a plain script.

This is a client-sided local script that utilizes UserInputService. Making it a ModuleScript wouldn’t make it any better. A function already exists within the script that lets you rebuild the sequence.

What if you need multiple keybind sequences that do different things? What if you need to change it from another script? Wouldn’t be too nice to have the same script cloned over and over

Creating a ModuleScript for this is completely useless. It would still duplicate the script into your Backpack for each sequence, and since Attributes cannot have a table value, changing the sequence from another script isn’t possible (unless you like using _G).

wow this is kinda cool and might be able to make cheat codes with this

1 Like