Preventing my npc from shooting raycasts through walls?

Im making an npc and it detects players through walls how could I fix this?
Script:

local DISTANCE = 50

local Players = game:GetService("Players")

local origin = script.Parent.Head 
local equip = script.Parent:WaitForChild("Humanoid").Animator:LoadAnimation(script.Parent.Anims.Equip)
local idle = script.Parent:WaitForChild("Humanoid").Animator:LoadAnimation(script.Parent.Anims.Idle)
local fire = script.Parent:WaitForChild("Humanoid").Animator:LoadAnimation(script.Parent.Anims.Fire)
local walk = script.Parent:WaitForChild("Humanoid").Animator:LoadAnimation(script.Parent.Anims.Walk)

db = false
while task.wait() do
	for _, player in pairs(Players:GetPlayers()) do 
		if player.Character  then 
			local HumanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
			local rayParams = RaycastParams.new()
			rayParams.FilterDescendantsInstances = {player.Character}
			rayParams.FilterType = Enum.RaycastFilterType.Whitelist
			local result = workspace:Raycast(origin.Position, CFrame.lookAt(origin.Position, HumanoidRootPart.Position).LookVector * DISTANCE, rayParams) 
			if result then
				local name = player.Name
				script.Parent.Head.face.Transparency = 0
				idle:Play()
				equip:Play()
				local ObjectPosition = game.Workspace:WaitForChild(name).PrimaryPart.Position
				script.Parent.PrimaryPart.CFrame = CFrame.new(script.Parent.PrimaryPart.Position, Vector3.new(ObjectPosition.x, script.Parent.PrimaryPart.Position.y, ObjectPosition.z))
				if db == false then
					db = true
					fire:Play()
					game.Workspace:WaitForChild(name):WaitForChild("Humanoid").Health -= 15
					script.Shoot:Play()
					script.Parent.Humanoid:MoveTo(game.Workspace:WaitForChild(name).PrimaryPart.Position)
					walk:Play()
					script.Parent.Humanoid.MoveToFinished:Connect(function()
						walk:Stop()
						idle:Stop()
					end)
					script.Parent.Head.face.Transparency = 1
					wait(2)
					db = false
				end
			end
		end
	end 
end

Your ray is ignoring walls and everything except the character, you should make it a Blacklist filter type

Also, preferably remove the FilterDescendantsInstances and just check if the part hit is owned by the character. The way you’re casting your ray may not work properly if this uses it as it could collide with a part beyond the character

2 Likes

I added the map to that and it is still broken.

rayParams.FilterDescendantsInstances = {player.Character, workspace.Map}

In what way is it “broken”? Does it cause the same unwanted result? Are there any errors in the output?

same unwanted result, no errors

1 Like

Did you make sure that the instance hit is part of the character?
I tried it with this snippet and it works perfectly fine

local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {player.Character, workspace.Map}
rayParams.FilterType = Enum.RaycastFilterType.Whitelist
local result = workspace:Raycast(origin.Position, CFrame.lookAt(origin.Position, HumanoidRootPart.Position).LookVector * DISTANCE, rayParams) 
if result and result.Instance:IsDescendantOf(player.Character) then
	-- ...
end