ContextActionService detects press and release

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I’m starting a combat system, and when I press ‘R’ I want to go into, or get out of fighting mode.

  1. What is the issue? Include screenshots / videos if possible!

I’d like for ContextActionService to fire after pressing and releasing the key ‘R’.
But it currently only detects the press.

After searching for some while, didin’t find anyhting.


-- Start of variables 
local ContextActionService=game:GetService("ContextActionService")

local Players = game:GetService("Players")

local player= Players.LocalPlayer

local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = player.Character:WaitForChild("Humanoid")

local animator= Humanoid:WaitForChild("Animator")

local animation= Instance.new("Animation")
animation.AnimationId="rbxassetid://13055232360"

local ispair= 1 -- this will help detect if I need to stop or play animation


localanimationTrack = animator:LoadAnimation(animation)

-- End of variables

local function BoxingGuard()--replace idle anim by new one
	if ispair % 2==0 then        -- if it's pair
		localanimationTrack:Stop()
		print("stop pressing R")
	else                         -- if it's not pair
		localanimationTrack:Play()
		print("press button R")
	end
	ispair+=1
end

ContextActionService:BindAction("IdleBoxingGuard",BoxingGuard,false,"r")

Do you know how can I make it so that ContextActionService fires the function after press and release of ‘R’.

1 Like

I believe it passes some value that tells you if the input has began or ended. It will fire once for begin and once for ended.

Change “R” to Enum.KeyCode.R.

2 Likes

It triggers when I do not touch R.

I see a few issues with your code. As @seancool07777 mentioned, you cannot use “r” for the keyboard bindings. You have to use Enum.KeyCode.R for it. Second, your BoxingGuard() function is missing a few parameters.

local function BoxingGuard(actionName, userInputState, inputObject)
	if actionName == "IdleBoxingGuard" then
		if userInputState == Enum.UserInputState.Begin then
			localanimationTrack:Play()
			print("press button R")
		elseif userInputState == Enum.UserInputState.End then
			localanimationTrack:Stop()
			print("stop pressing R")
		end
	end
end

This is in the documentation for ContextActionService.

2 Likes

I’ll go read that more in depths ty

1 Like

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