How do I find out what brick a player clicks on?

Hello! I’m creating a Farming game and my problem is figuring out how to create tools, I’ve been trying to figure out how to detect if the player is clicking on a tree, I’ve searched pretty much all day but haven’t found a way.

1 Like

Try using mouse.Target and UserInputService

local mouse = game.Players.LocalPlayer:GetMouse()

local UIS = game:GetService('UserInputService')

UIS.InputBegan:Connect(function(key)
    if key.UserInputType == Enum.UserInputType.MouseButton1 then
        local target = mouse.Target -- returns the part their mouse is on.
        print(target.Name) -- prints the part's name
    end
end
1 Like

Thanks! I’ll try this. :slight_smile:

1 Like

So I have this, for some reason it doesn’t detect when the tool is equipped? Do you know what’s wrong with this? (The local script that contains this is inside of a tool)


local UIS = game:GetService('UserInputService')
local Tool = script.Parent
UIS.InputBegan:Connect(function(key)
	if key.UserInputType == Enum.UserInputType.MouseButton1 then
		if Tool.Equipped == true then
			local target = mouse.Target -- returns the part their mouse is on.
			print(target.Name) -- prints the part's name
			
		end
		print("Unable to use tool because its unequipped.")
	end
end)```
2 Likes

Try putting it into StarterCharacterScripts.

I’m assuming the same issue happens with a Click Detector if you have a tool equipped… that is it doesn’t work.

Try using Tool.Activated and Mouse.Target. Here is a localscript I wrote that you can put inside of the tool.

local Tool = script.Parent
local lp = game.Players.LocalPlayer
local mouse = lp:GetMouse()

Tool.Activated:Connect(function()
	print(mouse.Target)
end) 
5 Likes

This worked! Thanks a lot. :slight_smile:

use the .Equipped as a function:

Tool.Equipped:Connect(function()

end)

its better to use conditional statements to check if the tool is parented to the player’s backpack or player. The tool is parented to the player when it is equipped but it is better to use the .Equipped function because it will run the code whenever the tool is equipped.

1 Like