How to make a raycast ignore a handle

Hello everyone! So i’m trying to make a sphere that uses raycasting to damage the player if it detects a humanoid.

If you look in the output it keeps printing “Handle” and it’s probably because my avatar had a banana suit and i don’t know how to locate it in FilterDescendantsInstances

Here’s the script:

while wait(1) do
	local LaserSphere = script.Parent
	local Position = LaserSphere.Position
	local Direction = -LaserSphere.CFrame.RightVector
	local rcParams = RaycastParams.new()
	rcParams.FilterDescendantsInstances = {LaserSphere}
	rcParams.FilterType = Enum.RaycastFilterType.Exclude

	local ray = workspace:Raycast(Position, Direction * 50)
	
	if ray then
		local Part = ray.Instance
		print(Part)
		if Part.Parent:FindFirstChild("Humanoid") then
			Part.Parent.Humanoid.Health -= 10
		end
	end
end

And thanks for helping!:smile:

2 Likes

Just add character in the filterdescendentinstances table and you would be good to go.

i used game.Players.LocalPlayer.Character but then i got this error: attempt to index nil with ‘Character’

Is the script a server script or local script?

1 Like

game.Player.LocalPlayer is for the client.
If you are doing this on a server script, it will be nil
That is what is causing the error

1 Like

So i have to use a local script?

yes you would.

I believe you are trying to make a laser damage a player when it crosses the ray from a sphere. Why would you not want the Handle to be involved? The handle is probably an accessory you have equipped on your character

Because else it doesn’t detect the humanoid

When a part is hit, check its parent, then check that parent’s parent, and keep going until either, the part has a humanoid as a child, or you reach workspace.

try replacing it with this

local Part = ray.Instance
local char = Part:FindFirstAncestorWhichIsA("Model")
if char ~= nil then
	local h = char:findFirstChild("Humanoid")
	if h ~= nil then
		h:TakeDamage(10)
	end
end

*** Polo beat me to it :stuck_out_tongue:

function checkPart (part)
  if part:FindFirstChild("Humanoid") then
    return true
  end
  if part.Parent == nil or part.Parent == workspace then
    return false
  end
  return checkPart(part.Parent)
end

something like that

1 Like

Now i got this error: attempt to index nil with ‘FindFirstChild’ and i’m using a local script that i have parented to the laser sphere

Did you use my code?

while wait(1) do
	local LaserSphere = script.Parent
	local Position = LaserSphere.Position
	local Direction = -LaserSphere.CFrame.RightVector
	local rcParams = RaycastParams.new()
	rcParams.FilterDescendantsInstances = {LaserSphere}
	rcParams.FilterType = Enum.RaycastFilterType.Exclude

	local ray = workspace:Raycast(Position, Direction * 50)

	if ray then
		local Part = ray.Instance
		print(Part)
		local char = Part:FindFirstAncestorWhichIsA("Model")
		if char ~= nil then
			local h = char:findFirstChild("Humanoid")
			if h ~= nil then
				h:TakeDamage(10)
			end
		end
	end
end

Also if this does solve your issue, please mark it as solved

2 Likes

You can set the CanQuery property of the accessory handles to false when a character spawns in, which prevents them from being included in operations like raycasting.

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        task.wait(0.5) -- small wait otherwise this might not work properly
        for _, v in pairs(character:GetDescendants()) do
            if v.Name == "Handle" and v.Parent:IsA("Accessory") then
                v.CanQuery = false
            end
        end
    end
end
2 Likes

It works! Thank you so much for helping.:smile:

1 Like

I’ll keep that in mind, thanks for telling me that.

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