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
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?
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
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)
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
Sorry I wasnât aware that it wasnât working through Raycasting.
You have two methods of fixing this:
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.
FindPartOnRayWithWhitelist where you can set a whitelist to the playerâs head or other body part and it should work well with this.