So I want to make an NPC that follows you and when it's close enough it kills you, but for some weird reason the if statement doesn't fire. I replaced the less than sign with the greater than sign, and that seemed to work but it kills you anywhere, please help.
if (script.Parent.HumanoidRootPart.Position - v.HumanoidRootPart.Position).magnitude < 10 then
v.Humanoid:TakeDamage(100)
wait(1.5)
d = false
end
Full script:
local d = false
while wait() do
for i, v in pairs(workspace:GetChildren()) do
if v:FindFirstChild("HumanoidRootPart") and (script.Parent.HumanoidRootPart.Position - v.HumanoidRootPart.Position).magnitude < 1000 and v ~= script.Parent and v.Humanoid.DisplayName ~= "Duck" and v:FindFirstChild("HumanoidRootPart").Anchored ~= true and v.Humanoid.Health ~= 0 then
if not d then
d = true
script.Parent.Humanoid:MoveTo(v.HumanoidRootPart.Position)
if (script.Parent.HumanoidRootPart.Position - v.HumanoidRootPart.Position).magnitude < 6 then
v.Humanoid:TakeDamage(100)
wait(1.5)
d = false
end
end
end
end
end
script.Parent.Humanoid.Died:Connect(function()
script:Destroy()
end)

2 comments
wdym by "but it kills you anywhere"? does that mean there are hiding spots/safe zones?
No, I meant that if you are far away from him he can still kill you. And I don't want it to do that.