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

How can I make the mouse recognize when I click a character's body part?

Asked by 7 years ago

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?

2 answers

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago

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
0
Thanks! Just what I needed. alonzo12345 60 — 7y
Ad
Log in to vote
0
Answered by
Nic_Wow -5
7 years ago

Click Select

Answer this question