Raycast won't output desired target

Whenever you click on a npc or player with my tool, it sends out a raycast to make sure nothing is blocking the way. However, it outputs the tool’s handle constantly instead of the object blocking it. Does anyone know why?

local gun = script.Parent
--SERVER SCRIPT
script.Parent.MouseEvent.OnServerEvent:Connect(function(player, target)
	local gunCast = gun.Handle
	local RootPart = target.Parent.HumanoidRootPart
	local parameters = RaycastParams.new()
	parameters.FilterType = Enum.RaycastFilterType.Blacklist
	for i, v in pairs(player.Character:GetChildren()) do
		if v:IsA("Part") or v:IsA("MeshPart") then
			parameters.FilterDescendantsInstances = {gunCast, v}
		end
	end
	while true do
		local GunRay = workspace:Raycast(gunCast.Position, RootPart.Position - gunCast.Position)
		print(GunRay)
		wait(1)
	end
end)
--LOCAL SCRIPT
--REMOTE EVENT REQUIRED
local gun = script.Parent
local localplayer = game.Players.LocalPlayer
local mouse = localplayer:GetMouse()


gun.Activated:Connect(function()
	if mouse.Target and mouse.Target.Parent then
		if mouse.Target.Parent:FindFirstChildWhichIsA("Humanoid") and mouse.Target.Parent:FindFirstChildWhichIsA("Humanoid").Health > 0 then
			local plrTarget = mouse.Target
			script.Parent.MouseEvent:FireServer(plrTarget)
		end
	end
end)

You should add your tool to the parameters.FilterDescendantsInstances table to avoid the Handle from registering

But I already did that. I registered it here
image
gunCast is the tool’s handle, v is the player that is holding the tool’s character parts

Try setting the .CanTouch and .CanQuery properties of the objects you want to be ignored when detecting the raycast hit part to false.

1 Like

I can’t change the .CanQuery property without disabling collision of the tool

Well too bad, then I think you have no choice but to do it.

Going mostly off of this:

I would try doing:

parameters.FilterDescendantsInstances = {gun, player.Character}

Instead of:

	for i, v in pairs(player.Character:GetChildren()) do
		if v:IsA("Part") or v:IsA("MeshPart") then
			parameters.FilterDescendantsInstances = {gunCast, v}
		end
	end
1 Like

He could also do this:

parameters.FilterDescendantsInstances = gun:GetDescendants()
1 Like

Ohhh, that makes so much more sense. I thought it would blacklist the specific part.

No problem, it looked like your code was working it was just getting the descendants of your tools handle, but not the actual handle, same for your body parts (at least all accessories were being registered!)

It should work though, hopefully.

Quick question, when you print the raycastresult, does it print the InstanceName or the actual name of the part.
For example the result is a part named Hello, would it print as Hello or Part.
Since the result now is just “Part”.

I believe it prints the Instance it has come in contact with, which would probably show up as Part aka its ClassName, with this result you have direct access to its properties

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