How would I make a raycast only interact with the specified parts within a model?

I wish to have my raycast only interact with the predetermined model, and nothing else. How would I do this? I’ve tried this

local params = RaycastParams.new()
params.FilterDescendantsInstances = {script.Parent}
params.FilterType = Enum.RaycastFilterType.Include

Yet, my raycast is still interacting with other instances or not interacting with anything. Any help would be greatly appreciated! Below you’ll find the code responsible for creating the raycast. just to ensure I have everything written fine.

local Rock = script.Parent
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Hitbox = script.Parent:WaitForChild("Hitbox")
local replicatedStorage = game:GetService("ReplicatedStorage")
local OreTouchedEvent = replicatedStorage.OreTouchedEvent
local Character = Player.Character or Player.CharacterAdded:Wait()
local Pickaxe = Character:WaitForChild("Pickaxe")
local Handle = Pickaxe:WaitForChild("Handle")
local Count = 0

local params = RaycastParams.new()
params.FilterDescendantsInstances = {script.Parent}
params.FilterType = Enum.RaycastFilterType.Include

Hitbox.Touched:Connect(function(player)
	local function Check()
		if Handle:GetAttribute("IsActivated") == true then
			Count += 1
			local Cast = workspace:Raycast(Handle.Position, Handle.CFrame.LookVector * 2, params)
			print (Cast)
			if Cast then
				if Cast.Instance.Name ~= Rock.Name then return end
			else
				return
			end
		end
	end

	repeat task.wait(1) Check() until Count == 5
	OreTouchedEvent:FireServer(Rock)
	Count = 0
end)

It looks like you are only putting script.Parent into the raycast parameters.
You should be including all the items the raycast is allowed to hit in the FilterDescendantInstances array.
As stated in the first link I posted, this can be done by simply adding the items to a collision group and using the filter’s array to check that group.

How do you create a collision group? I haven’t heard of this before, and I’m rather new at developing things.

Just search collision or collisiongroup in the same site (create.roblox.com) that the other links I posted go to.
Go to the Documents tab to do your search.

Hm… I’ve read up on the collision group documentation, however, despite this the params aren’t working as intended…

local params = RaycastParams.new()
params.CollisionGroup = "Rock"
params.FilterType = Enum.RaycastFilterType.Include

Have you added the CollisionGroup?
Have you selected all the Parts (I don’t think you can add models) you want the raycast to hit and clicked the + button next to the collision group in the Collision Group window?
If an item is in a group you’ll see it in the Part’s CollisionGroup Property. If it doesn’t say Rock then you know it’s not added.

Yup, it has all been added

image

image

Use the Include raycastfiltertype in the ray cast properties and put the model inside of the table

Which table? The FilterDescandantaInstances table? Since I’ve done this, however, it didn’t do what I wished it to do.

what do u wish it to do? u need to change the filter type of raycast info to Include instead of Exclude and then put the model inside of the table and it will only interact with the models inside of the table

I want the raycast to only be able to interact with the parts in the model. Also, I do believe I’ve already done this
image

maybe put filter type first before filter descendants instances idk why this wouldnt work thats what its meant for

I’m rather confused as to why this isn’t working as intended… It should work, given the parameters and the way it is setup, but it doesn’t even get to the cast part. The raycast is seemingly not touching anything…

Here’s my code to possibly have more information as to why it isn’t working.

local Rock = script.Parent
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Hitbox = script.Parent:WaitForChild("Hitbox")
local replicatedStorage = game:GetService("ReplicatedStorage")
local OreTouchedEvent = replicatedStorage.OreTouchedEvent
local Character = Player.Character or Player.CharacterAdded:Wait()
local Pickaxe = Character:WaitForChild("Pickaxe")
local Handle = Pickaxe:WaitForChild("Handle")
local Count = 0

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = {script.Parent.Hitbox}

Hitbox.Touched:Connect(function(player)
	local function Check()
		if Handle:GetAttribute("IsActivated") == true then
			Count += 1
			local Cast = workspace:Raycast(Handle.Position, Handle.CFrame.LookVector * 2, params)
			print(Cast)
			if Cast then
				if Cast.Instance.Name ~= Rock.Name then return end
			else
				return
			end
		end
	end

	repeat task.wait(1) Check() until Count == 5
	OreTouchedEvent:FireServer(Rock)
	Count = 0
end)

Whenever I click, which changes an attribute within the handle of the pickaxe tool that I’m using, it doesn’t seem to be getting picked up by the hitbox getting touched for some odd reason.

image

The hitbox is invisible, just to let you know.

I still do wish to receive assistance, as I’m confused on what to do.

Also, this isn’t the case with the ores created in my testing area, those work just fine, but when I attempted to implement the system in my actual game, this problem I’m now facing reared its ugly head.

I tried looking at the attribute of my placeholder pickaxe tool, and well, that isn’t an issue as it works just fine!

https://gyazo.com/3b485f2bb5e8951f5de3a9566425b859

Usually that issue is related to the fact that studio runs the ‘server’ and the client both on your computer.
In the actual game you’re running on the Roblox server and your client.

I’m definitely not an expert but is this script a localscript or server script?
Where is this script located?

I think the problem is your including the thing its supposed to be hitting, which is the hitbox, not the actual stuff its supposed to be hitting which is the ores in this case. i dont think you even need to use raycasting for this it might just be better to use .touched or region3 to detect what the pickaxe is hitting