You could try appending the name of each enemy with the number of the last enemy created + 1
like
local enemyNumber = 0
function createEnemy()
enemyNumber +=1
Enemy.Name = "EnemyName_"..enemyNumber
end
Enemy.Died:Connect(function()
-- first = find enemy with lowest higher number
end
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
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.
local numEnemies = 0
CollectionService.InstanceAdded("Enemy"):Connect(function(enemyNPC)
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.
enemyNPC.Humanoid.Died:Conect(function()
CollectionService:RemoveTag("Enemy", enemyNPC)
end
end)
function CreateEnemy()
CollectionService:AddTag("Enemy", enemyNPC)
end
-- run your checks for first/nearest enemy through [in pairs(CollectionService:GetTagged("Enemy"))]
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.