(Raycasting) Headshoots doesnt count when players have accessories (like hats and masks)

When I try to hit on head it doesnt make damage if the player have one mask or something like that.

local range = 400
local NormalDamage = 50
local HeadDamage = 1000

local NewRay = RaycastParams.new()
	local RayDirection = (TargetLocation - BulletLocation.Position) * range 
	
	NewRay.FilterDescendantsInstances = {Player.Character}
	
	local Result = game.Workspace:Raycast(BulletLocation.Position, RayDirection, NewRay)
	local humanoid =  Result.Instance.Parent:FindFirstChild("Humanoid")
		
	if Result then
		if Result.Instance then
			
			if not humanoid then
				return
			end
			if  Result.Instance.Name == "Head" then
				humanoid.Health -= HeadDamage
			elseif humanoid then
				humanoid.Health -= NormalDamage
				
			end
		end
	end
end)

How I can do something for ignore accessories?

Use this server script to make hats nonhittable at all through the use of collision groups, tested in studio it should work.

Stole the code from Player | Roblox Creator Documentation to get all the characters hats and turn them NonHittable.

The raycast should now pass right through all accessories and then hit the head.

local PhysicsService = game:GetService("PhysicsService")

PhysicsService:CreateCollisionGroup("NonHittable")
PhysicsService:CollisionGroupSetCollidable("NonHittable", "Default", false)
PhysicsService:CollisionGroupSetCollidable("NonHittable", "NonHittable", false)

local Players = game:GetService("Players")
 
local function playerAdded(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
 
		-- Make hats not be able to hit with raycasts at all
		for _, accessory: Accessory in pairs(humanoid:GetAccessories()) do 
            local basePart = accessory:FindFirstChildWhichIsA("BasePart")
            if basePart then
                PhysicsService:SetPartCollisionGroup(basePart,"NonHittable")
            end
		end
 
	end)
end
 
-- get existing players
for _, player in pairs(Players:GetPlayers()) do 
	playerAdded(player)
end
-- listen for new players
Players.PlayerAdded:Connect(playerAdded)
10 Likes