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

Simple way to detect left clicks on objects?

What is the simplest way to detect if an object is left clicked? I'd like it to work exactly like a click detector except without it detecting right clicks. Whenever someone moves the camera on my game, the blocks act like someone clicked them.

1 answer

✓ Accepted answer

Target

You can use a property of Mouse called Target. Target represents a BasePart the mouse is currently hovering over. You can get this target by implementing it in a mouse button event. Here's an example using UserInputService:

-- Get game services
local InputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Mouse  = Player:GetMouse() -- Get the player mouse

-- Maybe make some references to the input type enums
local UserInput = Enum.UserInputType
local KeyCode   = Enum.KeyCode

InputService.InputBegan:connect(function(Input)
	local User = Input.UserInputType
	local Key  = Input.KeyCode

	if User == UserInput.MouseButton1 then
		print(Mouse.Target) -- Print the object the mouse left clicked on
	end
end)

That should print the name of any object you left-click on. Remember, Mouse.Target returns the actual object of whatever the mouse is currently hovering over. If you have any questions, just let me know.

2 comments

0

Hmm... This detects what the player is clicking on. I have click detectors in only certain parts of my game, and when they are clicked, it does things with many different values within them. How would I implement this type of thing into them? How would I make it check if it is one of those specific targets?

0

Uhh, what do I put that in? Wherever I put it, the script doesn't work

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