Help with Tower Defense "First" Targeting

Hi, i need help with making a “First” Targeting like tower battles. I’ve found many methods but it’s either isn’t really accurate or not informative.

function findPossibleTarget()
	NearbyEnemies = {}
for _, Enemy in pairs(Enemies:GetChildren()) do
	if Enemy:IsA("Model") then
			if Enemy:FindFirstChild("HumanoidRootPart") then
				if (Humrp.Position - Enemy.Torso.Position).magnitude < _Range.Value then
				       table.insert(NearbyEnemies,Enemy)
						findPrimaryTarget(NearbyEnemies[1])
				end
			end
		end
	end				
end

while it does attack the first enemy, it only targets the first enemy that spawned in first, so if a faster enemy have passed an enemy, the tower will not target the faster enemy but only target the other enemy until it dies.

1 Like

You may want to consider manually inserting the mobs in order instead of using a loop.

how do i insert it in order, because even if i insert it, it will automatically
rearrange the table based on when the enemy spawned

1 Like

Not sure how your tower defense system works but couldn’t you just check the distance between the present enemies and the exit and have the towers shoot at the enemies closest to the exit (providing they are within the range of the tower)?

1 Like

You can add a table and every time a tower is added, it’ll add the tower instance into the table. Loop through the table and attack each one.

many people have suggested me this, but types of maps/path can really mess it up

i don’t quite understand what you mean by that, do you mean every time an enemy is added?, i’m not quite sure what to work with this, because i want it to target the first enemy not attack one by one.

I would create a custom Tower Class using a module script and wrap the tower object with the class. Then each instance of the tower would have a method that finds the closest target within (that towers) range and attacks that target until it dies, then locates the next closest target. Give each tower a non-collidable part to use as a detection zone that way it only has to search through the targets within its zone to find the closest target. If you want to always switch to whichever target is closest regardless if the current target is dead then put the check on a loop so that it find the closest target before each time it fires.

i am asking on how to make a “first” targeting not closest target(sorry if that sounded rude)

Then use the detection zone to capture the targets as they enter, then you know which target came before another. Every time a target enters the zone add them to the end of the list.

but what if an enemy enters first but a faster one enters second, it would just keep attacking the first one the enters until it dies to attack the second one.

What exactly do you want it to do?

attack the “first” target like in tower battles, basically the furthest in the path, for example a faster enemy have passed a slow enemy so the tower will switch target from the slow enemy to the faster one instead because it is now the furthest in the path, same applies to even faster enemies.

So that’s not first, that’s distance based. First implies time. I don’t know what Tower Battles is so you’ll have to excuse my ignorance in that regard.

Perhaps you meaning that you want each tower to attack whichever target is closest to a particular goal? You said something about a path, so I assume maybe targets are all going through a gauntlet? If thats the case then you talking distance from goal as the primary target.

because some maps can mess it up and it would not be accurate sometimes , for example, there would be a curve down then up, or circles path

I gotcha… You can overcome that problem by sectioning off the pathways. Give each pathway an increasing number, higher number means targets in that path are closer. Then target the enemy closest to the goal that’s within the closest path section.

that’s how they move around at the moment, but if multiples enemies are in the same path, like path 7, then their path value would all be 7 so the tower would be confused on who to shoot.

Thats why you use the path number to tell the tower which pathways are highest priority, then also check which player is closest to the end of the path or closest to the goal.

Path 7, enemy1 dist to goal smallest – Target this enemy

I would use non-collide parts for this, since you can reduce processing by only having to check enemies within that highest priority zone. And if the tower has limited range, make sure you also check the enemies range to that tower. You could have a tower also ignore zones that are not within its range.

got it, i’ll try if this works