Multiple Userinputservice events

I want to make a lot of things activated by user input service, but they require graphics so I need 2 scripts each for what I can see.

Can anybody then show me a way more efficient way of doing this without having to go through 20 scripts just to find one thing?

What does this mean, and why does it mean you have to use two scripts for “each”?

Like you know something different happens for each thing. But i can’t find an efficent way of making it better scripted without it getting messy.

You can create a dictionary with the keyboard input as the key, and a function as the value, like so:

    local UserInputService = game:GetService("UserInputService")

    local userInputDictionary = {
        ["A"] = --some random func,
        ["B"] = --some random func
    }

    UserInputService.InputBegan:Connect(function(input)
        if input.UserInputType == Enum.UserInputType.Keyboard then
            local keyPressed = input.KeyCode
            userInputDictionary[keyPressed] -- this line will invoke the func --
        end
    end)
3 Likes

I somehow never knew this, so thank you very much!

1 Like

Uhm… how do I do that with Module scripts where I need to pass a value to it?

-- modulescript
local InputDictionary = {
    ["A"] = function()
}

return InputDictionary

-- localscript
local InputDictionary = require(path.to.dictionary)

UserInputService.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.Keyboard then
      local keyPressed = input.KeyCode
        InputDictionary[keyPressed] -- this line will invoke whatever is in the dictionary
      end
end)
1 Like

Sorry although your script works wouldn’t it be better to use

local InputDictionary = {}

InputDictionary["A"] = function()
    --Stuff here
end

return InputDictionary

Or am i losing the plot?

Edit: i just realized im dumb disregard that

2 Likes

LOL, don’t worry about it. That works too, it’s just up to preference.