I’m making a basic LocalScript using ContextActionService to bind crouch and sprint actions to C and Left Shift, respectively. When you hold Left Shift, hold C, and then let go of Left Shift, it will fire an extra “Begin” InputState from the C key, even though it’s not being pressed again.
Here’s my output showing the extra “Begin” InputState (line 4):
Here’s a stripped-down script that will repeatedly cause this error:
local contextActionService = game:GetService("ContextActionService")
local function crouch (actionName, inputState, inputObject)
print("Crouch "..tostring(inputState))
end
local function sprint (actionName, inputState, inputObject)
print("Sprint "..tostring(inputState))
end
contextActionService:BindAction("Crouch", crouch, false, Enum.KeyCode.C)
contextActionService:BindAction("Sprint", sprint, false, Enum.KeyCode.LeftShift)
I would surely appreciate any information on why this is happening. Thanks in advance.
I’m not exactly sure why this happens, but :BindActionAtPriority deals with issues with overlapping key inputs. It allows you to set the priority of an input as the 4th argument.
Here’s the same code you provided but using the different method:
local contextActionService = game:GetService("ContextActionService")
local function crouch (actionName, inputState, inputObject)
print("Crouch "..tostring(inputState))
end
local function sprint (actionName, inputState, inputObject)
print("Sprint "..tostring(inputState))
end
contextActionService:BindActionAtPriority("Crouch", crouch, false, 2, Enum.KeyCode.C)
contextActionService:BindActionAtPriority("Sprint", sprint, false, 1, Enum.KeyCode.LeftShift)
Thanks for the reply. That is an interesting solution idea; however, it still is having the same issue. I tried reversing the priority to see if that would help, but the error persists.
The output you provided shows crouch - sprint - sprint - crouch, but the sequence that causes the bug starts with sprint and then crouch. I appreciate your looking into it though!
It looks like the reason you get an extra trigger after releasing LeftShift is because this key is seen as a “modifier key”. I’m not certain if this is the intended behavior or not, but you can see the change with this code:
local ContextActionService = game:GetService("ContextActionService")
local function crouch(action: string, state: Enum.UserInputState, io: InputObject)
print("Crouch", state, io:IsModifierKeyDown(Enum.ModifierKey.Shift))
end
local function sprint(action: string, state: Enum.UserInputState, io: InputObject)
print("Sprint", state)
end
ContextActionService:BindAction("Crouch", crouch, false, Enum.KeyCode.C)
ContextActionService:BindAction("Sprint", sprint, false, Enum.KeyCode.LeftShift)
Output after holding LeftShift, holding C, releasing LeftShift, releasing C: