Raycast not ignoring bullets

So I need the rays to ignore the bullets, but there is an issue where the raycast is not ignoring the bullets being made. The solutions I tried so far are Mouse.TargetFilter, and RayCastParams.FilterDescendantsInstances.

After some testing I realized that the raycast does not ignore the instance if there are other instances with the same name. How can I prevent that?

Here is the part of the script I am trying to solve.

local bullet = ServerStorage.Things.Bullet:Clone()

local rayCastParams = RaycastParams.new()


rayCastParams.FilterDescendantsInstances = {player.Character, bullet, shoot_part, tool.BodyAttach, nil, tool.Parts["Colt _Python"]}
rayCastParams.FilterType = Enum.RaycastFilterType.Blacklist

local origin = shoot_part.Position
local direction = (position - origin).Unit*300
local result = Workspace:Raycast(origin, direction, rayCastParams)

local intersection = result and result.Position or origin + direction
local distance = (origin - intersection).Magnitude

local x = math.random(-spread*100,spread*100)/100
local y = math.random(-spread*100,spread*100)/100
		
		print(x,y)
		
        bullet.Name = "Bullet"
		bullet.Size = Vector3.new(0.1, 0.1, distance)
		bullet.CFrame = CFrame.lookAt(origin, intersection)*CFrame.new(0, 0, -distance/2)*CFrame.Angles(math.rad(x),math.rad(y),0)
		
		
		bullet.Parent = game.Workspace.Bullets

Heres just an idea. You said that it doesn’t work if there are other bullets with the same name. Maybe give the bullet a random name like this?

local bullet = ServerStorage.Things.Bullet:Clone()
local randomNumber = math.random(1,1000)
bullet.Name = "Bullet"..randomNumber

Oh, I was trying to do that but I wasn’t sure if it was possible. I’ll try it out.

1 Like

Its still happening.

image

Below this line

Could you add this so we can see what the bullet hits.

bullet.Touched:Connect(function(hit)
       print(hit.Name)
end
1 Like

Its printing the bullet’s name.

image

How can a bullet touch itself?? Did you fire two bullets and did they touch each other?

I mean it printed the bullet it touched.

So was it’s path ok until it touched the bullet? Did it touch the bullet immediately?

Yes, the path was okay until it touched the bullet. No, it didn’t touch the bullet immediately.

Maybe instead of parenting the bullets to the workspace, parent the bullets to a folder named “Bullets” that is in the workspace. Then one of the blacklists parameters could be:

local folder = game.Workspace.Bullets
rayCastParams.FilterDescendantsInstances = {player.Character, folder:GetChildren(), shoot_part, tool.BodyAttach, nil, tool.Parts["Colt _Python"]}

When I do that I get this error.

image

If you can’t find a solution well don’t stress yourself. You can stop replying if there is no solution. Anyways thanks for the help though!

There is a solution just I got go for now. I promise you I will get back to you as soon as I’m free

1 Like

I would try adding the bullets to a folder and ignoring all descendants in that folder. FilterDescendantsInstances table will only ignore all descendants of the objects in the table, and not the instance itself, this is why your current code isn’t working.

2 Likes

Oh, thank you for informing me. It now ignores the bullets. XdJackyboiiXd21 solution almost worked but the only thing that didn’t make it work is the :GetChildren(). Thank you for your efforts guys.

1 Like