BindActionAtPriority() has a faulty code sample

Page: ContextActionService | Documentation - Roblox Creator Hub

Issue: ContextActionService:BindActionAtPriority() doesn’t have a working example. The fundamental problem is a missing return Enum.ContextActionResult.Pass in both called functions. There are also a name confusion and a typo present.

Solution: I suggest the code sample be rewritten to the following example with a couple of improvements:

local ContextActionService = game:GetService("ContextActionService")

local function handleThrow(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin then
		print("Throw")
	end
	-- By default, context action result is 'Sink'
	return Enum.ContextActionResult.Pass
end

local function handlePunch(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin then
		print("Punch")
	end
	return Enum.ContextActionResult.Pass
end

-- Punch is "stacked" as second, thus bound at a higher priority.
ContextActionService:BindAction("BindActionThrow", handleThrow, false, Enum.KeyCode.E)
ContextActionService:BindAction("BindActionPunch", handlePunch, false, Enum.KeyCode.E)

-- Throw is bound at a higher priority than Punch (priority 2 > priority 1).
-- Despite the order in which the functions were bound, handleThrow is going to
-- be called before handlePunch once Q is pressed.
ContextActionService:BindActionAtPriority("BindAtPriorityThrow", handleThrow, false, 2, Enum.KeyCode.Q)
ContextActionService:BindActionAtPriority("BindAtPriorityPunch", handlePunch, false, 1, Enum.KeyCode.Q)

All the best!

2 Likes

We’ve filed a ticket into our internal database for this issue, and will come back as soon as we have updates!

Thanks for flagging!

3 Likes

Nice, thank you.

I also see you’ve removed the solution, and I understand why. My bad. :slight_smile:

1 Like

Please don’t mark a reply as a solution if it doesn’t include a resolution to the issue, as a topic lock timer will turn on otherwise! @waves5217

Thank you for understanding!

Thanks for taking time to provide feedback on this!
The sample is now revised:

  • Fixed typo handePunchhandlePunch
  • Added inputState checks to avoid confusing double print from Begin/End events
  • Changed implicit Sink (no return) to explicitly return Sink
  • Revised comments, adding expected behavior
  • Added more descriptive action names
  • Swapped default/prioritized binding order to demonstrate default behavior before highlighting the difference with priorities

Note that both in the original and the revised samples, they intentionally sink the input so that either “Punch” or “Throw” is printed for each key press, but never both.

2 Likes

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