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)
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.
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.
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 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)
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.
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)