Context Action Service Questions

CAServ:BindAction("Left Punch", leftPunch, false, Enum.UserInputType.MouseButton1)
CAServ:BindAction("Right Punch", rightPunch, false, Enum.UserInputType.MouseButton2)
CAServ:BindAction("Kick", kick, false, Enum.KeyCode.E)

All three of these have an issue. They trigger not only when I press them down, but also when I lift them up. How can I fix this?

BONUS
When I bound the MB2, it now no longer works with moving the camera. How can I have it bound to my use, but also camera movement, at the same time?

For number one check the user input type for begin. See @Pr0pelled for the if statement example and the CAS article for code examples on it ContextActionService | Roblox Creator Documentation.

For the second try returning context action result to avoid sinking input.

If an action handler returns Enum.ContextActionResult.Pass, the next most recently bound action handler will be called

1 Like

An answer to the first question:
The function is triggered any time a change is made to the input (if that makes sense).
There are many different states, see this for more info:

If you only want it firing when the button is pressed down, then you can check in the handler function like this:

if inputState == Enum.UserInputState.Begin then
    --Do stuff here
end

An answer to question number two:
The default roblox controller scripts use ContextActionService to bind the inputs, with CAS, you can only have one thing bound to each key at a time, so by binding to MB2, you are overwriting the default controller script’s bind, and it therefore does not detect an input from that anymore.

2 Likes

For number 1,
Right, but I can’t do that whole if statement thing inside the arguments for bounding keys with CAS right? Because it just asks for the keycode?

For number 2,
So that’s why the problem is occurring, but how can I fix this?

For 1, CAS fire the function given with 3 parameters.


To ask this question you obviously haven’t understand the API.

For 2, honestly don’t know.

Well I just did a thing called “decided to look it up and learn” :sunglasses:
And now I get it, thanks for the help!

That sounds sarcastic
It isn’t

local contextAction = game:GetService("ContextActionService")

local function kickMove(actionName, inputState, inputObject)
	if actionName == "Kick" then --This is a given.
		if inputState.UserInputState == Enum.UserInputState.Begin then --This checks the state of the input (began, changed, ended).
			if inputObject.KeyCode == Enum.KeyCode.E then --This checks the input object (the input which triggered the action).
				--Perform "kick" action here.
			end
		end
	end
end

contextAction:BindAction("Kick", kickMove, false, Enum.KeyCode.E)

Here’s what you should be doing, using your “kick” action as an example.