How would I make it so that when you click the Left/or Right mouse button's they will do something different?

I’m currently making a on/off light source, and the thing I’m trying to achieve is that when you click using the Right Mouse Button it will turn off, then when you click the Left Mouse Button it will turn on. The thing is I’m unsure on how to achieve this as I’ve not found anything on the forums or API.

Code:

local Part = game.Workspace.Part1
local ClickDetector = script.Parent.ClickDetector
local Light = Part.SpotLight

	ClickDetector.MouseButton1Click:connect(function()
		Light.Enabled = true
		for brightness = 0.1, 1, 0.1 do
			Light.Brightness = brightness
			wait(0.05)
		end	
	end)

	ClickDetector.MouseButton2Click:Connect(function()
		for brightness = 1, 0, 0.1 do
			Light.Brightness = brightness
			wait(0.05)
			Light.Enabled = false
		
			end
		end)
	ClickDetector.MouseClick:connect(function()
		Light.Enabled = true
		for brightness = 0.1, 1, 0.1 do
			Light.Brightness = brightness
			wait(0.05)
		end	
	end)

	ClickDetector.RightMouseClick:Connect(function()
		for brightness = 1, 0, 0.1 do
			Light.Brightness = brightness
			wait(0.05)
			Light.Enabled = false
		
			end
		end)

Use RightMouseClick and MouseClick, MouseButton1Click are used for UIs.

RightMouseClick Doesn’t work when pressing the Right Mouse Button… only the left one works…

I think it’s cause of the way it is set up in your code, you have it so when you right click, it makes a for loop going from 1 to 0, in increments of 0.1. In that case the condition for that loop will never be met. Did you mean to do

local Part = game.Workspace.Part1
local ClickDetector = script.Parent.ClickDetector
local Light = Part.SpotLight

	ClickDetector.MouseClick:connect(function()
		Light.Enabled = true
		for brightness = 0, 1, 0.1 do
			Light.Brightness = brightness
			wait(0.05)
		end	
	end)

	ClickDetector.RightMouseClick:Connect(function()
		for brightness = 1, 0, -0.1 do
			Light.Brightness = brightness
			wait(0.05)	
		end
        Light.Enabled = false
	end)
1 Like