ContextActionService not working

Im writing a code thats supposed to bind an action when the server tells the client that the game has started, the action will bind properly but not work for some reason.

Server Code:

local RepStor = game:GetService("ReplicatedStorage")
local RemoteEvents = RepStor:WaitForChild("RemoteEvents")
local ActionBinding = RemoteEvents:WaitForChild("ActionBinding")

ActionBinding:FireAllClients()

Client Code:

local RepStor = game:GetService("ReplicatedStorage")
local RemoteEvents = RepStor:WaitForChild("RemoteEvents")
local ActionBinding = RemoteEvents:WaitForChild("ActionBinding")

local function Tackle(actionName, inputState)
	
	print("Clicked")

end

ActionBinding.OnClientEvent:Connect(function()

	CAS:BindAction("attack",Tackle,true,Enum.UserInputType.MouseButton1)
end)

The action also shows up in action bindings in the developer console but still doesn’t work.

Does it print Clicked or not?

It doesn’t 3 0 c h a r a c t e r s

Can you add print("Binding action") in the remote event, and see if it prints when you fire it from server:

ActionBinding.OnClientEvent:Connect(function()
	print("Binding action")
	CAS:BindAction("attack",Tackle,true,Enum.UserInputType.MouseButton1)
end)

Yeah its prints, it also shows up in action bindings in the developer console

Where do you have that script, inside a tool?

Its inside Starter Character Scripts

I tried testing your code in studio, it works perfectly fine for me

Try changing the function to

local function Tackle(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		print("Clicked")
	end
end

This will make it print Clicked only when the player clicks and not when he releases the left mouse button.

1 Like

That doesn’t really help my situation right now. But It seems to work when I use it in a different place, so im gonna try disabling other scripts and see if they’re interfering or something. Thanks for the help though :+1:

1 Like

You might have other scripts that replicate the action binding for Enum.UserInputType.MouseButton1, do CTRL+SHIFT+F to quickly search in all scripts.

1 Like

Alright so I finally figured out the problem, when the game starts all the players are killed so they respawn inside the map. But I guess firing the event to Bind the Action right before they die makes it not work. So I just changed the order and made the event fire once they respawn instead of before they die. Thanks again for all the help and tips though :heart:

3 Likes