How do I stop Touch events from detecting accessories

Hello! I have been having this issue where a simple take damage touch event keeps registering every single part of a character. This includes everything from body parts and accessories.

The code is as follows:

local debounce = false

script.Parent.Touched:Connect(function(hit)
	local h = hit.Parent.Humanoid
	if h ~= nil and debounce == true then
		h:TakeDamage(20)
		debounce = true
		wait(1.6)
		debounce = false
	end
end)

and this is the error it gives

  19:23:00.458  Humanoid is not a valid member of Accessory "Workspace.omotashi.Dragon Empress  Skirt"  -  Server - Script:4
  19:23:00.458  Stack Begin  -  Studio
  19:23:00.458  Script 'Workspace.Tutorial.Level 3.DamBrick.Script', Line 4  -  Studio - Script:4
  19:23:00.458  Stack End  -  Studio

Any help would be great!

Use hit.Parent:FindFirstChild(“Humanoid”) instead then check if it exists

1 Like

in place of where it says hit.Parent.Humanoid?

Yup! Im pretty sure findfirstchild doesn’t output an error if it’s nil

It worked! Thank you so much!!

1 Like

You’re correct, it doesn’t, it returns either a reference to the specified instance (if it exists/can be located) or nil (if the specified instance doesn’'t exist/can not be located).

1 Like

You could also use this, I use this all the time

local function HasAHumanoid(part)
	local newparent = part
	repeat wait()
		if newparent:FindFirstChild("Humanoid") then
			return newparent:FindFirstChild("Humanoid")
		elseif newparent.Name == "Workspace" then
			return false
		end
		newparent = newparent.Parent
	until false
end