Hit not registering when player wears an accessory

I’m working on a weapons game for ROBLOX. For some reason, a hit does not register if a player is wearing an accessory. Below is the code for hit detection in my weapon. Any help is appreciated.

 local ray = workspace:Raycast(tool.Handle.Muzzle.CFrame.Position, (mouseTarget.Position - tool.Handle.Muzzle.CFrame.Position) * 300)
	if ray ~= nil then
		if ray.Instance.Parent:FindFirstChildOfClass("Humanoid") ~= nil then
			if ray.Instance.Name == "Head" then
				ray.Instance.Parent.Humanoid.Health -= (80 + math.random(-10, 10))
			elseif ray.Instance.Name == "Torso" or ray.Instance.Name == "HumanoidRootPart" then
				ray.Instance.Parent.Humanoid.Health -= (30 + math.random(-10, 10))
			elseif ray.Instance.Parent:IsA("Accessory") then
				ray.Instance.Parent.Humanoid.Health -= (50 + math.random(-10, 10))
			else
				ray.Instance.Parent.Humanoid.Health -= (10 + math.random(-10, 10))
			end
		end
		if ray.Instance.Name == "BreakableGlass" then
			ray.Instance.Transparency = 1
			ray.Instance.CanTouch = false
			ray.Instance.CanCollide = false
			ray.Instance.CanQuery = false
			local sound = Instance.new("Sound")
			sound.Parent = ray.Instance
			sound.SoundId = "rbxasset://sounds/glassbreak.wav"
			sound:Play()
			for _, v in pairs(ray.Instance.BrokenGlass:GetChildren()) do
				v.Transparency = 0.9
				v.Anchored = false
				v.CanTouch = true
				v.CanQuery = true
				v.CanCollide = true
			end
			sound.Stopped:Connect(function()
				sound:Destroy()
			end)
		end
	end

Let me know if you have any questions or statements about my topic.
Thanks,
-poker_man29

local tool = -- define tool
tool.Activated:Connect(function()
    local mouseTarget = game.Players.LocalPlayer:GetMouse()

    local ray = workspace:Raycast(tool.Handle.Muzzle.CFrame.Position, (mouseTarget.Position - tool.Handle.Muzzle.CFrame.Position) * 300)
    if ray ~= nil then
        local hitInstance = ray.Instance

        -- Check if the hitInstance or its ancestor is an accessory
        local isHitInstanceAccessory = false
        local currentObject = hitInstance
        while currentObject do
            if currentObject:IsA("Accessory") then
                isHitInstanceAccessory = true
                break
            end
            currentObject = currentObject.Parent
        end

        if hitInstance.Parent:FindFirstChildOfClass("Humanoid") and not isHitInstanceAccessory then
            if hitInstance.Name == "Head" then
                hitInstance.Parent.Humanoid.Health -= (80 + math.random(-10, 10))
            elseif hitInstance.Name == "Torso" or hitInstance.Name == "HumanoidRootPart" then
                hitInstance.Parent.Humanoid.Health -= (30 + math.random(-10, 10))
            else
                hitInstance.Parent.Humanoid.Health -= (10 + math.random(-10, 10))
            end
        end

        -- Rest of your code for BreakableGlass
        if hitInstance.Name == "BreakableGlass" then
            hitInstance.Transparency = 1
            hitInstance.CanTouch = false
            hitInstance.CanCollide = false
            hitInstance.CanQuery = false
            local sound = Instance.new("Sound")
            sound.Parent = hitInstance
            sound.SoundId = "rbxasset://sounds/glassbreak.wav"
            sound:Play()
            for _, v in pairs(hitInstance.BrokenGlass:GetChildren()) do
                v.Transparency = 0.9
                v.Anchored = false
                v.CanTouch = true
                v.CanQuery = true
                v.CanCollide = true
            end
            sound.Stopped:Connect(function()
                sound:Destroy()
            end)
        end
    end
end)```

After doing some testing, I am sorry to say, but this code actually does not work. I apologize for saying that it worked in my previous (now deleted) post.

bumping because nobody responded

check whether the hit instance is a descendant of character

local Character = Ray.Instance:FindFirstAncestorOfClass("Model")
-- even if it found a model instance we are not sure if it is a character unless it has a humanoid
-- using and operator to avoid using if statement
-- if character is nil then skip else proceed to check whether it has humanoid 
local Humanoid = Character and Character:FindFirstChild("Humanoid")

forgot to close thread, solved my own issue

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.