Get Furthest Zombie

Hello, how would I get a zombie furthest on a given path, for instance I want to basically make an object target the further / first zombie on the path. I’ve tried to do it myself but the code just gets the zombie with the longest amount of time on the path. Here is my current code:

                repeat
					task.wait()
					local PosNum = (obj:GetAttribute("Last") - Vector3.new(obj:FindFirstChild("HumanoidRootPart").Position.X, workspace:WaitForChild("GameItems").Spawn.Spawn.Position.Y, obj:FindFirstChild("HumanoidRootPart").Position.Z))
					obj:SetAttribute("Position", PosNum.Magnitude)
					obj:SetAttribute("Last", PosNum)
					
					obj:WaitForChild("Humanoid", 3):MoveTo(Vector3.new(waypoints[waypoint].Position.X, obj.HumanoidRootPart.Position.Y, waypoints[waypoint].Position.Z))
				until (obj.HumanoidRootPart.Position - Vector3.new(waypoints[waypoint].Position.X, obj.HumanoidRootPart.Position.Y, waypoints[waypoint].Position.Z)).Magnitude <= 1

Get the magnitude from the position, then subtract it from the part you are checking. Save all the results in a table and then check which one is the closest one (Note use math.abs(result) so that the result is positive)

2 Likes

But how can it be a negative number?

1 Like

That doesn’t work, it gives me a varied range of values

1 Like

Magnitude minus magnitude, the part’s magnitude might be bigger

1 Like

? If you have a table with each result and then get the smallest one it can only be 1 value. Or what do you mean?

You can sort the table with table.sort(table, function(a, b) return a > b end)

1 Like

the result goes from like 60 to 17 back to 60 etc. Everytime it updates

1 Like

Can you show me your code? Text because I can’t send a message less than 30

task.spawn(function()
			while task.wait() do
				local PosNum = (Vector3.new(obj:FindFirstChild("HumanoidRootPart").Position.X, workspace:WaitForChild("GameItems").Spawn.Spawn.Position.Y, obj:FindFirstChild("HumanoidRootPart").Position.Z).Magnitude - obj:GetAttribute("Position"))
				obj:SetAttribute("Position", PosNum)				
			end
		end)
task.spawn(function()
	while task.wait() do
		local PosNum = math.abs(obj:FindFirstChild("HumanoidRootPart").Position.Magnitude - obj:GetAttribute("Position").Magnitude)
		obj:SetAttribute("Position", PosNum)				
	end
end)

i added math.abs() but it still does the same thing

You are doing it wrong. It should look like (Pos1 - Pos2).Magnitude

1 Like

that is what i tried the first time

Oh I completely forgot, yes

task.spawn(function()
	while task.wait() do
		local PosNum = (obj:FindFirstChild("HumanoidRootPart").Position - obj:GetAttribute("Position")).Magnitude
		obj:SetAttribute("Position", PosNum)				
	end
end)

Wait, why are you using an attribute to get the position. The other position should be the zombie position, not the result. The result is the distance

Because i have a seperate script for the object that targets

for i, target in pairs(workspace:WaitForChild("GameItems").Mobs:GetChildren()) do
			local distance
			
			if Tower then
				distance = (target.HumanoidRootPart.Position - Tower.PrimaryPart.Position).Magnitude
			end

			if distance < maxDistance then

				table.insert(targetList, target)			

			end
		end

		for _, target in pairs(targetList) do
			
			
			if not firstTarget then
			if target:GetAttribute("Hidden") and data.HiddenDetection or not target:GetAttribute("Hidden") then
					firstTarget = target
				else
					continue
				end
			else
			if target:GetAttribute("Hidden") and data.HiddenDetection or not target:GetAttribute("Hidden") then
					if firstTarget:GetAttribute("Position") < target:GetAttribute("Position") then
						firstTarget = target
					end
				else
					continue
				end
			end
			

		end

Is obj the zombie or the player?

it is the zombie. Text because 30 limit