How to make a raycast not go through parts

I’m trying to make a gun using raycasts. The problem with this is that the raycast travels through parts. I’m unable to use FilterDescendantsInstances because there are just too many parts in my game to add them all to the table. Anyone got any solutions?

Code:

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local raycastResult = workspace:Raycast(script.Parent.Handle.Muzzle.Position, (mousePos - script.Parent.Handle.Muzzle.Position).Unit * 300, raycastParams)

This is really annoying, someone please help me

Did you set RaycastParams.FilterType to Enum.RaycastFilterType.Blacklist?

Yeah. Still goes through parts.

I am not sure if this is a thing or not, but are do those parts have CanCollide == false? Cause maybe the raycast passes through them, because they cannot collide.

That’s odd. Can we see the code that calls the :Raycast() function?

local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local raycastResult = workspace:Raycast(script.Parent.Handle.Muzzle.Position, (mousePos - script.Parent.Handle.Muzzle.Position) * 300, raycastParams)

I believe it has to do with the * 300

1 Like

Aren’t you are supposed to like take the unit vector of (mousePos - script.Parent.Handle.Muzzle.Position) then multiply by 300?

1 Like

I don’t know, that’s why I’m asking

All I want it to do is not go through parts.

So I did some experiments and the code worked fine for me when I took the unit vector and multiplied by 300.

local raycastResult = workspace:Raycast(script.Parent.Handle.Muzzle.Position, (mousePos - script.Parent.Handle.Muzzle.Position).Unit * 300, raycastParams)

Tried it, it still goes through parts.

But… It still goes through parts?