Context action service

I got this script where it has a toggle variable. You click once the variable becomes true, you click again it becomes false and repeat. However, if you hold the key it becomes true but when you stop holding it, it becomes false. I am trying to find a way for that not to happen.

The script:

local function testFunction()

	if test == false then
		test = true
		print(test)
	elseif test == true then
		test = false
		print(test)
	end
end	

CAS:BindAction("name",testFunction,true,Enum.KeyCode.F)
CAS:SetTitle("name","test")
CAS:SetPosition("name",UDim2.fromScale(0.54,0.786))

Second return of a Bindaction function is the state of the action, you can check if it’s the start of an action via checking if the inputstate is Begin

Basically change your testFunction to this

local function testFunction(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		test = not test
		print(test)	
	end
end	

Also you can simply invert the variable instead of an if statement

1 Like