Magnitude not working

Here is my code:

local zombieTorso = script.Parent.Torso
local zombieHum = script.Parent.Humanoid

local function findTarget()
	local agroDistance = 100
	local target = nil
	for i,v in pairs(game.Workspace:GetChildren())do
		local human = v:FindFirstChild("Humanoid")
		local Torso = v:FindFirstChild("Torso")
		if human and Torso and v == script.Parent then
			--check distance
			if (zombieTorso.Position - Torso.Position).magnitude < agroDistance then
				agroDistance = (zombieTorso.Position - Torso.Position).magnitude
				target = Torso
	return target

while wait(1) do
	local Torso = findTarget()
	if Torso then

The script is inside the npc ^^

Im trying to make an npc to chase another npc
It dint give me any errors
any help will be appreciated


I had this issue for a while, it would only return ‘0’ if I printed it. [check this before you try anything, if it returns a correct value dont edit it]
For some reason putting a CalculateMagnitude(PointA, PointB) function into a module script solved it? Still unsure as to why.

You keep assigning a magnitude to agroDistance when you can keep it at 100 at all times.

So what you are doing right now is changing the agroDistance for every loop.

does that mean i have to remove the magnitude at the line?

Yeah, try removing this line → agroDistance = (zombieTorso.Position - Torso.Position).magnitude

It din’t work and it also din’t give me any errors

Try doing v ~= script.Parent instead.

It’s probably something to do with your if statement. Try debugging. Maybe remove the v == script.Parent?

local human = v:FindFirstChild("Humanoid")
local Torso = v:FindFirstChild("Torso")


Check if the human and Torso exists. Let me know if it prints something in the output.

Thx it work now and thx @Abroxus for helping too.

1 Like