Need help with npc player dection system

I am trying to make a system where, if the player is within 200 studs of the NPC and there is no part/wall blocking the 2, it will shoot. But it barely works. I need to get right infront of the npc for it to shoot me and i also get shot even though there is a wall between the npc and the player.

Hese is my (local)script:

while true do
wait(2)
local plr = game.Players.LocalPlayer
	local char = script.Parent
	local hrp = char:WaitForChild("HumanoidRootPart")
for i, v in pairs(workspace.Wantedhouse1NPCs:GetChildren()) do
		local targetchar = v
		if v:FindFirstChild("Wanted") then
			print(targetchar.Name)
			local targethrp = v:WaitForChild("HumanoidRootPart")
			local vector = (hrp.Position - targethrp.Position)
			if vector.magnitude < 200 and plr.Team ~= game.Teams.Civillian and plr.Team ~= game.Teams.Outlawed and targetchar:WaitForChild("Humanoid").Health > 0 then
				local raypa = RaycastParams.new()
				local result = workspace:Raycast(script.Parent.Head.Position,vector,raypa)
				if not result then
					print("Damaging "..char.Name)
					targetchar:FindFirstChild("GEAR").RemoteEvent:FireServer(char.Torso, workspace.NPCs.walkspeed.Value)
				else
					print(result)
				end
			end
		end
	end
end

Help would be appreciated!

EDIT:
I have changed the code to this:

    wait(2)
    local plr = game.Players.LocalPlayer
    local char = script.Parent
    local hrp = char:WaitForChild("HumanoidRootPart")
    print(hrp.Position)
    for i, v in pairs(workspace.Wantedhouse1NPCs:GetChildren()) do
        local targetchar = v
        if v:FindFirstChild("Wanted") then
            print(targetchar.Name)
            local targethrp = v:WaitForChild("HumanoidRootPart")
            local vector = (hrp.Position - targethrp.Position)
            if vector.magnitude &lt; 200 and plr.Team ~= game.Teams.Civillian and plr.Team ~= game.Teams.Outlawed and targetchar:WaitForChild("Humanoid").Health &gt; 0 then
                local raypa = RaycastParams.new()
                local result = workspace:Raycast(script.Parent.Head.Position,vector,raypa)
                if not result then
                    print("Damaging "..char.Name)
                    targetchar:FindFirstChild("GEAR").RemoteEvent:FireServer(char.Torso, workspace.NPCs.walkspeed.Value)
                else
                    print(result)
                end
            end
        end
    end
end

and it still doesnt work.

There are a couple things to note:

I need to get right infront of the npc for it to shoot me

You're checking the distance from the player's ```HumanoidRootPart``` to the NPC's ```HumanoidRootPart```. That's going to be the distance between the two characters' feet. You probably want to check the distance to the player's ```Head``` instead.

I also get shot even though there is a wall between the npc and the player.

You're not checking for walls. You're checking whether the raycast returns a result or not. It means you're checking whether the player has their head inside a part. That's not going to help you. Instead, you should be checking the ```Instance``` property of the ```RaycastResult```. If this is a ```Model```, you can check whether the model is a wall. If this is a ```Part```, you can check whether the part is a wall. If this is ```nil```, then the raycast didn't hit anything.