ContextActionService:UnbindAction() no longer fires the action with Enum.UserInputState.Cancel

ContextActionService:UnbindAction() no longer fires the action with Enum.UserInputState.Cancel

Code:

local cas = game:GetService("ContextActionService")

cas:BindAction("Test", function(_, state, inp)
	print("STATE:", state)
	
	
end, false, Enum.KeyCode.E)

cas:BindAction("Test2", function(_, state, inp)
	cas:UnbindAction("Test")
	print("Unbound")
end, false, Enum.KeyCode.F)

Output:

  22:48:08.477  STATE: Enum.UserInputState.Begin  -  Client - LocalScript:4
  22:48:08.577  STATE: Enum.UserInputState.End  -  Client - LocalScript:4
  22:48:09.227  STATE: Enum.UserInputState.Begin  -  Client - LocalScript:4
  22:48:09.561  STATE: Enum.UserInputState.End  -  Client - LocalScript:4
  22:48:10.026   ▶ Unbound (x6)  -  Client - LocalScript:12

cas_bug_repro.rbxl (52.9 KB)

System info:
Win 11
All betas enabled
This bug occurs online as well as in studio.

Expected behavior

Unbinding should fire the first function with state == Enum.UserInputState.Cancel

7 Likes

I am experiencing this major minor annoying pesky bug as well. The docs say it should work AND IT NO WORKIE. Sobbing irl

1 Like

bump :・゚✧:・゚✧*:・゚✧*:・゚✧*:・゚✧*:・゚✧*:・゚✧*:・゚✧*:・゚✧*:・゚✧*:・゚✧*:・゚✧*:・゚✧*:・゚✧*:・゚✧*:・゚✧

1 Like

Bada bum bum bummmmmmmmp :pray::pray::pray::pray::pray::pray:
(did not mean to reply :face_with_diagonal_mouth:)

1 Like

We have a fix coming later this week. Thanks for the report!

2 Likes

This should be fixed now. Thanks!

1 Like

The fix probably caused a different issue: now the target function is executed when binding the action as well as when unbinding it. Repro (LocalScript):

local actSvc = game:GetService("ContextActionService")

function someAction()
	print("Executing test function")
end

actSvc:BindAction("PressE", someAction, false, Enum.KeyCode.E)
wait()
actSvc:UnbindAction("PressE")

Output:
▶ Executing test function (x2)

The function was called 2 times despite the “E” key was never pressed.

2 Likes

I am not getting this result on my end (I appended your code + some extra prints to my repro)

local cas = game:GetService("ContextActionService")

cas:BindAction("Test", function(_, state, inp)
	print("STATE:", state)
	
	
end, false, Enum.KeyCode.E)

cas:BindAction("Test2", function(_, state, inp)
	
	cas:UnbindAction("Test")
	print("Unbound")
end, false, Enum.KeyCode.F)


local function someAction(...)
	print("Executed", ...)
end
print("Binding action...")
cas:BindAction("PressE", someAction, false, Enum.KeyCode.R)

print("Waiting...")
wait()

print("Unbinding action...")
cas:UnbindAction("PressE")
  16:58:03.822  Binding action...  -  Client - LocalScript:19
  16:58:03.822  Waiting...  -  Client - LocalScript:22
  16:58:04.945  Unbinding action...  -  Client - LocalScript:25
  16:58:04.945  Executed PressE Enum.UserInputState.Cancel InputObject  -  Client - LocalScript:17

Hey. I has issue with UnbindAction() in tool equipped.
code:

local tool = script.Parent
local cas = game:GetService("ContextActionService")

tool.Equipped:Connect(function()
	cas:BindAction("Test", function(_, state, inp)
		print("STATE:", state)
		local block = Instance.new("Part")
		block.Name = "Block"
		block.BrickColor = BrickColor.new("Cool yellow")
		block.Size = Vector3.new(2, 2, 2)
		block.Position = tool.Handle.Position + Vector3.new(0, -1.5, 0)
		block.Parent = workspace
	end, false, Enum.KeyCode.E)
end)

tool.Unequipped:Connect(function()
	cas:UnbindAction("Test")
end)

Test Tool.rbxl (54.7 KB)

Expected behavior

Tool equipped is active and unequipped is deactive, while UnbindAction() still occurs after ‘‘This should be fixed now’’.

You ARE getting the same result as me. The function someAction was called and printed “Executed” despite the fact that the actual key was never pressed.

Th at’s intended behavior. You need to check the state to see if it is being called with “Begin”, “End”, or “Cancel” to differentiate. Otherwise the function would run every time you pressed OR released the key.

This is how ContextActionService is meant to be used:

local function someAction(actionName, state, inputObject)
	if state == Enum.UserInputState.Begin then
		print("Key is pressed:", inputObject.KeyCode)
	elseif state == Enum.UserInputState.End then
		print("Key is released:", inputObject.KeyCode)
	elseif state == Enum.UserInputState.Cancel then
		print(actionName.." has been unbound")
	end
end

cas:BindAction("PressE", someAction, false, Enum.KeyCode.R)

Here My test:

code:

local actSvc = game:GetService("ContextActionService")
local player = game.Players.LocalPlayer
local char = player.Character
local light = script.Parent.ImageLabel

local function someAction(actionName, state, inputObject)
	if state == Enum.UserInputState.Begin then
		print("Key is pressed:", inputObject.KeyCode)
	elseif state == Enum.UserInputState.End then
		print("Key is released:", inputObject.KeyCode)
	elseif state == Enum.UserInputState.Cancel then
		print(actionName.." has been unbound")
	end
	local block = Instance.new("Part")
	block.Name = "Block"
	block.BrickColor = BrickColor.new("Cool yellow")
	block.Size = Vector3.new(2, 2, 2)
	block.Position = char.Head.Position + Vector3.new(0, -4.5, 0)
	block.Parent = workspace
end

while true do
	task.wait(5)
	actSvc:BindAction("PressE", someAction, false, Enum.KeyCode.R)
	light.Image = "http://www.roblox.com/asset/?id=13214238968"
	task.wait(5)
	actSvc:UnbindAction("PressE")
	light.Image = "http://www.roblox.com/asset/?id=13214229273"
end

shotscreen:


Roblox Test1a

Light red and green.rbxl (57.3 KB)

Issue with UnbindAction() while not press.

I am curious how it would be the intended behavior that a function cited in an unbindAction where the intent is to remove the mapped inputs that would trigger it should also result in that function being executed? I don’t recall this being documented behavior for the method, and it wasn’t occurring for the past year. This “fix” looks to have introduced a behavior breaking existing implementations, and I’m not sure I follow what use case should result in the deliberate unbinding of a context action (intended to prevent a function from being called by those inputs) should also result in that very function you’re unbinding to actually be called. That sounds counter intuitive and a break from how it’s been functioning for over a year.

1 Like

I read through the documentation for UnbindAction (ContextActionService | Documentation - Roblox Creator Hub) and for UserInputState Cancel (UserInputState | Documentation - Roblox Creator Hub), and it says it’s only supposed to fire Cancel for in-progress input… but instead it now appears to always be firing the function 100% of the time on unbindAction, even when there was no input. That’s not been happening in the past year, and it now appears to be occurring since this fix, which suggests a bug was introduced.

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