Trying to create a shortcut for UIS(keycode) in my framework

Back Story
So for the last couple days I’ve been developing a framework that basically makes my life easier and shorter in studio and I thought WHY NOT A SHORTCUT FOR USERINPUTSERVICE? So I’m setting up the method in the module and I broke my brain…

Where self.Brain Broke
function Mod:FireKeyCode(...)
	local Key = ...
	UIS.InputBegan:Connect(function(inp, gpe)
		if inp.KeyCode == Key and not gpe then
			print("WAIT A MINUTE?!?!?!")
		end
	end)
end

How am I going to make it to where they can insert their code ?!

and thats where the masterminds of the lua come in handy(This is where you comment and criticize my life choices up to this point)

function Mod:FireKeyCode(key, callback)
	UIS.InputBegan:Connect(function(inp, gpe)
		if inp.KeyCode == key and not gpe then
			return callback()
		end
	end)
end

and you would call It like

Module:FireKeyCode(Enum.KeyCode.E, function()
  print("They pressed E")
end)
1 Like

Ok thank you, I’ll give it a try.

Still didn’t print so far.

Module
function Mod:FireKeyCode(callback, ...)
	local Key = ...
	UIS.InputBegan:Connect(function(inp, gpe)
		if inp.KeyCode == Key and not gpe then
			return callback()
		end
	end)
end```

Client
local GET = require(script:WaitForChild("UIS"))

local Players = game:GetService("Players")

local Player = Players.LocalPlayer

local INPUT = GET.CreateIndex()

INPUT:FireKeyCode(Enum.KeyCode.Q, function()

print("Pressed Q")

end)```

you have edited what I sent you. the way I did it was the first argument was the keycode and the second was a function.

use the first module I sent you and this as the client code

local GET = require(script:WaitForChild("UIS"))
local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local INPUT = GET.CreateIndex()

INPUT:FireKeyCode(Enum.KeyCode.Q, function()
    print("Pressed Q")
end)

You shouldn’t connect a new event everytime the function is called as it’ll cause a memory leak, you should instead put the callbacks into a table, and call them when the said keycode is fired outside of the function.

Ex. Register/Unregister functions to register callbacks for certain keycodes.

1 Like

Can you by chance make an example in lua?

keycodetable = {} -- the table containing the functions of the keycodes

keycodetable[Enum.KeyCode.E] = function() -- we link keycode E to a function
print(e)
end

local function onInputBegan(input, gameProcessed) -- 
 if keycodetable [input.UserInputType] then -- checks the table if for that keycode is a function
  keycodetable [input.UserInputType]() -- if there is a function execute that function
 end
end
UserInputService.InputBegan:Connect(onInputBegan) -- activates on userinputservice

i think he/she means something like this. but it still needs checks for when people are typing.
or holding down 2 buttons at once