I'm working on a template userinput script and I can't figure out how to make it use a string/variable for the KeyCode. I want to make my game customizable w/ the keybinds and have no idea how to get this to work. Is it possible to even use variables for UserInput?
local userInputService = game:GetService("UserInputService") local plyr = game.Players.LocalPlayer local pgui = plyr.PlayerGui local bpack = plyr.Backpack local enabled = true local key = script.Parent.Value -- Value is "One", for the 1 key. userInputService.InputBegan:connect(function(input, processed) if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == key then -- Key is the string print("Variable key was pressed!") end end)
Anyways my question is, is this possible and if so how?
Because you're dealing with UserInputService, I assume you know or have some knowledge about tables. Did you know that you can directly index the Enum.KeyCode
table? :D
Assuming that the 'key' variable is a valid member of KeyCode Enums, you can use brackets [ ]
to index the KeyCode Enum table.
local userInputService = game:GetService("UserInputService") local plyr = game.Players.LocalPlayer local pgui = plyr.PlayerGui local bpack = plyr.Backpack local enabled = true local key = script.Parent.Value -- Value is "One", for the 1 key. userInputService.InputBegan:connect(function(input, processed) if not processed then if input.UserInputType == Enum.UserInputType.Keyboard then --Index the enum table with the variable! if input.KeyCode == Enum.KeyCode[key] then print("Variable key was pressed!") end end end end)
As with most code there are multiple methods to solve the problem, in your case it will depend upon how complex your key bindings are.
If you are using single keys then I would recommend using the ContextActionService as it will provide all of the functionality that you need.
The down side to the ContextActionService is that you cannot use multiple keys ie Alt+del but you can map multiple keys to the same function meaning that all of the listed keys will fire the given function.
A quick example of how to use the basics of binding keys:-
-- get the service local conActionServ = game:GetService('ContextActionService') -- the function is passed the action name, input state and the input object by the ContextActionService local testFunction = function(str, inpState, inpObj) print(str, inpState, inpObj.KeyCode) end -- bind the function to the key/s you would probably use one key conActionServ:BindAction('a', testFunction, false, Enum.KeyCode.P, Enum.KeyCode.O) -- waits 10 and unbinds the action wait(10) conActionServ:UnbindAction('a') -- now we bind only P conActionServ:BindAction('a', testFunction, false, Enum.KeyCode.P)
For your case you would need to first check that the key is not being used. You can use the function GetAllBoundActionInfo which passes back the infomation you need to check if the keys are being used.
You can use a simple function to loop each action to check its key code:-
-- retuns true if the key is in use local function checkForKey(key) -- action list for actionName, data in pairs(conActionServ:GetAllBoundActionInfo()) do -- action for inpObj, keyList in pairs(data) do -- key code bound to action for i=1, #keyList do if key == keyList[i] then return true end end end end return false end