How to make an tower like in Tower Battles?

I am currently trying to make something like Tower Battles, I wonder how to make an tower. I’ve succeed to make it shoot and face the enemy, but it doesn’t shoot the first target. How I could make my tower shoot an first target in the range?

When a lot of enemys spawn, my towers get confused what to shoot, so thats why I want to make an tower to shoot first target.

Here is how it looks if someone wonders:

Here is script for shooting:

local Configuration = script.Parent.Parent.Configuration

function awardPlayers()
	for i, v in pairs(game.Players:GetChildren()) do
		if v:FindFirstChild("leaderstats") then
			if v.leaderstats:FindFirstChild("Money") then
				v.leaderstats.Money.Value = v.leaderstats.Money.Value + 1

while wait() do
	local enemy = game.Workspace.Enemy:GetChildren()
	for i = 1, #enemy do
  		 if game.Workspace.Enemy:FindFirstChild(enemy[i].Name) then
			if game.Workspace.Enemy[enemy[i].Name]:FindFirstChild("Humanoid") then
				if game.Workspace.Enemy[enemy[i].Name]:FindFirstChild("HumanoidRootPart") then
					if (game.Workspace.Enemy[enemy[i].Name].HumanoidRootPart.Position - script.Parent.Position).magnitude < Configuration.Range.Value then
						if Configuration.Can.Value == true then
							Configuration.Can.Value = false
							script.Parent.Parent.Output.ParticleEmitterFX.Enabled = true
							script.Parent.Parent.Output.PointLightFX.Enabled = true
							script.Parent.Parent.Output.SmokeFX.Enabled = true
							script.Parent.Parent.Output.ParticleEmitterFX.Enabled = false
							script.Parent.Parent.Output.PointLightFX.Enabled = false
							script.Parent.Parent.Output.SmokeFX.Enabled = false
							Configuration.Can.Value = true

I want also to tell, it’s not best solution for shooting tower, but this what I could create and yes I know I should put spawn(function()) in the particle and light effects instead of delaying rest of code/script.


Have you considered this method:

  • add all the enemies (in the round) into a single array called Enemies where index 1 is the first enemy (furthest from start) and index n is the most recent enemy spawned
  • loop over all these enemies and find those in range of the tower and store them into NearbyEnemies array

The first object in NearbyEnemies would be the first target in range. If you need a more detailed explanation, feel free to reply. :slight_smile:

1 Like

Could you show me example as code so I can get an some sort of idea of how to apply?

local enemies -- = wherever you store it in other **scripts** (NOT FOLDER), you should not be making an array for every single tower

local nearbyEnemies = {}

for enemy in pairs (enemies) do
     if (tower.base.Position - enemy.base.Position).magnitude < TOWER_RANGE then
         table.insert(nearbyEnemies, enemy)


If you want to shorten the code, you can just shoot on the first iteration of the enemies in the pairs loop because that’ll be the closest in-range enemy.


I have a question about that topic. How would the troop change its first target if an NPC with a higher speed passes the first NPC? I tested the script and it did not change to the faster NPC.

I just found out. You basically do Magnitude from each node and the closest becomes the first. You order them accordingly and done. Though I don’t know how to order all of this in a right way.

1 Like

Could you show me the example as code so that I can get an idea of how to apply it? Would I have to check the distance of each enemy on each node?