How would I make aiming down sights for a gun using ContextActionService?

I know you can handle ADS with UserInputService using InputBegan and InputEnded for the right mouse button, but how could I implement this with ContextActionService?

You should probably check out how ContextActionService works if you don’t know already. But in your case, I’d really recommend either creating my own enums/keybinds, or just directly use UserInputService, which is more recommended for this case.

I’m using ContextActionService because there are multiple ADS animations for certain guns. UserInputService would require me to do if/else functions to determine which animation to play, which could induce slight input lag, right? Additionally, how could I make my own enum?

Here’s a rundown of how you could do it.

 local ContextActionService = game:GetService("ContextActionService")

local aiming = false

local function aimDownSights()
	aiming = not aiming
	
	if aiming then
		print("Aiming down sights!")
	else
		print("Un-aiming sights!")
	end
end

ContextActionService:BindAction("AimDownSights", function(_, state, input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		aimDownSights()
	elseif input.UserInputType == Enum.UserInputType.Touch and state == Enum.UserInputState.Begin then -- this second check makes it so that it's toggleable on mobile
		aimDownSights()
	end
	
end, true, Enum.UserInputType.MouseButton2)
1 Like

It requires modulescripts, some knowledge with metatables and OOP