Raycast hits local player despite the character being in the ignore list

I want the raycast to ignore the player

The raycast hits the local player despite the character being in the ignore list. The character is ignored until it dies.

local ignoreTable = {character, workspace.Ignores, camera}
castParams.FilterType = Enum.RaycastFilterType.Blacklist
castParams.IgnoreWater = true
castParams.FilterDescendantsInstances = ignoreTable
--
	local origin = camera.CFrame.Position
	local spread = weaponData.Spread
	local randSpread = math.random(-weaponData.Spread*100, weaponData.Spread*100)/100
	local direction = (CFrame.new(origin, mouse.Hit.Position)*CFrame.Angles(math.rad(randSpread), math.rad(randSpread), 0)).LookVector
	--
	muzzleEffects(viewmodel, weaponData)
	recoil:ShootRecoil(weaponData)

	
	mouse.TargetFilter = camera
	distance = (origin - mouse.Hit.Position).Magnitude
	weaponData.Ammo -= 1
	--
	local result = workspace:Raycast(origin, direction * 999, castParams)
	if result and result ~= nil then onRayHit(result, direction) end
	mouse.TargetFilter = camera

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

I don’t know for certain, but I have experiemented before and found that whenever setting the FilterDescendantsInstances you have to define a new table every time. You cannot edit an existing one. You may try to set it up like this instead.

castParams.FilterDescendantsInstances = {character, workspace.Ignores, camera}

EDIT: Also, some of your code is redundant, such as the code determining the direction the mouse is pointing in. You can just use the Mouse | Roblox Creator Documentation property. Also, CFrame.new(origin, target) has been deprecated and replaced with CFrame.lookAt(origin, target)

I still have the same issue that I had. I update the ignore list on every shot and I can still hit my own character.

function module:FireNormalShot(viewmodel, weaponData)
	--
	castParams.FilterDescendantsInstances = {character, workspace.Ignores, camera}
	
	local origin = camera.CFrame.Position
	local spread = weaponData.Spread
	local randSpread = math.random(-weaponData.Spread*100, weaponData.Spread*100)/100
	local direction = (CFrame.new(origin, mouse.Hit.Position)*CFrame.Angles(math.rad(randSpread), math.rad(randSpread), 0)).LookVector
	--
	muzzleEffects(viewmodel, weaponData)
	recoil:ShootRecoil(weaponData)
	
	mouse.TargetFilter = camera
	distance = (origin - mouse.Hit.Position).Magnitude
	weaponData.Ammo -= 1
	--
	local result = workspace:Raycast(origin, direction * 999, castParams)
	if result and result ~= nil then onRayHit(result, direction) end
end

Where is your code defining the character variable then? Are you making sure it’s completely loaded before trying to reference it?

local player = game.Players.LocalPlayer
local character = player.Character

image

Here’s an example of Mouse.Origin:

function module:FireNormalShot(viewmodel, weaponData)
	--
	local spread = weaponData.Spread
	local randSpread = math.random(-weaponData.Spread*100, weaponData.Spread*100)/100
	local direction = (mouse.Origin * CFrame.Angles(math.rad(randSpread), math.rad(randSpread), 0)).LookVector
	--
	muzzleEffects(viewmodel, weaponData)
	recoil:ShootRecoil(weaponData)
	
	mouse.TargetFilter = camera
	distance = (mouse.Origin.Position - mouse.Hit.Position).Magnitude
	weaponData.Ammo -= 1
	--
	local result = workspace:Raycast(origin, direction * 999, castParams)
	if result then onRayHit(result, direction) end
end

You can use this instead:

local character = player.Character or player.CharacterAdded:Wait()