How do i add ignore list to raycasting?

Hello DevForum community.

I have a problem where my gun can penetrate players and certain material wich is fine however because of the “HumanoidRootPart” the Player Torso dont get hit making him loose no health but the shot will still penetrate him because it finds humanoid in thie player grouped wich is fine.

Here is my shooting function

function Fire(ShootingHandle)
	if PullingOut == false and CurrentlyUnJamming == false then
	local PierceAvailable = Module.Piercing
	PiercedHumanoid = {}
	function RayCast(Start, Direction, Range, Ignore)
		local MouseUnit = Direction.unit -- The unit of where the mouse is aiming.
		local Spread = Module.Spread*(AimDown and 1-Module.SpreadRedution or 1)
		local BulletUnit = (script.Parent.Muzzle.CFrame *  CFrame.Angles(math.rad(-Spread+(math.random()*(Spread*2))),math.rad(-Spread+(math.random()*(Spread*2))),0)).lookVector -- The unit of where the barrel is facing.
		local UnitTolerance = 1 -- If the difference between the two units is greater than this, the BulletUnit will be used. Otherwise, the MouseUnit is used.
		local AcceptedUnit
		
		if (MouseUnit-BulletUnit).magnitude > UnitTolerance then -- Out of bounds.
			AcceptedUnit = BulletUnit
		else -- Within bounds.
			AcceptedUnit = MouseUnit
		end

		BulletRay = Ray.new(Start, Direction+AcceptedUnit * Range)
		local Hit,EndPos = game.Workspace:FindPartOnRay(BulletRay, Ignore)
		CamShake(5,15000)
		if Hit then
if Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent.Humanoid.Health == 0 or Hit.Parent:FindFirstChild("Humanoid") or Hit.Parent:FindFirstChild("Humanoid") and PiercedHumanoid[Hit.Parent.Humanoid] then
				if EndPos and Hit then
					Player.PlayerScripts.BulletVisualizerScript.Visualize:Fire(nil,ShootingHandle, Module.MuzzleOffset,EndPos,script.MuzzleEffect, script.HitEffects.Blood, Module.Blood_HitSound, {Module.ExplosiveEnabled,Module.BlastRadius,Module.BlastPressure},{Module.BulletSpeed,Module.BulletSize,Module.BulletColor,Module.BulletTransparency,Module.BulletMaterial,Module.FadeTime},false,PierceAvailable == -1 and Module.VisualizerEnabled or false, Hit, BulletRay)
					game.ReplicatedStorage.VisualizeBullet:FireServer(ShootingHandle, Module.MuzzleOffset,EndPos,script.MuzzleEffect, script.HitEffects.Blood, Module.Blood_HitSound, {Module.ExplosiveEnabled,Module.BlastRadius,Module.BlastPressure},{Module.BulletSpeed,Module.BulletSize,Module.BulletColor,Module.BulletTransparency,Module.BulletMaterial,Module.FadeTime},true,PierceAvailable == -1 and Module.VisualizerEnabled or false, Hit, BulletRay)
				    local decal = Instance.new("Decal")
                    local decal2 = Instance.new("Decal")
                    local decal3 = Instance.new("Decal")
                    local decal4 = Instance.new("Decal")
			     	decal.Texture = "rbxassetid://246162648"
					decal.Face = "Front"
					decal.Parent = Hit
					decal2.Texture = "rbxassetid://246162648"
					decal2.Face = "Back"
					decal2.Parent = Hit
					decal3.Face = "Left"
					decal3.Texture = "rbxassetid://246162648"
					decal3.Parent = Hit
					decal4.Parent = Hit
					decal4.Texture = "rbxassetid://246162648"
					decal4.Face = "Right"
				end
				local TargetHumanoid = Hit.Parent:FindFirstChild("Humanoid")
				local TargetTorso = Hit.Parent:FindFirstChild("Torso")
				if TargetHumanoid and TargetHumanoid.Health > 0 and TargetTorso then
					InflictTarget:FireServer(TargetHumanoid, TargetTorso, (Hit.Name == "Head" and Module.HeadshotEnabled) and Module.BaseDamage * Module.HeadshotDamageMultiplier or Module.BaseDamage, Direction, Module.Knockback, Module.Lifesteal, Module.FlamingBullet, Hit)
					PiercedHumanoid[TargetHumanoid] = true
				end
				if AcceptedUnit == BulletUnit then
					Hit, EndPos = RayCast(EndPos+(BulletUnit*.01),BulletUnit,Range-((Start-EndPos).magnitude),Ignore)
				elseif AcceptedUnit == MouseUnit then
					Hit, EndPos = RayCast(EndPos+(Direction*.01),Direction,Range-((Start-EndPos).magnitude),Ignore)
				end
			end
		end
		ObjPenetrations = 2
		return Hit, EndPos
	end

