How do i make this script detect if target has highest value and lowest value?

So uh, my tower script just shoots randomly, my zombies have 2 targets that indicate a value, so how do i make it that targets near must have some 2 values, and one of them must be the highest and the other must be the lowest, so it can shoot whats first.

while true do
	if script.Parent.LVL.Value == 0 then
		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 firerate = script.Parent.Data.LVL_0.Firerate_Cooldown.Value
		local function FindFirstTarget()
			local maxDistance = script.Parent.Distance.Size.Z/1.85
			local nearestTarget = nil

			for i, target in ipairs(mobs:GetChildren()) do
				local distance = (target.HumanoidRootPart.Position - towertorso.Position).Magnitude
				if distance < maxDistance then

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

			return nearestTarget
		end
		
		while true do
			local target = FindFirstTarget()
			local FireSound = Instance.new('Sound')
			local damagenumber = 3
			if target then
				target.Zombie:TakeDamage(2)
				FireSound.PlaybackSpeed = 1
				FireSound.SoundId = "rbxassetid://7241925700"
				FireSound.Parent = script.Parent.Torso
				FireSound.Volume = 2
				FireSound.TimePosition = 0
				FireSound:Play()
				humanim:Play()
				print(damagenumber, target.Name)

			end

			wait(firerate)
		end


	end
	end

Just compare them to eachother and check what’s the highest value
Like this:


local value1 = 16
local value2 = 32

if value1 < value2 then
    --code
else
    --code
end

but its gonna be a magnitude range script where it has zombie in a folder, it will not do something and will keep doing random target, so what i mean is i want my script to shoot the target if it has Magnitude value very low because its know its close to a waypoint, and also the higher the waypoint means its first.

So, it will not work if its something like this.

I think you could approach this problem by looping through all the characters of the targets and then for every one of them check how close they are to the gun using magnitude.

One question, does this choose the smallest?

I think you’re suggesting a priority check? You can do

if (distance < maxDistance or (distance <= startDistance and priority1 > maxPriority)) and priority1 >= maxPriority then

Basically what this will do is make it where if the target has a higher priority value on them (rather it could be health, damage, whatever you want to make it) it will be favored over targets that may be closer in range with lower priority.

Maybe thats it!, if the target is near and has that and it will shoot that, thats actually a good idea, but how do i make it for the lower one though?

If you’re talking about if the target has a lower priority? You can just switch the operator symbols (</>) when checking for priority.

If you’re talking about targetting people that is furthest away from the statue while in range of the statue, just check if the distance is > maxDistance and set the MaxDistance to 0. You’ll need to make sure to keep a startDistance/originalDistance value and check that to make sure the statue doesn’t target units outside of it’s range.

also uh, the values are confusing me though. the values just like unorganized.