Can I disable the number keys for equipping gears?

I would like to rebind the number keys, perhaps with UsersInputService.InputBegan, but when I do that the numbers still toggle gears. Could this be done with ContextActionService? I found this snippet for disabling pressing S to move backwards

local ContextActionService = game:GetService("ContextActionService")
ContextActionService:UnbindAction("moveBackwardAction")

code that demonstrates this:

local ContextActionService = game:GetService("ContextActionService")

local function handleNumberKeyPress(actionName, inputState, inputObject)
    if inputState == Enum.UserInputState.Begin then
        print(actionName .. " pressed")
        -- Your custom logic here
        return Enum.ContextActionResult.Sink -- Prevents default behavior
    end
end

-- Bind action for number keys 1 to 9
for i = 1, 9 do
    local actionName = "NumberKey" .. i
    ContextActionService:BindAction(actionName, handleNumberKeyPress, false, Enum.KeyCode["Number" .. i])
end

Oh thanks for the quick response. I thought I would have to unbind the number keys with UnbindAction. I made another post: Is there a list of all the default binded actions in ContextActionService?

@RobloxHasTalentR I noticed an error in the snippet, though idk if I was supposed to fill it out on my own: Enum.KeyCode["Number" .. i]. Since I only want to remap some of the numbers I just made the for loop iterate over an array:

Edit: In this snippet I actually do need tostring() and tonumber() for it to work.

local function gearKeys(actionName, inputState)
    if inputState == Enum.UserInputState.Begin then 
    -- you custom logic below
    toggleGear(items[tonumber(actionName)][1]) return Enum.ContextActionResult.Sink
end end
for i, n in {"Four", "Six", "Eight", "Nine", "Zero"} do 
    game:GetService("ContextActionService"):BindAction(tostring(i), gearKeys, false, Enum.KeyCode[n]) end
local function gearKeys(actionName, inputState)
    if inputState == Enum.UserInputState.Begin then
        toggleGear(items[actionName][1])
        return Enum.ContextActionResult.Sink
    end
end

local keyMappings = {
    ["Four"] = Enum.KeyCode.Key4, 
    ["Six"]  = Enum.KeyCode.Key6,  
    ["Eight"] = Enum.KeyCode.Key8,  
    ["Nine"]  = Enum.KeyCode.Key9,  
    ["Zero"]  = Enum.KeyCode.Key0,  
}

for keyName, keyCode in pairs(keyMappings) do
    game:GetService("ContextActionService"):BindAction(keyName, gearKeys, false, keyCode)
end

I’m using i - the array index in the for loop as the actionName in my script.

1 Like

I for the array is fine that solves it

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.