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 remove this "Humanoid is not a valid member of Hat" error? [Solved]

I have basically tried doing

elseif Hit:IsA("Hat") then 
end

then the error keep coming up. This error doesn't effect anything but I really want to remove it from happening. My script doesn't break when a hat touches it and this is a Script inside of a Part Parented to Workspace! Here's the Script I made:

UniTop = "rbxassetid://227007415"
UniBottom = "rbxassetid://227007712"


script.Parent.Touched:connect(function(Hit)
	if Hit.Parent.Humanoid then
		local FindS = Hit.Parent:FindFirstChild("Shirt")
		if FindS and FindS.ShirtTemplate ~= UniTop then
			Hit.Parent.Shirt.ShirtTemplate = UniTop
		elseif not FindS then
			local S = Instance.new("Shirt", Hit.Parent)
			S.ShirtTemplate = UniTop		
		end		
		local FindP = Hit.Parent:FindFirstChild("Pants")
		if FindP and FindP.PantsTemplate ~= UniBottom then
			FindP.PantsTemplate = UniBottom
		elseif not FindP then
			local P = Instance.new("Pants",Hit.Parent)
			P.PantsTemplate = UniBottom
		end		
	end		
end)

I can give more information if needed, Just comment Bellow

2 answers

✓ Accepted answer

When checking for stuff, always use :FindFIrstChild(), otherwise the script will error if that thing you are looking for isn't there. Fixed code below:

UniTop = "rbxassetid://227007415"
UniBottom = "rbxassetid://227007712"

script.Parent.Touched:connect(function(Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then
        local FindS = Hit.Parent:FindFirstChild("Shirt")
        if FindS and FindS.ShirtTemplate ~= UniTop then
            Hit.Parent.Shirt.ShirtTemplate = UniTop
        elseif not FindS then
            local S = Instance.new("Shirt", Hit.Parent)
            S.ShirtTemplate = UniTop        
        end     
        local FindP = Hit.Parent:FindFirstChild("Pants")
        if FindP and FindP.PantsTemplate ~= UniBottom then
            FindP.PantsTemplate = UniBottom
        elseif not FindP then
            local P = Instance.new("Pants",Hit.Parent)
            P.PantsTemplate = UniBottom
        end     
    end     
end)

To my understanding, hats do not trigger the onTouched event. Try checking if the parent of hit has the name "Hat" instead.

Instead of

elseif Hit:IsA("Hat") then
end

do this

elseif Hit.Parent:IsA("Hat") then
end

Why? Well if you examine the children of a hat, you notice that it has a child named "Handle". The onTouch event is run when it touches this handle but it's name is not "Hat" so it does nothing.

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