Context Action Service

I want to learn Context Action Service. Can yall share examples, demonstrations, and explanations of how to use it, and why to use it? Thank you in advance

A basic summary of ContextActionService is that it is used to bind button presses to actions. If you want to, it also can make mobile buttons for you too. It is generally more precise and easier to code these things using ContextActionService rather than UserInputService. Since it deals with client-side inputs, menaing it only works in a LocalScript. Here’s an example of a simple sprint script using ContextActionService:

local CAS = game:GetService("ContextActionService")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local function MovementAction(actionName, inputState, inputObject)
	if actionName == "Sprint" then
		if inputState == Enum.UserInputState.Begin then
			Humanoid.WalkSpeed = 32
		elseif inputState == Enum.UserInputState.End then	
			Humanoid.WalkSpeed = 16
		end
	end
end

CAS:BindAction("Sprint", MovementAction, true, Enum.KeyCode.LeftShift, Enum.KeyCode.RightShift, Enum.KeyCode.ButtonX)

local SprintButton = CAS:GetButton("Sprint")

CAS:SetPosition("Sprint", UDim2.new(1, -89, 0, 5))
CAS:SetTitle("Sprint", "Sprint")

if SprintButton ~= nil then
	SprintButton.Size = UDim2.new(0.25, 0, 0.32, 0)
end
1 Like

Umm so it is similar to UserInputService. Why is it better than UserInputService? Isn’t it the same, almost? Also, can it be used for checking if the player is inside a frame, and if he is, and press like W, instead of moving, he does something of the frame that needs the W button?

To clarify, no, ContextActionService is not necessarily better than UserInputService. It just handles binding buttons to actions and making mobile buttons easier, but it isn’t really better, as UserInputService has the advantage of easier general button press usage. I also think you can try to bind the W button to do something in the frame if the player is inside the frame.

1 Like

You can just use userinputservice, I’ve never used contextactionservice because I find it so useless. You can detect what device a player is on using userinputservice aswell so contextactionservice basically has no advantage.

Ohh, so the main functionality of the Context Action Service is to handle mobile buttons so that when they press it makes something? Also with the W example, I mean like:

Imagine I press E to open a frame, and I want it so that when the player presses the Up or Down button, the text is selected, but I want the Up/Down button to work ONLY if the frame is visible. If the frame isn’t visible, then the Up or Down button would be used for moving the character.

I’ll use both, as both can be useful, as @J_Angry said.

Yeah I guess it’s your choice. But I would suggest sticking to uis.

2 Likes

Yes, I think you get the point. It just handles button presses to actions and making mobile buttons easier and simpler to make. For the W example though, I would recommend UserInputService for that one. If it helps to understand them both more, you can look at their respective pages: ContextActionService, UserInputService.

1 Like

Alright. Thank you! How could I do the W thing tho?

I find ContextActionService useful.
I have a gas mask in my game, that used UserInputService with the “G” key. Only issue is, when players were using the mask while in the gas, when they were typing in the chat, the gas mask would unequip if they pressed “G” while typing. I changed this to use CAS instead, as I found the keys wouldn’t register if the user was typing in the chat. That’s the only difference I know of, but I found that useful for my game.

2 Likes

I’m late, but ill throw this out. The second parameter of InputBegan basically checks if the player is typing. For example :

UserInputService:InputBegan:Connect(function(input, isTyping)
        if isTyping == false then
--the normal script

so basically if you use this script and you are typing, the function wont run.

1 Like

Nice thanks! I’ll keep this in mind in future.

1 Like