Hi all, I am looking at click events and I found two techniques: ClickDetector and MouseHit. My understanding is that click detector you connect a function to it, and mouse hit you need to have an infinite loop constantly collect mouse points. In this case, I prefer using ClickDetector! But visually it looks outadated like something from the 90s, this makes me think it's soon to be deprecated. In this circumstance I am not sure what is the good approach for handling mouse-related events. What is the correct way to handle click events?
It's really based on what you prefer.
If you want to go for the simplier way, you can use ClickDetector
. It's FE compatible and easy to use.
clickdetector.MouseClick:Connect(function(plr) --stuff end)
However ClickDetectors are pretty old, so some people prefer to use the Mouse
object in combination with RemoteEvents
.
Note that this does not require any infinite loops. It's as simple as this:
local plr = game:GetService"Players".LocalPlayer local mouse = plr"GetMouse" local remote = game:GetService"ReplicatedStorage".SomeRemote mouse.Button1Down:Connect(function() remote:FireServer(mouse.Target) --let the server do the rest end)
ClickDetector isn't going to be deprecated anytime soon as far as I know. Yes, it may have an old looking icon, but it's still the best way to detect a click. Connecting to an event uses a lot less performance compared to constantly checking for a click.