How do I find the "subject" of mouse.Target?

OKAY OKAY OKAY, let me explain this in detail. In this script i am Attempting to either delete a part, or write the message “the target was human” if the “target” has the humanoid child in them. However… I am having an EXTREMELY hard time figuring out how to find if the part has a humanoid inside of it. The most recent error being :

“Players.snipperdiaper.Backpack.Tool.LocalScript:8: attempt to index nil with ‘FindFirstChild’”

which I cant figure out how to fix. So any help is appreciated!

local player = game.Players.LocalPlayer
local tool = script.Parent
local mouse = player:GetMouse()

	
tool.Activated:Connect(function()
	local target = mouse.Target
	local HumanoidFactor = target:FindFirstChild("Humanoid")
	
	if target ~= HumanoidFactor then
		
		target:Destroy()

	elseif target == HumanoidFactor then
		
		print("The Target was human")
	end

		
end)
	

Thanks in advance, snipperdiaper

1 Like

Mouse.Target only returns a part if it hits a part. If you hit nothing (such as clicking in the sky) then it will return nothing.

The error you were given, “Players.snipperdiaper.Backpack.Tool.LocalScript:8: attempt to index nil with ‘FindFirstChild'", just means that mouse.Target returned nil, meaning that you can’t find it’s children because it doesn’t exist.

Make sure you check that Mouse.Target actually exists by doing something like the following:

if not target then return end
-- do stuff

--------------- OR -------------
if target ~= nil then
    -- do stuff
end

If you’re trying to find if the Mouse.Target model has a humanoid you need to do:

local Humanoid = mouse.Target:FindFirstAncestorWhichIsA("Model"):FindFirstChild("Humanoid")
if Humanoid then
    -- Maybe take some damage? Humanoid:TakeDamage(damage)
end

You could also use @OriChanRBLX’s method of using a Raycast but I personally find Mouse.Target to be easier.

Using a quick google search I found this, which may also be of assistance to you: https://scriptinghelpers.org/questions/70617/how-to-get-the-object-mouse-hits-in-workspace

1 Like

You could also do that but you’d need to get the Target parent’s model first (assuming the humanoid models use the basic structure like default ROBLOX characters or dummies.
image

Then I believe you wrote your code wrong as you did “FindFirstChildWhichIsA(“Model”)” not Ancestor.
(If this is what you were trying to do to get the character model. I should have corrected it however to actually go back to the model still as well since it is a part not the model as the target.)

mouse.Target returns the instance (i think. it could also just be the BasePart. havent tested it). For example. if I click on the right arm of this dummy, it’ll return the RightArm because that’s what I’m clicking on. From this I would need to find the parent model image through :FindFirstAncestorWhichIsA("Model") and then I can find the actual humanoid using :FindFirstChild(), :FindFirstChildOfClass(), etc.

You can test this using the following short code

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Down:Connect(function()
	print(Mouse.Target)
end)

1 Like

I know how it works. Re-read your code you posted above.


I know I’ve only been doing this since 2011, but…

2 Likes

Ahhh, my bad mate. It’s those little things that get mixed up. Fixed it up in the post. Cheers :+1:

2 Likes

I get that one, I’ve done that too many times. Especially with the new Luau autocomplete, it suggests FindFirstAncestor() before FindFirstChild() and I’m sitting there wondering why my code isn’t working.

2 Likes

Had to add my script to this one but i worked!!

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local tool = script.Parent


tool.Activated:Connect(function()
	
	wait(0.1)
	local MouseTargetParent =  Mouse.Target.Parent
	print(MouseTargetParent)

	if MouseTargetParent.Name == "Humanoid" then




		print("weeeeeeeooeoeoeoe")



	else do
			local MouseTargetDeletionBrick = Mouse.Target
			wait(0.2)
			MouseTargetDeletionBrick.Transparency = 0.2
			wait(0.2)
			MouseTargetDeletionBrick.Transparency = 0.4
			wait(0.2)
			MouseTargetDeletionBrick.Transparency = 0.6
			wait(0.2)
			MouseTargetDeletionBrick.Transparency = 0.8
			wait(0.3)
			MouseTargetDeletionBrick:Destroy()
		end
	end

end)
	
1 Like

Fun fact, you don’t need the do block there after the else unless you really want it there.

Your code would work just as

--..
else
  local MouseTargetDeletionBrick = Mouse.Target
  --..
end
1 Like