Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

The Magnitude script is not working, got an error?

Hello, im a new guy here and this is my first post here and i have a question,i followed TheDevKing's Magnitude script tutorial and when i finished the script,i tried to test it but it said Workspace.magnitudescript:23: attempt to perform arithmetic(sub) on nil and Vector3

Heres the script

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

local function findTarget()
	local agroDistance = 100
	local hitDistance = 6
    local target = nil
	for _, v in pairs(game.Workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("HumanoidRootPart")
		local npc = v:FindFirstChild("ZombieNPC")
		if human and torso and v ~= npc then
			if (zombieTorso.Position - torso.Position).magnitude < agroDistance then
				agroDistance = (zombieTorso.Position - torso.Position).magnitude
				target = torso
				if (zombieTorso.Position - torso.Position).magnitude <= hitDistance then
					human.Health = human.Health - 10
				end
			end
		end
    end
	return target
end

while wait(1) do
	local torso = findTarget()
	if torso then
		zombieHumanoid:MoveTo(torso.Position)
	else
		zombieHumanoid:MoveTo(zombieTorso.Position + Vector3.new(math.random(-50, 50), 0, math.random(-50, 50)))
	end
end

I`m sorry if you didnt understand well the question, english is not my primary language(also you can tell about any other mistakes i did,i wanted to also make that the target will take damage)

1 answer

✓ Accepted answer

In line 11-12 you’re trying to find “ZombieNPC” inside the player’s character, which doesn’t exist. What you will do is to remove the ZombieNPC variable and check if the player’s character ~= script.Parent.

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

local function findTarget()
    local agroDistance = 100
    local hitDistance = 6
    local target = nil
    for _, v in pairs(game.Workspace:GetChildren()) do
        local human = v:FindFirstChild("Humanoid")
        local torso = v:FindFirstChild("HumanoidRootPart")
        if human and torso and v ~= script.Parent then
            if (zombieTorso.Position - torso.Position).magnitude < agroDistance then
                agroDistance = (zombieTorso.Position - torso.Position).magnitude
                target = torso
                if (zombieTorso.Position - torso.Position).magnitude <= hitDistance then
                    human.Health = human.Health - 10
                end
            end
        end
    end
    return target
end

while wait(1) do
    local torso = findTarget()
    if torso then
        zombieHumanoid:MoveTo(torso.Position)
    else
        zombieHumanoid:MoveTo(zombieTorso.Position + Vector3.new(math.random(-50, 50), 0, math.random(-50, 50)))
    end
end

1 comment

0

Thanks alot! This worked, i will come here if i have any more issues

This discussion is preserved as it was. New answers, comments, and votes are closed.