ContextActionService Runs Function Twice

So I’m using ContextActionService for my game in order to make my game mobile-friendly, however, what I’ve noticed is that when I use :BindAction() in order to run a function, it will run that function twice, when I press the key, and when I let go of the key, I only want the function running when the key is first pressed, but I don’t want to use UserInputService since I want to use all the perks ContextActionService gives, here’s my code:

--LocalScript
local function leftClickAction()
	game.ReplicatedStorage.BasicAttackPlay:FireServer()
end
game.ContextActionService:BindActivate("leftClick",leftClickAction, true, Enum.UserInputType.MouseButton1)
--ServerScript
game.ReplicatedStorage.SideAttackPlay.OnServerEvent:Connect(function(player)

	local dummy0 = game.Workspace[tostring(player)]
	
	local animTrack0 = dummy0.Humanoid:LoadAnimation(game.ReplicatedStorage.SideAttackAnimation)

	animTrack0:Play()
end)
2 Likes

ContextActionService is fired twice, on input and off input.

To work around this, use the UserInputState to direct the function to either input. You can read more on the API and find examples here.

local function handleAction(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin then
		-- Input began
	else
		-- Input ended
	end
end

ContextActionService:BindAction("Action Name", handleAction, true, Enum.KeyCode.H)
5 Likes