Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

The correct way to handle click events?

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

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)

2 comments

0

Although stuff such as custom icon on hover and doing actions on hover would probably require a loop

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?

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.

6 comments

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?

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".

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.

0

You would need a loop to always know where the mouse is and detecting when it's been clicked.

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

1

You wouldn't need a loop. You can connect to Button1Down event and then fire a RemoteEvent with the mouse.Hit

This discussion is preserved as it was. New answers, comments, and votes are closed.