How do I fix this magnitude script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want an NPC to walk to another if the magnitude is under 100.
  2. What is the issue? The NPC will not move.
  3. What solutions have you tried so far? Asked for help in discord, no one answered :stuck_out_tongue:
local zombieTorso = script.Parent.Torso
local zombieHumanoid = script.Parent.Humanoid

local function findTarget()
	local aggroDistance = 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
			if (zombieTorso.Position - torso.Position).magnitude < aggroDistance then
				aggroDistance = (zombieTorso.Position - torso.Position).magnitude
				Target = torso
			end
		end
	end
end

while wait(1) do
	local torso = findTarget()
	if torso then
		zombieHumanoid:MoveTo(torso.Position)
	end
end```
1 Like

Youre not returning the target

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

local function findTarget()
	local aggroDistance = 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
			if (zombieTorso.Position - torso.Position).magnitude < aggroDistance then
				aggroDistance = (zombieTorso.Position - torso.Position).magnitude
				Target = torso
			end
		end
	end
return Target
end

while wait(1) do
	local torso = findTarget()
	if torso then
		zombieHumanoid:MoveTo(torso.Position)
	end
end

There’s a few problems with this implementation.

  1. This implementation will only work for R6, as you’re checking for ‘Torso’ when you iterate through the descendants of the workspace.
  2. The FindTarget method doesn’t return anything, so ‘torso’ in the while loop below will always be nil, hence the if condition below won’t ever be true.

Here’s a re-work of the script, it’ll work for R6 and R15 too. :slight_smile:

local zombieTorso = script.Parent.Torso -- Assuming this is an R6 rig.
local zombieHumanoid = script.Parent.Humanoid

local function findTarget()
	local aggroDistance = 100
	local Target = nil

	for i, v in pairs(game.Workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local torso 

        -- We can't assume the player will have an R6 rig (unless you've set the game settings to be otherwise). So we'll check to be on the safe side.
        if human.RigType == Enum.HumanoidRigType.R6 then
            torso = v:FindFirstChild("Torso")
        elseif human.RigType == Enum.HumanoidRigType.R15 then
            torso = v:FindFirstChild("HumanoidRootPart")
        end

		if human and torso and v ~= script.Parent then
			if (zombieTorso.Position - torso.Position).magnitude < aggroDistance then
				aggroDistance = (zombieTorso.Position - torso.Position).magnitude
				Target = torso
			end
		end
	end

    return Target
end

while wait(1) do
	local torso = findTarget()
	if torso then
		zombieHumanoid:MoveTo(torso.Position)
	end
end

Actually, since R6 characters AND, R15 characters have a HumanoidRootPart, you can just use that without an if statement.

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

local function findTarget()
	local aggroDistance = 100
	local Target = nil

	for i, v in pairs(game.Workspace:GetChildren()) do
		local human = v:FindFirstChildWhichIsA("Humanoid")
		local torso = v:FindFirstChild("HumanoidRootPart")

		if human and torso and v ~= script.Parent then
			if (zombieTorso.Position - torso.Position).Magnitude < aggroDistance then
				aggroDistance = (zombieTorso.Position - torso.Position).Magnitude
				Target = torso
			end
		end
	end

	return Target
end

while task.wait(1) do
	local torso = findTarget()
	if torso then
		zombieHumanoid:MoveTo(torso.Position)
	end
end

Plus, you only check if human exists after you do the rig type check, meaning you may get an error thrown.

1 Like

This advice is very correct, nice shout didn’t even notice at all. :slight_smile: