How do I make the Ray ignore a HumanoidRootPart? [SOLVED]

Hello, I’m new to raycasting and I’m facing a problem.

I have this raycast function in a weapon tool and it works perfectly.

It’s just that the HumanoidRootPart of the target, blocks the ray from reaching the upper torso or lower torso.

I tried inserting the HumanoidRootPart into the IgnoreList table and it doesn’t seem to work.

function FireGun(Type, MousePos, Target)
	
	if Target then
		local gun = script.Parent

		if not gun then
			return
		end

		local IgnoreList = {character, gun}

		if Target:FindFirstAncestorOfClass("Model"):FindFirstChild("Humanoid") then
			table.insert(IgnoreList, Target:FindFirstAncestorOfClass("Model").PrimaryPart)
		end
		
		local gunHandle = gun:WaitForChild("Handle", 1000)
		local gunTip = gun.Parts.Nodes:WaitForChild("Barrel", 1000)

		local barrelPosition = gunTip.Position
		local targetPoint

		local raycastParams = RaycastParams.new()
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		raycastParams.IgnoreWater = true
		raycastParams.FilterDescendantsInstances = IgnoreList

		local rayDirection = gunHandle.CFrame:vectorToWorldSpace(Vector3.new(0, 0, -1))
		local ray = Ray.new(gunTip.Position, rayDirection * 3000)

		local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, IgnoreList)

		targetPoint = position

		if Type == "Mouse" then
			targetPoint = MousePos
			hit = Target
		end

		local distance = (barrelPosition - targetPoint).Magnitude

		if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then
			local targetHumanoid = hit.Parent:FindFirstChild("Humanoid")
			dealDamage(hit, targetHumanoid)
		elseif hit:FindFirstChild("AccessoryWeld") then
			local targetHumanoid = hit:FindFirstAncestorOfClass("Model"):FindFirstChild("Humanoid")
			dealDamage(hit, targetHumanoid, hit:FindFirstChild("AccessoryWeld").Part1)
		end
	end
end

Thanks for reading.

Try this?

function FireGun(Type, MousePos, Target)
	
	if Target then
		local gun = script.Parent

		if not gun then
			return
		end

		local IgnoreList = {character, character:FindFirstChild("HumanoidRootPart"), character:WaitForChild("HumanoidRootPart", 3.5), gun}

		if Target:FindFirstAncestorOfClass("Model"):FindFirstChild("Humanoid") then
			table.insert(IgnoreList, Target:FindFirstAncestorOfClass("Model").PrimaryPart)
		end
		
		local gunHandle = gun:WaitForChild("Handle", 1000)
		local gunTip = gun.Parts.Nodes:WaitForChild("Barrel", 1000)

		local barrelPosition = gunTip.Position
		local targetPoint

		local raycastParams = RaycastParams.new()
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		raycastParams.IgnoreWater = true
		raycastParams.FilterDescendantsInstances = IgnoreList

		local rayDirection = gunHandle.CFrame:vectorToWorldSpace(Vector3.new(0, 0, -1))
		local ray = Ray.new(gunTip.Position, rayDirection * 3000)

		local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, IgnoreList)

		targetPoint = position

		if Type == "Mouse" then
			targetPoint = MousePos
			hit = Target
		end

		local distance = (barrelPosition - targetPoint).Magnitude

		if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") and hit.Name ~= "HumanoidRootPart" then
			local targetHumanoid = hit.Parent:FindFirstChild("Humanoid")
			dealDamage(hit, targetHumanoid)
		elseif hit:FindFirstChild("AccessoryWeld") then
			local targetHumanoid = hit:FindFirstAncestorOfClass("Model"):FindFirstChild("Humanoid")
			dealDamage(hit, targetHumanoid, hit:FindFirstChild("AccessoryWeld").Part1)
		end
	end
end

Ignore the code above, you should probably follow what @Dude_Danish is saying because I am not experienced in raycasting at all lol

I think you should keep ignorelist as it is in the code above though.

Ray.new is deprecated. I’d recommend workspace:Raycast instead. In general the code is a bit confusing to me, as you have raycastparams defined and all but it isn’t actually implemented into the ray itself?

That unfortunately doesn’t work

Yeah, as I mentioned, i’m new to raycasting and don’t have much experience in raycasting.

It’s totally fair, I’m pretty new myself and spend a great amount of time trying to understand it.
But try to replace this:

local ray = Ray.new(gunTip.Position, rayDirection * 3000)

With this:

local ray = workspace:Raycast(gunTip.Position, rayDirection * 3000, raycastParams)

An error occured:

Unable to cast RaycastResult to Ray
Line 30 - function FireGun

Line 30 is:
local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, IgnoreList)

find part on ray is also decaprecated. use workspace:Raycast()

Oh right, FindPartOnRayWithIgnoreList is deprecated aswell, I believe you can just delete that as you already got the RaycastParams defined in the third parameter.

use raycast parameters, and set blacklist to parts you vvan’t , this is all

Alright, I just tried that and another error occured:

Unable to cast RaycastResult to Vector3
Line 30 - function FireGun

--The 'ray' variable is causing this error now
local hit, position = game.Workspace:Raycast(ray, raycastParams)

result is one, you have to get result, then result.Position ect.

I don’t quite understand, what ‘result’ are you referring to?

1 Like

This is the Incorrect function to be using. RaycastParams only work with workspace:Raycast which is faster and more accurate than Ray

workspace:Raycast returns what is called a RaycastResult, which is a table of Data consisting of specific Details / Object of where the Ray was intersected.

You get this Data when it was a Successful Raycast, if a Raycast Fails, or does not reach a destination, the result will return nil, which is why you check if it was sucessful using an if statement, like this:

local rayResult = workspace:Raycast(pos, dir, params) -- the Raycast

if rayResult then -- if the Raycast was successful
    local PartOnRay = rayResult.Instance -- Instance Intersected
    print(`Intersected {PartOnRay} at {rayResult.Position}`)
else
    print(`Nothing was Intersected, Result: {rayResult}`)
end

As far as I know for workspace:FindPartOnRay(), im pretty sure RaycastResult.Instance is its replacement.

1 Like

result is vvhat you are doing, if you doing vvorkspace:raycast() then this is result

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