How do I use collisiongroups properly for raycasting?

I’m trying to make it so that this raycasting gun can shoot through certain parts.
I wasn’t sure how to do this, so I made a post about it. When I made the post I got suggested that I use collisiongroups. I asked how to do this, but they didn’t respond because I took too long and 6 days had already passed. I just need this question answered.

How do I use a collisiongroup for raycasting?

This is my script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("GunEvents").Prototype_Gun
local physicsService = game:GetService("PhysicsService")
physicsService:CreateCollisionGroup("blacklistedParts")
physicsService:CreateCollisionGroup("raycastGroup")
physicsService:SetPartCollisionGroup(workspace.TestGlass, "blacklistedParts")
physicsService:CollisionGroupSetCollidable("raycastGroup", "blacklistedParts", false)

local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
RayParams.IgnoreWater = true
RayParams.CollisionGroup = "raycastGroup"

event.OnServerEvent:Connect(function(Player, GunPos, MouseP, Handle)
	Handle.Fire:Play()
	
	local RayCast = workspace:Raycast(GunPos, MouseP*1000, RayParams)
	
	if RayCast then
		local hum = RayCast.Instance.Parent:FindFirstChild("Humanoid") or RayCast.Instance.Parent.Parent:FindFirstChild("Humanoid")
		if hum then
			hum:TakeDamage(25)
		end		
		
		local bullet = Instance.new("Part")
		bullet.CanCollide = false
		bullet.Anchored = true
		bullet.BrickColor = BrickColor.new("Cool yellow")
		bullet.Material = Enum.Material.Neon

		local Dist = (GunPos-RayCast.Position).magnitude
		bullet.CFrame = CFrame.new(GunPos, RayCast.Position)*CFrame.new(0,0,-Dist/2)
		bullet.Size = Vector3.new(0.1, 0.1, Dist)
		game.Debris:AddItem(bullet, 0.5)
	end
	
end)
4 Likes

The developer website is really helpful for things you don’t understand, I recommend searching on it before posting a topic, here is everything about Collision Filtering and Collision Grouping: Collision Filtering | Documentation - Roblox Creator Hub

Did you found how?

I have no clue even with the website.

The way I used it wrong.

castParams.CollisionGroup = game.workspace.SoundRegion.SoundRegion1