Script to exclude Accesories from Raycast not working

  1. **What do you want to achieve?
    I want to be able to raycast without having the player’s accessories blocking the ray.

  2. What is the issue?
    Although I have excluded the accessories and handle, they are still blocking my ray? I am unsure if it’s because of the raycast filtering param not accepting accessories as hats.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have made a code that put these accessories and handles into an array. There are other approaches to this, but I specifically want my code to be used in this case.

The following code is in localscript

local MAX_DISTANCE = 1000
local excludeTable = {}
-- Filter all mouse related parts
for _, filterInstance in pairs(game.Workspace.FilterMouse:GetChildren()) do
	table.insert(excludeTable, filterInstance)
end
			
for _, part in pairs(character:GetChildren()) do
	if part.ClassName == "Accessory" then
		for _, descedant in pairs(part:GetDescendants()) do
			if descedant:IsA("BasePart") then
				table.insert(excludeTable, descedant)
			end
		end
	end
end
			
local newRaycastParams = RaycastParams.new()
newRaycastParams.FilterDescendantsInstances = excludeTable
newRaycastParams.FilterType = Enum.RaycastFilterType.Blacklist
			
local newRayDirection = (mouse.Hit.Position - character.Head.Position).Unit * MAX_DISTANCE 
local newRaycastResult = game.Workspace:Raycast(character.Head.Position, newRayDirection, newRaycastParams)
-- Go through all parents until character is found	
if newRaycastResult then
	local part = newRaycastResult.Instance
				
	while part.Parent ~= nil do
		print(part.Name)
		if part:FindFirstChildWhichIsA("Humanoid") then
			print("found character")
			local otherCharacter = part
			for _, children in pairs(character:GetChildren()) do
				if children.ClassName == "Accessory" then
					for _, descedant in pairs(children:GetDescendants()) do
						if descedant:IsA("BasePart") then
							table.insert(excludeTable, descedant)
						end
					end
				end
			end
			break
		end
	print("didn't found character")
	part = part.Parent
	end
end
			
print(excludeTable) -- shows all parts excluded
-- refire raycast with new excluded parts		
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = excludeTable
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
			
local rayDirection = (mouse.Hit.Position - character.Head.Position).Unit * MAX_DISTANCE 
local raycastResult = game.Workspace:Raycast(character.Head.Position, rayDirection, raycastParams)
			
if raycastResult then
	print(raycastResult.Instance) -- Prints handle that was already excluded??
	print(raycastResult.Instance.Parent)
end

What I do for my game is I just edit the CanQuery property and turn it off for all the descendants of Accessory instances.

a part cannot be an accessory i would say instead of if part.ClassName == "Accessory" make: if part.Parent:IsA("Accessory") then

I understand this solution, but I already have another loop where the descendant of that accessory, in this case the handle, is being added to the exclude list

Never mind everyone, sorry if you were finding a solution through this excluding technique. I found a way where I included the character’s part instead of excluding only their hats. This method is much faster compared if you were to fire 2 raycast at once. Let me know if I am correct about the performance part, but I would include the player’s body to be detected by raycasting.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.