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

Simple way to detect left clicks on objects?

Asked by 8 years ago

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

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

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.

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? ZoltofLightning 27 — 8y
0
Uhh, what do I put that in? Wherever I put it, the script doesn't work ZoltofLightning 27 — 8y
Ad

Answer this question