The click detector basically does the job detecting various stuff related to your cursor or mouse.
It can detect and do a lot of stuff related to your cursor. Now, we're going to go over the 4 different events of the Click Detetor. You can add a Click detector and execute these events :
MouseClick
MouseHoverEnter
MouseHoverLeave
RightMouseClick
Note : It's nessasary to put your ClickDetector as a child of the selected part, like this :
You can make a lot of stuff using the click detectors. You can make buttons, doors, pickable items, interactable pets, etc.
The MouseClick event is self-explanitory, the event triggers when a player clicks on the Click Detector's Parent i.e the part in this case. You can trigger it like this :
script.Parent.MouseClick:Connect(function ()
--The code you type here will run when the part is clicked
end)
The MouseHoverEnter event triggers when your mouse hovers over the selected part. An example would be - If you wanted to change the transparency of the part when your cursor hovers over it, You can write it like this :
script.Parent.MouseHoverEnter:Connect(function ()
script.Parent.Parent.Transparency = 0.5
end)
The event MouseHoverLeave is basically the opposite of MouseHoverEnter. It triggers when a player moves their mouse off the selected part. Suppose if you wanted to change the color of the part to red when the player moves their mouse off the part, you could do :
script.Parent.MouseHoverLeave:Connect(function ()
script.Parent.Parent.BrickColor = BrickColor.new("Bright red")
end)
RightMouseClick is basically MouseClick but it triggers when the part is clicked by the right mouse button. As simple as that!
Now, you're probably wondering, what can we use these events for? Here are some examples :
1 - MouseClick and RightMouseClick
You can use MouseClick when making a simple button or a simple door. You can do the same for RightMouseClick too
2 - MouseHoverEnter and MouseHoverLeave
You can use these both for eg: When the player's mouse hovers over the part, it turns green, and when the player's mouse has left hovering the part, it turns red. The code for it would be -
script.Parent.MouseHoverEnter:Connect(function ()
script.Parent.Parent.BrickColor = BrickColor.new("Camo")
end)
script.Parent.MouseHoverLeave:Connect(function ()
script.Parent.Parent.BrickColor = BrickColor.new("Bright red")
end)
Easy and simple. We first write the events (MouseHoverEnter and MouseHoverLeave) and then we change the color of the part accordingly ; Green if the player's mouse is hovering over the part, and Red if the player's mouse has left hovering the part
These examples are for beginners only. You can write way more advanced stuff then this.
Thank you for reading and a review would be appriciated! I hope you have a good day and I wish you best of luck in your scripting journey 😊