Targeting "first" is acting weird

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to make a tower defense targeting system

  2. What is the issue? It sometimes doesn’t target the first enemy in line

  3. What solutions have you tried so far? I’ve looked everywhere but didn’t find anything and I have tried debugging it on my own

Here is the targeting system code: (its a module)

Closed rn

The radius detection is inf right now
Here is a video of the issue:


You could try appending the name of each enemy with the number of the last enemy created + 1


local enemyNumber = 0

function createEnemy()
    enemyNumber +=1
    Enemy.Name = "EnemyName_"..enemyNumber

    -- first = find enemy with lowest higher number

Yes, but as I have different mobs with different walkspeeds this wont work

1 Like

Can’t you make an enemy template? I don’t understand.

Just add a tag to the enemies and use CollectionService.InstanceAdded to name each tagged enemy

So I have a tower that requires a module where it checks what enemy is the farthest in the waypoints so if it is the first enemy it checked it and its in range, it would apply the locals to the mobs pos etc, so lets say there is a mob at waypoint 1 and its distance is halfway to the second one, then the distance would be around 5 if the waypoint was 10 studs apart, and as its the first enemy the module checked it set the stats of mob, bestwaypoint, bestdistance to its stats and if the second enemy is ahead of it in a waypoint then it would over write it, but if the waypoint is the same it checks for the distance if its less (closer to next waypoint) then it sets the stats to the mobs stats.

I’ve never used CollectionService.InstanceAdded so im not sure what to do but i already have a collision set up so the mobs can go threw each other and cant touch the players or towers

Oh wait im stupid i thought it said collision service

Also im not sure if it will fix the problem

CollectionService can let you run the same script on a whole bunch of objects at once.

Like instead of putting a script inside every lava part, just use


CollectionService:AddTag(lavaPart, "Lava")

Oh, but how would that help me?

Cycle through the list CollectionService:GetTagged(“Enemy”) for the first/nearest enemy every time it needs updating, instead of sending in connections and constant checks all over the place.

Im not sure how I’ll install that to the module but if you have an idea or a code on how can you please provide it? Ill try doing it myself but I am 99% sure I wont be able to.

1 Like

I wasen’t able to… i just attracted more bugs lol

local numEnemies = 0
    numEnemies += 1
    -- check which NPC type it is and assign its name with the number of enemies
    -- apply its special Enemy Type functions or modules here.
        CollectionService:RemoveTag("Enemy", enemyNPC)

function CreateEnemy()
    CollectionService:AddTag("Enemy", enemyNPC)

-- run your checks for first/nearest enemy through [in pairs(CollectionService:GetTagged("Enemy"))]

If you have probems with CollectionService, use a Tag Editor plugin. It makes it way easier to actually see and fetch tags.

Ok hold on ima try doing smt with this

Using a Tag Editor can add a tag to CollectionService without needing to create one in the script to start with lol.

The tag can exist in the service and not the part, or on the part and not in the service, so you should create the tag in the service first

Wait I just realized how would this help with the targeting, it would be the same if i made this check on each mob.

1 Like

Having the check on each mob would slow down the server because of all the different scripts running at once. Using a connection achieves the same outcome, but doesn’t need a whole bunch of instances to be running copies of the same code.