Hats block headshots (Even with IgnoreList)

Hello, I have a problem where my gun cannot hit headshots when hats are applied. I have added hats to IgnoreList, as well as done Mouse.TargetFilter on them, but it still blocks.

Here is a script to work the IgnoreList in StarterPlayerScripts:

function X(Descendant)
	local MParts = {"Head";"Torso";"Left Arm";"Right Arm";"Left Leg";"Right Leg"}
	local Z = false
	if Descendant:IsA("BasePart") then
		for _,v in pairs(MParts) do
			if Descendant.Name == v then
				Z = true
				break
			end
		end
		if Z == false then
			table.insert(IgnoreList,Descendant)
			Mouse.TargetFilter = Descendant
		end
	end
	_G.IgnoreList = IgnoreList
end
CharactersFolder.DescendantAdded:Connect(function(Descendant)
	X(Descendant)
end)

for _,v in pairs(CharactersFolder:GetDescendants()) do
	if v:IsA("BasePart") then
		X(v)
	end
end

This all works, and bullets go through the head, but for some reason only when I remove the hats it works.

This picks up the raycast

function CastRay(StartPos,Direction,Length)
	local Hit,EndPos = workspace:FindPartOnRayWithIgnoreList(Ray.new(StartPos,Direction * Length),_G.IgnoreList)
	if Hit then
		print(Hit)
		if (not Tool.Parent or Hit:IsDescendantOf(Tool.Parent)) or Hit.Transparency > 0.9 then
			return CastRay(EndPos + (Direction * 0.01),Direction,Length - ((StartPos - EndPos).magnitude))
		end
	end
	return EndPos
end

If anyone knows why this is it would be appreciated thank you.

3 Likes

Maybe not the best solution but if u can pick up the hat then you could make the fun destroy it? would be pretty cool to see visually too. else you could stop the bullets from terminating until they hit a head object or expire?

Headgear are accesories, so when you ask it to ignore parts it doesn’t ignore headgear due to it not being a “BasePart”.

You could fix this by adding at the end of the X function a recurse

local function X(Descendant)
	-- The stuff you are already doing.
	for _, child in pairs(Descendant) do
		X(child)
	end
end

This will then loop over everything inside a model (x) also removing the need of

for _,v in pairs(CharactersFolder:GetDescendants()) do
	if v:IsA("BasePart") then
		X(v)
	end
end

allowing

X(CharactersFolder)

– Edit: Didn’t notice this was with descendants and not children. And I’m totally stumped on this issue.

Would be cool, but only if everyone had the same headgear otherwise people would have advantages (you can wear so many accessories)

But if it gets the descendants, then it get the Handle of the accessory too so what difference would this make?

Maybe custom avatars then (lots of gun games follow this). Personally I dont tend to work with gears however I would recommend seeing how roblox made guns cope with the issue and modifying your scripts to that

There must be some way to fix it, otherwise that might be why phantom forces only has people with no hats

I swear i have the same problem,you can create a table for all Accessories
and if bullet touches the accessories you still take damage

Include the Accessories to the MPARTS

sort of, otherway round for me. Bottom line they cause issues with raycasting.

Maybe because Accessories of the players are most likely /CanCollide = false

If you parented them to currentcamera would it make a difference?

RayCasting only works with CanCollide = true objects just like making a OnTouched Door it wont work unless you have it on CanCollide = true

i wouldn’t think so,it will still seem the same

As he said,well you can ignore them

Why not make it so if the object hit’s Parent is an Accessory, then you can set it it to a different parent.
It seems like a pretty easy fix to me. Obviously, modify it to match with your game

Here is an example to get the idea:

obj.Touched:Connect(function(hit)
   local playerHit

   if hit.Parent:FindFirstChild('Humanoid') then
      playerHit = hit.Parent
   elseif hit.Parent:IsA('Accessory')
      playerHit = hit.Parent.Parent
   end

    playerHit.Humanoid:TakeDamage() -- blah blah blah or whatever you use to make them take damage
end)
1 Like

It is hitting via Raycasting. The problem is it doesnt seem to want to hit anything at all, bullets go through. And what you are saying is basically so accessories add to the characters hitbox, that makes the game unfair.

Could it be that the Handle part of the headgear makes the Ray go “This is inside the blacklist, ignore!” and ignores the fact that a head is inside of it?

I’m not sure but if you could do some experimental coding you could try to use whitelists instead to see if that fixes things.

tested with lazer gun from tutorial on wiki, so far it successfully ignores all accessories.
all the objects and their descendants are ignored

local IgnoreList = {}

function scanHats(char)
	for _,v in pairs(char:GetChildren()) do
		if v:IsA("Accessory") then
			table.insert(IgnoreList,v)
		end
	end
end

for _,v in pairs(game.Players:GetChildren()) do
	v.CharacterAdded:Connect(function(char)
		--wait(1) -- this will not be needed on serverside as I was testing the code on client which obviously has some latency before all accessories appear
		scanHats(char)
	end)
	if v.Character then
		pcall(function() scanHats(v.Character) end)
	end
end
1 Like

Sorry I wasn’t aware that it wasn’t working through Raycasting.

You have two methods of fixing this:

  1. Using what other people suggested, you should just add a table of accessories to the ignore list. If you find that this is an interference, which it shouldn’t be, you can just create a table of accessories when the player joins (in a server script) and access it through a bindable event (alternatively use a module) and just update it when they die to account for an update to their accessories.

  2. FindPartOnRayWithWhitelist where you can set a whitelist to the player’s head or other body part and it should work well with this.