How do i make a magnitude script that targets the strongest, weakest in range

So i made a similar post here, but it has some complications.

Anyways to make a magnitude script that only targets in a specified folder so it chooses only the one with the strongest health or the weakest health, making it print the one thats weakest or strongest. how do i do something like this that is magnitudal and counts each humanoid in a folder.
how do i do something like so it chooses the strongest or the weakest, in like a folder magnitude script.

You loop through all the folder’s humanoids and then you store them somewhere like a table to compare them all to eachother. Then you check which one is the weakest and which one is the strongest and you choose the target.

Be sure to store the location of the humanoid too, cause then you can get both the humanoid’s strength and its location (workspace or something like that).

how do i even do that bro. doing something like operators doesn’t work.

Yeah, I admit I don’t know how you would compare them, maybe there’s like a library you can use to sort all of the values?

As @BandQueenForever said, loop through all the Folder with all of the other Folders inside by using :GetDescendants() which will allow you to get every single content inside of the Folder. Then inside the loop, you would make few if statements by checking if there is a Health value inside. I am not sure what kind of logic you want to approach for checking the weakest and strongest Humanoid, but the simplest one would be to just check if Health Value is 100, then that is the strongest Humanoid.

Once you find a Strongest Humanoid, you can use a Magnitude Value between your Character and the NPC. Check if Magnitude Value is below a specified number and then do something.

but how though? operators just don’t work, they just keep choosing randomly.

Which operators are you talking about?

like >=,=<,==,

Operators should be fine, it’s definitely the logic you are approaching that is not being precise.

well, if zombies get in range of the tower, i can’t just like make if below 15 then shoot. no it will shoot randomly, you can’t sort it somehow.

Can you show us the most recently updated code for your Zombie Script?

table.sort() exists precisely for this, unless I am missing out on something major.

while wait(0.3) do
	
		local towertorso = script.Parent.Torso
		local mobs = workspace.Enemies
		local humanoid = script.Parent.Humanoid
		local humanoidnpc = script.Parent.Humanoid
		--local humanim = humanoidnpc:LoadAnimation(script.Parent.fire)
		local function FindFirstTarget()
			local maxDistance = 150
			local nearestTarget = nil

			for i, target in ipairs(mobs:GetChildren()) do
				local distance = (target.HumanoidRootPart.Position - towertorso.Position).Magnitude
				if target.DistanceToWaypoint.Value < 999 and target.MovingValue.Value > 0 then

					towertorso.Parent:SetPrimaryPartCFrame(CFrame.new(towertorso.Parent.PrimaryPart.Position,target.Torso.Position))
					maxDistance = distance
				end
			end

		end

		while true do
			local target = FindFirstTarget()
			local FireSound = Instance.new('Sound')
			local damagenumber = 3
			if target then
				
				
			
	
				-- Do something
			end
				


			task.wait(0.5)
		end


	end

Why do you have a while loop nested inside another while loop?

how do you even sort strong health and weak health throughout a magnitude script that checks a ‘‘Zombies’’ folder?

doesn’t matter the First while loop is domiant.

You can try using table.insert() to insert the specified Humanoid to be shot at. After that you can just loop through the new table of the Specified Humanoids.

Assuming you already have a table of humanoids, you can loop through them and keep setting variables to find which one has the least/most value (then return it). I’d do this:

function SortHumanoids(Humanoids,Mode) -- Mode is either "Least" or "Most"
	local selected = nil
	local healthThreshold

	if Mode == "Least" then
		HealthThreshold = math.huge
		for i,v in pairs(Humanoids) do
			if v.Health < HealthThreshold then
				HealthThreshold = v.Health
				selected = v
			end
		end
	elseif Mode == "Most" then
		HealthThreshold = -math.huge
		for i,v in pairs(Humanoids) do
			if v.Health > HealthThreshold then
				HealthThreshold = v.Health
				selected = v
			end
		end
	end
	return selected
end

Would that work on a loop if a zombie is very close to the tower? or something?

That depends on your logic, all this function does is take an array of humanoids (e.g. {Humanoid, Humanoid, Humanoid}) and gives you which one has the most/least health.