for i,v in pairs(workspace:GetChildren()) do
if v:FindFirstChild("Humanoid") and v ~= character then
print(v)
local distance = (v.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude
if distance < 10 then
v:Destroy()
end
end
end
I also want to add to this some code that makes it so if I’m not facing the humanoidrootpart of the target, the target doesn’t get destroyed. I know of dotproduct, it checks angles but my angle is the exact same even when I turn around. I read a bit on CFrame inversing, I don’t really know how I’d use it for this situation. Help would be appreciated.
This system looks for all parts that are in front of the selected part (you can modify the lenght of the detector). I think you could make a table of all characters in the game and do it a whitelist and the Ray will looks for parts in this whitelist who are in front of your selected part
I hope my english is not too bad lol
Hope help you and If you need some help i will try reply to you !
If you get the vector from your NPC to your player character then get the dot product of that with the look vector of the NPC’s HRP it will give a number from -1 to 1 and if it is positive that means your NPC is looking within 180 degrees of your player character
for i,v in pairs(workspace:GetChildren()) do
if v:FindFirstChild("Humanoid") and v ~= character then
local direction = character.HumanoidRootPart.Position - v.HumanoidRootPart.Position
local distance = Direction.Magnitude
direction = direction.Unit
if distance < 10 then
print("Within Range")
if direction:Dot(v.HumanoidRootPart.CFrame.LookVector) > 0 then
print("Within 180 degrees")
v:Destroy()
end
end
end
end