Could someone breakdown how CAS works and the code that ROBLOX provides?

Hello, there!

In my last post I made, I asked how to use UIS only is spefic situations and the majority said to use CAS instead. I have been struggling to find resources on the topic of situational CAS so I would be very grateful if someone could explain to me how to use it, and possibly break-down ROBLOX’s provided sample.

Thanks, pevdn

ROBLOX’s provided sample:

local ContextActionService = game:GetService("ContextActionService")
 
-- A car horn sound
local honkSound = Instance.new("Sound", workspace)
honkSound.Looped = true
honkSound.SoundId = "rbxassetid://3017580236"
 
local function handleAction(actionName, inputState, inputObject)
	if actionName == "HonkHorn" then
		if inputState == Enum.UserInputState.Begin then
			honkSound:Play()
		else
			honkSound:Pause()
		end
	end
end
 
-- When the player sits in the vehicle:
ContextActionService:BindAction("HonkHorn", handleAction, true, Enum.KeyCode.H, Enum.KeyCode.ButtonY)
 
-- When the player gets out:
ContextActionService:UnbindAction("HonkHorn")

Usually CAS is better,

For the reasons why you gotta compare it with user input services which is what I have done in the post below :point_down:

The primary reason is dealing with key bindings as CAS makes it easier to unbind rather than UIS where you need to disconnect the connection or use a bool value.

For now I don’t have know a specific case example where user input services would be better than CAS.

Perhaps when you just want to permanently bind a key or always listen to an input.

I have a quick separate question- does CAS have to be used in a LocalScript, like UIS, or can it be used in global scripts?

ContextActionService is atrocious to work with in my opinion. From my experience you’d really only want to use it if you can’t do an InputBegan on something because Roblox has priority control over it (for example scrolling or the I/O keys already being in use by the camera, or wasd/arrows being in use by the default controls)


So you and future developers don’t have to suffer like I (and many others) had to, here’s some cut down example code from a custom camera I made for my game. I commented it a bunch to try and give you a rough idea of what it’s doing. Hopefully it helps at least a little bit.

Example code
local contextActionService = game:GetService("ContextActionService")

local function zoomControl(name,state,input) -- Function that gets called when CAS does stuff
	if name == "zooming" then -- Mouse scrollwheel control
		if input.Position.Z == 1 then -- Scroll up
			if camheight - camChange >= minHeight then
				camheight = camheight - camChange
			end
		elseif input.Position.Z == -1 then -- Scroll down
			if camheight + camChange <= maxHeight then
				camheight = camheight + camChange
			end
		end
	elseif name == "zoomIn" then -- I key
		if camheight - camChange >= minHeight then
			camheight = camheight - camChange
		end
	elseif name == "zoomOut" then -- O key
		if camheight + camChange <= maxHeight then
			camheight = camheight + camChange
		end
	end
end
contextActionService:BindAction("zooming", zoomControl, false, Enum.UserInputType.MouseWheel)
contextActionService:BindAction("zoomIn", zoomControl, false, Enum.KeyCode.I)
contextActionService:BindAction("zoomOut", zoomControl, false, Enum.KeyCode.O)
-- Name that the function refers to, which function gets called, no idea what true/false does but it's called state in the arguments, and which input is going to call this function (most often a keycode for keyboard input)

Note: It doesn’t matter what names you give the actions, as long as the function refers to the action name

Edit: Also I believe it only works in local scripts because it’s listening to local inputs.

contextActionService:BindAction("zooming", zoomControl, false, Enum.UserInputType.MouseWheel)

One more quick question… what would the “false” in this code do?

I don’t have any idea, to be honest. I believe it helps tell if a user is holding a key down, but I’m not entirely sure.

Wouldn’t hurt to experiment, put it as true or false in your code and see if it works as you desire!

Seems to work exactly the same. Well it works either way so I am satisfied.

The false tells ContextActionService to not make a button for touch input devices

1 Like

Ohhh. Thanks for specifying!

(stupid character limit)

1 Like

Interesting, would definitely like to hear about your experiences.

So far your source is only

And

Got any examples or case scenarios to highlight it?

Sorry, you know a lot of false information going around these days.

It being atrocious is entirely my opinion, for all I know it could be the greatest thing to exist in Roblox scripting.
For the others suffering part however, there are many threads under Topics tagged contextactionservice so I’m clearly not the only one having issues on working with it.

1 Like

Any raised concerns about the ‘ContextActionService’ are likely in part a consequence of its complexity in contrast to the ‘UserInputService’.

2 Likes