PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

Button2Down

The Button2Down even fires when the the player presses their right mouse button.

This can also be accessed from a Tool. For example, when placed in a LocalScript, the code below prints Button2Down whenever the right mouse button is pressed:

local Tool = script.Parent --make sure this is a Tool object

Tool.Equipped:Connect(function(Mouse)
	Mouse.Button2Down:Connect(function()
		print("Button2Down")
	end)
end).

Developers can find out the position of the mouse in world-space, and if it is pointing at any BasePart, using the Mouse/Hit and Mouse/Target properties.

For information on how to obtain the mouse object, please see the Mouse page.

Note, developers are recommended to use UserInputService instead of the Mouse object in new work.


Code Samples


Color Randomizer Tool

The following code sample, when placed in StarterPlayerScripts will create a tool in the player’s backpack that, once equipped, will change the BasePart/BrickColor of every BasePart the player clicks on.

local Players = game:GetService("Players")

local localPlayer = Players.LocalPlayer
local backpack = localPlayer:WaitForChild("Backpack")

local tool = Instance.new("Tool")
tool.RequiresHandle = false
tool.CanBeDropped = false 
tool.Parent = backpack

tool.Equipped:Connect(function(mouse)
	mouse.Button2Down:Connect(function()
		if mouse.Target and mouse.Target.Parent then
			mouse.Target.BrickColor = BrickColor.Random()
		end
	end)
end)