here is a video about my problem https://www.youtube.com/watch?v=hj5OJZwD8Sg

7 Likes

Is “Ignore” a table or a part?

If it is a part everything should work fine.

If it is a table i would recommend using FindPartOnRayWithIgnoreList instead of just FindPartOnRay

1 Like

Yea, agree. What I did was just put an ignore list where needed, put models to ignore in it.

Found an example from another post.

local Hit, Position = workspace:FindPartOnRayWithIgnoreList(RY, {script.Parent, game.Workspace.NonCollidableObjects})

Not my code, just an example

the thing is i tried to look for “ignore” i found legit nothing its not a part its not a table its nothing

so my question is the function starts with

local Hit,EndPos = game.Workspace:FindPartOnRay(BulletRay, Ignore)

could i just easily replace it with findPartOnRayWithIgnoreList ? and how do i add HumanoidRootPart to ignore list ?

thats a gunkit i use and i dont have much idea how raycasts work so im confused how do i define HumanoudRootPart to add it to the ignore table ?
and how would i change its currently function to “FinDpartOnRayWithIgnoreList”

if i understand you right you only need to ignore a single part? humanoidrootpart.

Then you can just use FindPartOnRay() and replace Ignore with the humanoidrootpart

Would it then look like this ?

local Hit,EndPos = game.Workspace:FindPartOnRay(BulletRay,  HumanoidRootPart)

or would i need to add this foot tag “” or should i just write the name of the part ? and yes currently i want it to ignore HumanoidRootPart but what about in the future if i maybe want it to ignore player accessories because they block shots aswell

as i said, FindPartOnRay only ignores 1 part. So if you want to add other parts to it just do like this:

local Hit,EndPos = game.Workspace:FindPartOnRayWithIgnoreList(BulletRay,  {HumanoidRootPart, otherpart})

use that instead, it is better if you in the future maybe want to add more parts

Does it then scan for the name ? and if the name is HumanoidRootPart is ignores it ?

Also since in my script the “ignore” dosent mean anything can i just create a local table so i dont have to replace everyhing ?

local Ignore = {HumanoidRootPart, otherpart}

Does it then scan for the name ? and if the name is HumanoidRootPart is ignores it ?

No, you need to give it a object to ignore. So just having “HumanoidRootPart” will just error. It needs to be a object.

Also since in my script the “ignore” dosent mean anything can i just create a local table so i dont have to replace everyhing ?

Well, yes you could, i would still recommend having it in the function. As then it is easier to change what it will ignore.

How do i do that ? how do i add it as a object then ?

I think you should learn atleast basic scripting first. Especially if you are making a gun.

bruh i know some basics but not all i mostly learn by doing i am a german dude i mostly use roblox wiki and dev forum if i get problems

Then add it as a object, it is exactly like doing

Part = workspace.Part

But getting the humanoidrootpart instead

But isnt it more complicated because the Player itself is a group and since i cant just create a variable
because players have different names ?

what i did is when the Hit.Parent finds HumanoidRootPart then add it to the table

Hit.Parent:FindFirstChild("HumanoidRootPart") then
 Ignore[TargetHumanoidRootPart] = true
end

To get the root of a player just do Root = player.Character.HumanoidRootPart

i think that is what you want, atleast from what i have read.
If not then please explain what you want to achieve

Dosent that get the Root of the own player himself ? i want it to ignore every possible HumanoudRootPart in the workspace

then just loop trough each player and add their root to a table that it then will ignore

Ray.new() and workspace:FindPartOnRay… are both legacy API and shouldn’t be used anymore. They’ve actually been deprecated very recently. Instead, you should use the newer and more upgraded raycasting:

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
-- blacklist is essentially the same thing as ignore list
raycastParams.FilterDescendantsInstances = {}
-- put the stuff you want in the ignore list inside that table.

local origin, direction = -- fill in with your own origin/direction

local raycastResult = workspace:Raycast(origin, direction, raycastParams)
if raycastResult then -- it's nil if it doesn't hit anything, so check
    print(raycastResult.Instance) -- the part that it hit
    print(raycastResult.Position) -- the position where the ray hit
    print(raycastResult.Material) -- the material of the part it hit
    print(raycastResult.Normal) -- the surface normal where it hit
end

Some of the benefits of this newer API is that you can reuse RaycastParams (you don’t have to re-create it every time, just once at the top of your script) AND you don’t have to create a ray object every time you want to raycast. It’s just 1 function.

Also, there’s no more checking the docs for the proper argument/return order. Since it’s a table now, you can set the options/read the result in any order you want.

11 Likes

Oh thats interesting. However i would call my self a noob scripter im scared to break the whole script when i try to add this new raycasting would you help me to set this up ?