#User Input Dictionary Help

5 messages · Page 1 of 1 (latest)

brittle gulch
#

Hello. I'm new to Lua and Trying to set up an Input Dictionary, but I am getting an error. Any help would be much appreciated. Thanks

``lua
local UserInputService = game:GetService("UserInputService")

local inputDictionary = {
["Tab"] = print("Player has pressed Tab!"),
["R"] = print("Player has pressed R!")
}

UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
local keyPressed = input.KeyCode
inputDictionary[keyPressed] -- Incomplete statement: expected assignment or a function call
end
end)
``

formal pond
#
local InputDictionary = {
    ["Tab"] = function() print("a") end,
    ["R"] = function() print("A") end
}

uis.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local action = InputDictionary[input.KeyCode.Name]
        if action then
            action()
        end
    end
end)
brittle gulch
#

Thank you! Works like a charm. Is this the way you would recommend handling input?

formal pond