How do I make a holding clickdetector?

Hi, I need help with the clickdetector that makes a part change color when I click it and holding, and when I stop holding it, the color returns to normal. Thanks!

game.Workspace.Part.ClickDetector.MouseClick.OnClick:Connect(function()
	 game.Workspace.Part.BrickColor = BrickColor.Random()
end

Read this article for more info: Handling Events

1 Like

i think clickdetector cant hold cuz its always down or it just function when the mouse is released
edit: I mean Up

You can’t achieve this with a “ClickDetector” instance as it has no “mouse down”/“mouse released” events. I’d recommend using a “ProximityPrompt” instance instead.

Sticking with a “ClickDetector” instance you could however make use if its “MouseHoverEnter” and “MouseHoverLeave” events, here’s an example script.

local tweens = game:GetService("TweenService")

local click = script.Parent
local part = click.Parent
part.Color = Color3.new(0, 0, 0)

local tween

click.MouseHoverEnter:Connect(function()
	if tween then
		tween:Cancel()
	end
	
	tween = tweens:Create(part, TweenInfo.new(1), {Color = Color3.new(1, 1, 1)})
	tween:Play()
end)

click.MouseHoverLeave:Connect(function()
	if tween then
		tween:Cancel()
	end
	
	tween = tweens:Create(part, TweenInfo.new(1), {Color = Color3.new(0, 0, 0)})
	tween:Play()
end)

When the click detector is focused the part’s color animates from white to black, similarly, when the click detector is unfocused the part’s color animates from black to white.

Here’s the model file for reproduction purposes.

repro.rbxm (3.2 KB)

1 Like

So, it’s there another way to make this? I’m trying to make a vent light from FNAF

You could insert a surface gui with a button in it. It has a MouseDown function, Hope this helps.

1 Like