Can you activate a tool using the right mouse button?

So I’m trying to make a tool that can do a light attack when the left mouse button is pressed and a heavy attack when the right mouse button is pressed. Is it really possible to create this?

and if it is, is there anywhere I can put this in my local script?:

--Can it be put inside of Tool.Equipped or Tool.Activated?--
Tool.Equipped:Connect(function()
	IdleAnim:Play()
	if Humanoid.WalkSpeed >= 23 then
		Humanoid.WalkSpeed = 16
	end
end)

Tool.Activated:Connect(function()
	if not debounce then
		debounce = true
		CanDamage = true
		Whoosh:Play()
		if combo == 1 then
			AttackAnim:Play()
			combo = 2
		elseif combo == 2 then
			Attack2Anim:Play()
			combo = 1
		end
		wait(DEBOUNCE_NUMBER)
		debounce = false
		CanDamage = false
	end
end)

Any answers? Then please message me! :smiley:

6 Likes

I don’t believe you can but you can use the UserInputService to check if it is a right click.

1 Like

As Enforce said, you will have to use UserInputService to check that the given input is a right click, then you can call Tool:Activate() to manually fire the Activated event. To differentiate between attacks from left/right click, you can store the click in a variable and run different blocks of code in the Activated function based on this variable’s value. For example, this pseudocode:

local mouseButton = "" -- Outside of the scope of both functions, so both can use this variable.
UserInputService.InputBegan:Connect(function(input, gpe)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then -- Left mouse button
		mouseButton = "LEFT"
		Tool:Activate()
	elseif input.UserInputType == Enum.UserInputType.MouseButton2 then -- Right mouse button
		mouseButton = "RIGHT"
		Tool:Activate()
	end
end)

Tool.Activated:Connect(function()
	if mouseButton == "LEFT" then
		-- Do light attack things here
	elseif mouseButton == "RIGHT" then
		-- Do heavy attack things here
	end
end)
9 Likes

Use the Mouse() instead

Example:

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button2Down:Connect(function()
     --Check if the tool is equipped
     --code
end)

You can use user input service or the players mouse to detect a right-click. I also would recommend to check if the tool is equipped first before continuing with execution. Here is a example with the 1_upz’s code:

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Tool = script.Parent
Mouse.Button2Down:Connect(function()
    if Tool.Equipped then
              -- Code here
    end 
end)

Instructions:

  1. Replace part of this Script with your Script, and fix the variables, so they aren’t nil.

  2. Optional: There should be an “Unequipped” function (inside the “Activated” function): add whatever you want to it, so when you unequip the tool, it stops doing something that isn’t supposed to keep happening.

  3. Place this Script in your Tool.

local Player = game.Players.LocalPlayer -- Define
local Tool = script.Parent
local Key -- [Left=false, Right=true]

Tool.Equipped:Connect(function()
	local LeftClick
	local RightClick
	local Activated
	local Unequipped
	
	Activated = Tool.Activated:Connect(function()
		if Key == true then
			if not debounce then
				debounce = true
				CanDamage = true
				Whoosh:Play()
				if combo == 1 then
					AttackAnim:Play()
					combo = 2
				elseif combo == 2 then
					Attack2Anim:Play()
					combo = 1
				end
				wait(DEBOUNCE_NUMBER)
				debounce = false
				CanDamage = false
			end
		end
		Unequipped = Tool.Unequipped:Connect(function()
			Activated:Disconnect()
			Unequipped:Disconnect()
		end)
	end)
	
	LeftClick = Player:GetMouse().Button1Down:Connect(function()
		Key = false
		Tool:Activate()
		Unequipped = Tool.Unequipped:Connect(function()
			LeftClick:Disconnect()
			Unequipped:Disconnect()
		end)
	end)
	
	RightClick = Player:GetMouse().Button2Down:Connect(function()
		Key = true
		Tool:Activate()
		Unequipped = Tool.Unequipped:Connect(function()
			RightClick:Disconnect()
			Unequipped:Disconnect()
		end)
	end)
end)
2 Likes

@DevCramp , @1_upz , @AC_Starmarine
UserInputService is better. Mouse is the legacy method of UserInputService.

However, ContextActionService would be the best.

local ctx = game:GetService("ContextActionService")

local function rightClick()
	-- Do whatever
end

ctx:BindAction("rightClickAction", rightClick, true, Enum.UserInputType.MouseButton2, Enum.KeyCode.ButtonA)
-- Name of the action, the function, if a mobile button is created, the click, and the button for Xbox.
-- This is for if the tool gets equipped. Use tool.Equipped:Connect(function()
ctx:SetTitle("rightClickAction", "Use tool")
-- Mobile Button for text
ctx:SetImage("rightClickAction", "rbxassetid://123")
-- Mobile Button for image
ctx:UnbindAction("rightClickAction")
-- For removing it when the tool is unequipped. Use tool.Unequipped:Connect(function()

Reasons ContextActionService is better than both UserInputService and Mouse

  1. It has cross platform support (Xbox AND mobile :slight_smile: )
  2. If you were hovering on the chat, it would not activate.
  3. Doesn’t fire for every input
3 Likes

if it is in a local script you can do Button1Down for a left click, and Button2Down for a right click

local player = game.Players.LocalPlayer
local  mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
	print("Left btn")
end)

mouse.Button2Down:Connect(function()
	print("Right btn")
end)
2 Likes