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

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

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

✓ 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

1 comment

0

Thanks! Just what I needed.

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