I want to make a tool that lowers another player's health while I'm holding the mouse down. I can do that part but how can I make it where the script gets the part clicked?
✓ ANSWER ACCEPTED
How can I make the mouse recognize when I click a character's body part?
2 answers
✓ Accepted answer
The Mouse object has a Property called Target. This resembles the object your curser is on.
Get the Mouse's Target in a Button1Down event - you can use an if statement to check if the Target's Parent has a Humanoid object, and decrease health.
######(The mouse object can only be accessed from LocalScripts - put a LocalScript in any bin that replicates to the client; e.g. StarterPack, StarterGui, StarterPlayerScripts)
local plr = game.Players.LocalPlayer; --Player
local mouse = plr:GetMouse(); --Player's mouse
function LeftClick()
local targ = mouse.Target; --This is what the mouse is on
if targ then --If the mouse is actually on something
--Search for a Humanoid
local potentialHum = targ.Parent:FindFirstChild("Humanoid");
if potentialHum then --If there's a Humanoid
print("Damage da enemy >:3");
end
end
end
mouse.Button1Down:Connect(LeftClick) --Button1Down event on mouse
Click Select
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Thanks! Just what I needed.