How to add the players character to this certain ignore list?

I am making a module script to handle bullets being shot. It handles the tracers, hit registration, etc.

I want hats to be ignored by the gun. And also ignore the player shooting the bullet.
But I cant add the character to the ignore list without it making the player invicible.

I am out of ideas of how to fix this at this point.

If you can help me that would be cool thx.

local module = {}

--[[=Variables=]]--
local Debris = game:GetService("Debris")
local ig = {workspace.Ignore}



local playSound = function(name,part,shift)
   local Sound = script.Sounds:FindFirstChild(name):Clone()
   Sound.Parent=part
   Sound:Play()
   Debris:AddItem(Sound,3)
end

local makeEffects = function(stat,mpos,hole,part,normal)
   local Beam = stat.Beam or script.Beam:Clone()
   
   Beam.Parent=hole
   
   local AttPart = Instance.new("Part",workspace.Ignore)
   AttPart.Size=Vector3.new(1,1,0.05)
   AttPart.Position=mpos
   AttPart.CFrame=CFrame.new(AttPart.Position, AttPart.Position+normal)
   AttPart.Transparency=1
   AttPart.CanCollide=false
   script.BulletHole:Clone().Parent=AttPart
   
   local Att1 = Instance.new("Attachment", AttPart)
   Att1.WorldPosition=mpos
   local Att0 = Instance.new("Attachment", hole)
   
   local Weld = Instance.new("WeldConstraint", AttPart)
   Weld.Part0=part
   Weld.Part1=AttPart
   
   Beam.Attachment0=Att0
   Beam.Attachment1=Att1
   Debris:AddItem(AttPart, 2)
   Debris:AddItem(Att1, 2)
   Debris:AddItem(Weld, 2)
   Debris:AddItem(Att0, 0.2)
   Debris:AddItem(Beam, 0.2)
end

module.raycast = function(stat,origin,mpos)
   
   local rayParams = RaycastParams.new()
   rayParams.FilterType = Enum.RaycastFilterType.Blacklist
   rayParams.FilterDescendantsInstances = ig
   rayParams.IgnoreWater = true
   
   local rayResult = workspace:Raycast(origin.Position, (mpos-origin.Position).unit*stat.Range, rayParams)
   
   if rayResult then
   	if not rayResult.Instance.Parent:IsA("Accessory") then -- Make sure its not a hat.
   		
   		local hitPart = rayResult.Instance
   		local humanoid = hitPart.Parent:FindFirstChild("Humanoid")

   		makeEffects(stat,mpos,stat.hole,hitPart,rayResult.Normal)

   		if humanoid then
   			playSound("Human",hitPart)

   			local Blood = script.Blood:Clone()
   			Blood.Parent=hitPart
   			Blood.Enabled=true
   			Blood.Script.Disabled=false
   			game:GetService("Debris"):AddItem(Blood,1)

   			if hitPart.Name=="Head" then
   				humanoid:TakeDamage(stat.HeadDamage)
   			else
   				humanoid:TakeDamage(stat.Damage)
   			end
   		end
   		
   	else
   		table.insert(ig,rayResult.Instance)
   		module.raycast(stat,origin,mpos) -- Redo raycast.
   	end
   end
   
   stat=nil
   origin=nil
   mpos=nil
   rayParams=nil
   rayResult=nil
end

return module

Just build a new ignore table whenever you do the raycast

rayParams.FilterDescendantsInstances = {workspace.Ignore, character}
1 Like

But how would I ignore hats?

Here in this code it does that

table.insert(ig,rayResult.Instance)
module.raycast(stat,origin,mpos) -- Redo raycast.

Careful with that, you’re changing the ig table for all future raycasts too, even unrelated ones.

The easiest way would probably be to add all hats to their own collision group that doesn’t collide with anything else.

Or loop through everyone’s hats and add them to the table when you raycast.

Or keep track of all the current hats when players respawn/die and add them to the table.

You may also be interested in the FastCast module, which handles a lot of this. You can write a penetration check method to see if an object is a hat or not.

I got it to work on my own by enclosing the ig variable and the raycast function inside of another function.

Thanks though.

1 Like