How do i make a tower script for a tower defense game so it either aims one with the highest value or the lowest value or both

Hi, so uh… im trying to make a tower script, thats my goal.

Is it possible to like have enemies in range filtered so the highest or the lowest gets valued in the TargetValue?

highest what? lowest what?
give more details

highest and lowest value

if you didn’t know

what value?!?!?!?!?!?
dhhdhdhdhdhd

numbervalues :confused:

now you know right?

when you get the enemies in range loop through them and figure out which one has the highest/lower number

for highest you do this

local high = 0
local enemy = nil
for i, v in pairs(enemies)
     if v.NumberValue.Value > high then
          high = v.NumberValue.Value
          enemy = v
     end
end

lowest

local low = 9999999999
local enemy = nil
for i, v in pairs(enemies)
     if v.NumberValue.Value < low then
          low = v.NumberValue.Value
          enemy = v
     end
end

are you SURE, this sorts out?
:sweat_smile:

You should figure out that yourself.

use math.huge instead.
local low = math.huge

This gives me a ‘‘do’’ at the last line of these 2 codes, what do i need to add or erase.

i forgot to put do for the loops

Alr ill fix it now. :sweat_smile: so hopefully it works for now…

hey so somehow it printed lowest the first and the highest last,
if i loop this in a tower script, it will shoot like 5,4,3,2,1 as targets repeatedly,
making look it if it shoots 5 to 1 each shot, how do i fix this?
error

you pick which one to use
if you want the highest one then use the highest loop
if you want the lowest one then use the lowest loop

local high = 0
local enemy = script.Sort
local enemies = workspace.Folder

for i, v in pairs(enemies:GetChildren()) do
	
	if v.NumberValue.Value > high then
		high = v.NumberValue.Value
		print(v.Name)
		enemy.Value = v
	end	
end 

so is this script right or wrong to print only the highest of all?,
this is the same script that printed the 2 prints in the console log.

you put the print in the loop so its gonna print everytime it finds a higher value

but this is not what i meant, i meant like so it targets the one with the highest and thats it stop, if it leaves it in range, then find a new one.
How do i get something like this?

what? that script will pick the enemy with the highest value

i dont understand what you want

then why does it print lowest to highest somehow? i want it to stop on highest not lowest and highest.

because the print is inside the loop

yeah but, objectvalues also count, which im trying make my tower target enemies so they get in one of the towers Target ObjectValues
So if this is the same result just as the print, then it may not be the one im asking help for.