(I now know its not actually a bug but if anyone else has this problem here you can find a solution)
Recently, while figuring out controls for a game, we encountered an issue when trying to disable certain actions by setting another action with higher priority using ContextActionService
. Despite setting a higher priority, it seemed that the handling functions of all actions with the same key bind would still be called the first time, causing unexpected behavior.
Problem
When binding multiple actions to the same key with different priorities, all actions are invoked in the order of their priority. This means that even if you set a higher priority action to override a lower priority one, the lower priority action’s function will still be called the first time, then continue normally until the priority order was changed again.
Example:
Local Script (StarterCharacterScripts)
local ContextActionService = game:GetService("ContextActionService")
local function HandleAction(actionName, inputState)
if inputState == Enum.UserInputState.End then return end
print('Handled in Script')
end
ContextActionService:BindActionAtPriority('HandleAction', HandleAction, true, 1, Enum.KeyCode.F)
Local Script (Under Tool)
local ContextActionService = game:GetService("ContextActionService")
local tool = script.Parent
local function TakeActionPriority()
print('Priority took')
end
tool.Equipped:Connect(function()
ContextActionService:BindActionAtPriority("Priority", TakeActionPriority , true, 2, Enum.KeyCode.F)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction("Priority")
end)
Output as events unfold:
Player Presses F
19:12:32.429 Handled in Script - Client - DevExample:6
Tool equipped
19:12:32.429 Handled in Script - Client - DevExample:6
Tool unequipped
19:12:41.063 Priority took - Client - Main:6
As you may be able to see from the code and output provided below, the handle functions are being called when priority is changed through binding or unbinding.
Solution
To ensure that the higher priority action fully overrides the lower priority action, we modified the input handling functions to check the input object’s input type. This allowed us to bypass the lower priority action when the higher priority action was invoked.
(Some text was clarified using ChatGPT)
Although its possible to solve this issue, its unexpected behavior can be confusing. Not to mention I haven’t seen it mentioned anywhere in the docs that this would happen. However please do contribute to this discussion and correct me on anything, or ask questions if your uncertain what I mean.