Why when I unbind action, it fires binded function?

I don’t understand why when I unbind action it fires binded function.
My script:

local WorkspaceService = game:GetService("Workspace")
local ContextActionService = game:GetService("ContextActionService")

local Part = WorkspaceService:WaitForChild("ContextActionServicePart")

local function OnButtonPressed()--it fires this
	Part.BrickColor = BrickColor.new("New Yeller")
end

ContextActionService:BindAction("TurnBrickYellow",OnButtonPressed,true,Enum.KeyCode.T)
task.wait(5)
ContextActionService:UnbindAction("TurnBrickYellow")--when i use this

Is this the exact code? If it is a variation of this, please share

1 Like

Yeah it’s full code, just for test.

I think i fixed issue by myself.
I added line to check if state is cancel.
It works for me.

local WorkspaceService = game:GetService("Workspace")
local ContextActionService = game:GetService("ContextActionService")

local Part = WorkspaceService:WaitForChild("ContextActionServicePart")

local function OnButtonPressed(ActionName,State,InputObject)
	if State == Enum.UserInputState.Cancel then return end
	Part.BrickColor = BrickColor.new("New Yeller")
end

ContextActionService:BindAction("TurnBrickYellow",OnButtonPressed,true,Enum.KeyCode.T)
task.wait(5)
ContextActionService:UnbindAction("TurnBrickYellow")

Refer to the UserInputState documentation. You should only be executing your action when you receive a “Begin” event, as there are several different events which probably aren’t what you want.

1 Like

Although this is good practice and excellent advice, the act of using the unbindAction is to remove the function from being available from inputs. This method does not document it would execute the bound function when unbinding, and this behavior wasn’t occurring a couple months back. This actually seems like it may be a bug.

I’ve had inputs with BindAction in my game for over a year now and previously had no issues with use of unbindAction. It would take the previous keys mapped to a function and do an unbindAction of it followed by a BindAction of the keys to a new Confirm dialogue’s “Yes” button function to make it quicker to affirm and close (otherwise it defaults to “No”)… And this is done in reverse when the Confirm dialogue closes so the mapped keys are back to their previous functions. Although, I’ve received bug reports for my game the past month that it has started always defaulting to “Yes”… And that’s when I stumbled onto this post that the unbindAction is now executing the function its removing from context and inputs, which I just confirmed is happening in our game.

To me at least this appears to be a change in behavior. The unbindAction shouldn’t auto-execute the mapped function that you are trying to prevent having input and being used. I’ve had to implement a similar work-around as the original poster in the interim which also references the state of the Confirm dialogue to ensure the button / function is still valid for use when called from the unbindAction so as not to inadvertently set.

2 Likes

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