Get raycast to ignore walls

I’m trying to make a custom weapon. When it shoots, localscript finds the position of the mouse using uis:GetMouseLocation() and ScreenPointToRay().Origin.

The localscript works fine and it’s prints out the correct vector3 position.

The localscript remotely sends the position to the server script. The server then uses the position it receives to make another ray to find the npc’s instance. I can’t do both rays on the client because not all instances inside the npc loads for the client yet then it can’t find them.

There’s an invisible wall that’s using collisiongroup to prevent he npc from falling off the map while chasing the player. The character model doesn’t collide with the wall so it can go behind that wall (like a safe zone). The player then shoots the npc, but the ray can’t find the npc, instead it’s hitting the wall.

I made a collision group for the ray, but it seems like it’s ignoring it. I’m not sure if my script is right.

Note: The weapon is an accessory with a RightGripAttachment.

The server part:

local tool = script.Parent
local remote = tool:WaitForChild("mouseremote")

local firepart = tool:WaitForChild("FirePart")

local tween = game:GetService("TweenService")
local info = TweenInfo.new(
	1,
	Enum.EasingStyle.Circular,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

local function maketag(player,clonepart)
	local tag = Instance.new("ObjectValue")
	tag.Value = player
	tag.Name = player.UserId
	tag.Parent = clonepart
end

local blacklist = {}

local params = RaycastParams.new()
params.CollisionGroup = "Raycasting"
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = blacklist

local function getray(clonepart,pos,char)
	
	table.insert(blacklist,char)
	table.insert(blacklist,workspace.Arena.mobwalls)
	
	local ray = workspace:Raycast(clonepart.Position,pos,params)
	if ray then
		return ray.Instance
	else
		clonepart:Destroy()
	end
end

remote.OnServerEvent:Connect(function(player,pos)
	local clonefire = firepart:Clone()
	clonefire.Anchored = true
	clonefire.RopeConstraint:Destroy()
	clonefire.Parent = workspace
	clonefire.Trail.Enabled = true
	clonefire.Transparency = 0.9
	clonefire.FirePartTouch.Enabled = true
	clonefire.CanTouch = true
	clonefire.att0.CFrame = CFrame.new(0,2,0)
	clonefire.att1.CFrame = CFrame.new(0,-2,0)
	
	maketag(player,clonefire)
	
	local mobInstance = getray(clonefire,pos,player.Character)
	
	if mobInstance then
		print("mob found, moving out")
		local makemove = tween:Create(clonefire,info,{Position = mobInstance.Position})
		makemove:Play()
		table.clear(blacklist)
		
	else
		print("no mob found")
		table.clear(blacklist)
		
		return
	end
	
end)


It’s printing “mob found” when player’s inside the arena and not behind a wall. It’s printing “no mob found” when player goes behind a wall even when the npc is face to face with the player.

I solved this with an alternative way, using workspace:GetPartBoundsInRadius(). This way, I can raycast all around the player to find npcs that are near it at once.

local tool = script.Parent
local remote = tool:WaitForChild("mouseremote")

local debris = game:GetService("Debris")

local tween = game:GetService("TweenService")
local info = TweenInfo.new(
	1,
	Enum.EasingStyle.Circular,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

local function maketag(player,clonepart)
	local tag = Instance.new("ObjectValue")
	tag.Value = player
	tag.Name = player.UserId
	tag.Parent = clonepart
end


local function makefire()
	local clonefire = tool.FirePart:Clone()	
	clonefire.RopeConstraint:Destroy()
	clonefire.Anchored = true
	clonefire.Parent = workspace
	clonefire:WaitForChild("Trail").Enabled = true
	clonefire.Transparency = 0.9
	clonefire:WaitForChild("FirePartTouch").Enabled = true
	task.wait(0.1)
	clonefire.CanTouch = true
	clonefire.att0.CFrame = CFrame.new(0,2,0)
	clonefire.att1.CFrame = CFrame.new(0,-2,0)
	task.wait()
	return clonefire
end

local function getmobs(player)
	local char = player.Character
	local overlapParams = OverlapParams.new()
	overlapParams.CollisionGroup = "Raycasting"
	overlapParams.FilterDescendantsInstances = {workspace.Arena.mobwalls, char}
	overlapParams.FilterType = Enum.RaycastFilterType.Exclude
	overlapParams.MaxParts = 5
	
	local seek = workspace:GetPartBoundsInRadius(char.HumanoidRootPart.Position,100,overlapParams)
	
	--for _,thing in pairs(seek) do
	--	print(thing,(thing.Position - char.HumanoidRootPart.Position).Magnitude)
	--end
	
	local clonefire = makefire()
	maketag(player,clonefire)
	
	local firstone = seek[1]
	if firstone then
		local makemove = tween:Create(clonefire,info,{Position = firstone.Position})
		makemove:Play()
		debris:AddItem(clonefire,2)
		
	else
		debris:AddItem(clonefire,0.3)
	end
end

remote.OnServerEvent:Connect(getmobs)