Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Using variables for UserInput?

Asked by 7 years ago

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?

0
I find this very interesting as I would like to know how to do this aswell. I feel like wiki doesn't explain enough sometimes abnotaddable 920 — 7y

2 answers

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

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.

note: if 'key' is not a valid member of KeyCode Enums, this will throw an error.
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)
0
You know, this is exactly the first thing I tried. I must've did it wrong since I was working for about 3 hours w/ no breaks, but it worked. OneTruePain 191 — 7y
Ad
Log in to vote
-1
Answered by 7 years ago

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

Answer this question