Raycast incorrectly recieving information

Hey everyone. I made a script where the NPC will only attack the player if it can see the player, however even when the player is behind a wall it can see the player. I don’t know how to fix this. I urgently need to fix it too. Thanks.

local PathfindingService = game:GetService("PathfindingService")

local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local hrp = npc:WaitForChild("HumanoidRootPart")
hrp:SetNetworkOwner(nil)

local walkAnim = humanoid.Animator:LoadAnimation(script.Walk)
local attackAnim = humanoid.Animator:LoadAnimation(script.Attack)

local pathParams = {
	AgentHeight = 5,
	AgentRadius = 3,
	AgentCanJump = true,
}

local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
rayParams.FilterDescendantsInstances = {npc}

local lastPos
local animPlaying = false

local RANGE = 45
local DAMAGE = 100

local function canSeeTarget(target)
	local orgin = hrp.Position
	local direction = (target.HumanoidRootPart.Position - hrp.Position).Unit * RANGE
	local ray = workspace:Raycast(orgin, direction, rayParams)

	if ray and ray.Instance then
		if ray.Instance:IsDescendantOf(target) then
			print("CAN SEE")
			return true
		else
			print("CANnot SEE")
			return false
		end
	else
		return false
	end
end

local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = RANGE
	local nearestTarget

	for i, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (hrp.Position - target.HumanoidRootPart.Position).Magnitude

			if distance < maxDistance and canSeeTarget(target) then
				print("HEY")
				local plr = game.Players:GetPlayerFromCharacter(target)
				game.ReplicatedStorage.Seen:FireClient(plr, true)
				nearestTarget = target
				maxDistance = distance
				if canSeeTarget(target) == false then
					game.ReplicatedStorage.Seen:FireClient(plr, false)
				end
			end
		end
	end

	return nearestTarget
end



while task.wait(0.2) do
	findTarget()
end

you can try instead of making the canseetarget and the find target be different functions, make when the find target function be fired() it creates the raycast and then check if the part is actually visible