Context Action Service messing up Tool.Activated

Context Action Service conflicts with tool.activated, i researched inside my game by enabling and disabling scripts and the one that had this lines of code was the issue:

cas:BindAction("M1",M1,true,Enum.UserInputType.MouseButton1,Enum.KeyCode.ButtonR1)
cas:BindAction("M2",M1,true,Enum.UserInputType.MouseButton1,Enum.KeyCode.ButtonR2)

Tried disabling the script and it worked also deleted the line but i need the mouse button 1 for my attacks so i couldn’t find a great solution.

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()

local cas = game:GetService("ContextActionService")

local Available = Char:WaitForChild("Available")

local UIS = game:GetService("UserInputService")

local fx = require(game.ReplicatedStorage.Modules.FX)

local ctrlHold = false

local onPhone = UIS.TouchEnabled

local db = false
local db2 = false
local db3 = false
local db4 = false

local PrepAnim = Instance.new("Animation")
PrepAnim.AnimationId = "rbxassetid://7967648696"

local PlayAnim = Char.Humanoid:LoadAnimation(PrepAnim)

function Ki(actionName, inputState)
	if Char.Stats.Ki.Value > 5 and Available and not db and inputState == Enum.UserInputState.Begin then
		db = true
		game.ReplicatedStorage["Ki Blast"]:FireServer(Player:GetMouse().Hit.LookVector*100)
		Wait(.3)
		db = false
	end
end

function M1(actionName, inputState)
	if not db2 and Available.Value == true and inputState == Enum.UserInputState.Begin and not ctrlHold then
		db2 = true
		game.ReplicatedStorage["Punch"]:FireServer(Char.LockOn.Value)
		Wait(.4)
		db2 = false
	end
	if not db3 and Available.Value == true and inputState == Enum.UserInputState.Begin and not onPhone and ctrlHold then
		db3 = true
		game.ReplicatedStorage["Hard Punch"]:FireServer()
		wait(1)
		db3 = false
	elseif not db3 and Available.Value == true and inputState == Enum.UserInputState.Begin and onPhone and actionName == "M2" then
		db3 = true
		game.ReplicatedStorage["Hard Punch"]:FireServer()
		wait(1)
		db3 = false
	end
end

function Block(actionName, inputState)
	if not db4 and Available.Value == true and inputState == Enum.UserInputState.Begin then
		game.ReplicatedStorage.Block:InvokeServer(false)
		Char.Available.Value = false
		PlayAnim:Play()
	elseif inputState == Enum.UserInputState.End then
		db4 = true
		PlayAnim:Stop()
		game.ReplicatedStorage.Block:InvokeServer(true)
		Char.Available.Value = true
		wait(.5)
		db4 = false
	end
end

UIS.InputBegan:Connect(function(key,no)
	if not no then
		if key.KeyCode == Enum.KeyCode.LeftControl then
			ctrlHold = true
		end
	end
end)

UIS.InputEnded:Connect(function(key,no)
	if not no then
		if key.KeyCode == Enum.KeyCode.LeftControl then
			ctrlHold = false
		end
	end
end)

cas:BindAction("Ki",Ki,true,Enum.KeyCode.E,Enum.KeyCode.ButtonA)
cas:BindAction("M1",M1,true,Enum.UserInputType.MouseButton1,Enum.KeyCode.ButtonR1)
cas:BindAction("M2",M1,true,Enum.UserInputType.MouseButton1,Enum.KeyCode.ButtonR2)
cas:BindAction("Block",Block,true,Enum.KeyCode.F,Enum.KeyCode.ButtonL2)
cas:SetTitle("Ki","Ki Blast")
cas:SetTitle("M1","M1")
cas:SetTitle("M2","M2")
cas:SetTitle("Block","Block")
cas:SetPosition("Ki", UDim2.new(1, -70, 0, 10))
cas:SetPosition("M1", UDim2.new(1, -135,0, 30))
cas:SetPosition("M2", UDim2.new(1, -155,0, 90))
cas:SetPosition("Block", UDim2.new(1, -200,0, 30))

When the script of the Context action service is enabled this tool script doesn’t work
image

Sorry, I might have missed this completely, but wouldn’t one solution be to do:

tool.Activated:Connect(function()
	M1()
end)

Ye i need to explain it better, the thing i was trying to explain was that a local script that uses context action service is messing up another local script that is using tool.activated

Oh. I understand. that makes things a bit trickier. You could create a BindableEvent and parent it to the tool. One of the scripts will listen for the actual mouse input and then fire the BindableEvent. The other script will listen for the BindableEvent instead of the actual mouse click.

1 Like

Sorry i don’t understand how that would work since the tool.activated is not listening to the mouse since context action service is messing it up

If I understand it correctly, Tool.Activated is not fired since CAS is “hogging” that input. If the CAS fires a BindableEvent as soon as it receives the input then anything listening to the BindableEvent would be notified of a mouse click. Since tool.Activated is just a click listener (while tool is equipped) you could do:

bindableEvent.Event:Connect(function()
	if not equipped then return end
	-- Tool was activated.
end)

I could do that but i cant think of a way to detect which tool is equiped since im using multiple tools

You could add:

local equipped = false

Tool.Equipped:Connect(function()
	equipped = true
end)

Tool.Unequipped:Connect(function()
	equipped = false
end)

This will only work if you are using roblox’s built-in tool system. If you are using a custom system then you will need to get which tool is equipped that way. Alternatively, when a tool is equipped it’s parent will be Player.Character. Checking for this is also a viable option.

Relevant tool events:
Tool.Equipped (roblox.com)
Tool.Unequipped (roblox.com)