Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

The correct way to handle click events?

Asked by
Ribasu 127
6 years ago

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?

2 answers

Log in to vote
1
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
6 years ago

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)
0
Although stuff such as custom icon on hover and doing actions on hover would probably require a loop Amiaa16 3227 — 6y
0
If I have a dynamic environment, i.e. at one time I will have 100 objects, later 105, later 80, that are clickable, how can I achieve this? I was thinking I could connect, and disconnect, multiple functions as the need arises? Ribasu 127 — 6y
Ad
Log in to vote
0
Answered by
Dog2puppy 168
6 years ago

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.

0
Yes!!! That's my argument, I'm surprised that this is the state of matters. Can you make the clickdetector icon invisible or something? Ribasu 127 — 6y
0
The ClickDetector class has a CursorIcon property. You could change that to something more modern, or even just change it to the black "Roblox mouse". Dog2puppy 168 — 6y
2
What are you guys talking about? Mouse.Hit doesn't run any infinite loop. It calculates the world CFrame from the screen coordinates of the mouse then returns that CFrame. It's much better than using a click detector. ScriptGuider 5640 — 6y
0
You would need a loop to always know where the mouse is and detecting when it's been clicked. Dog2puppy 168 — 6y
View all comments (2 more)
0
@ScriptGuider as @Dog2puppy has suggested, you would need a loop constantly checking the mouse position AND you would need to define some form of table/ a lot of if statements  (if X was clicked, then do Y). I think this can get ugly in terms of code and will be much poorer in performance compared to a click detector. But a click detector looks old and heard it is outdated; the point of this quest Ribasu 127 — 6y
1
You wouldn't need a loop. You can connect to Button1Down event and then fire a RemoteEvent with the mouse.Hit Amiaa16 3227 — 6y

Answer this question