Right click alt fire for a tool

Im trying to create a weapon with 2 firing modes, left click and right click, already got .Activated but i couldnt figure out how to do the right click part. All i need is how to activate a function when the player holding the tool presses right click.

1 Like
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)

not mine but works
post: Can you activate a tool using the right mouse button? - #3 by punyman9?

Tried this

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
		print("L")
	elseif mouseButton == "RIGHT" then
		print("R")
	end
end)

Nothing, not even errors

UserInputService can be directly used for this:

--LocalScript inside the Tool
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer 
local Tool = script.Parent 

UserInputService.InputBegan:Connect(function(input)
	--if the tool is parented inside their character, they are holding the tool
	local Equipped = (Tool.Parent == Player.Character)
	--if the tool isn't equipped, do nothing
	if not Equipped then return end 
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		print("left")
	elseif input.UserInputType == Enum.UserInputType.MouseButton2 then 
		print("right")
	end
end)

u are using a local script inside the player or character right?
cause a normal “script” doesn’t work for keypresses

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

local tool = script.Parent

local equipped = false

tool.Activated:Connect(function()
	--Do stuff here.
end)

tool.Deactivated:Connect(function()
	--Do stuff here.
end)

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

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

mouse.Button2Down:Connect(function()
	if equipped then --Make sure tool is equipped.
		--Perform action when right mouse button is clicked.
	end
end)

Local script inside the tool.

Proof (print commands executed when right mouse button was clicked while the tool was equipped):
image