Tower Defense Enemy System Help

I’ve been making a tower defense game, and keep running into the issue of after hitting a certain wave, the amount of enemies starts to cause frame issues, I was hoping there was some solution to this issue, but so far haven’t found one.

I know it’s somewhat possible, since @BelowNatural managed to find a solution for Tower Defense Simulator, but I’ve not managed to figure it out.
I’ve tried things like “Eliminating all humanoids”, but that didn’t do it.

If anyone knows a solution, or if Below somehow ends up seeing this, please let me know, I’ve been trying to find a solution for quite some time now.

Thanks for any and all help :smiley:


I’d imagine that it has to do with how the enemy is scripted. Just rendering the enemy in your game shouldn’t cause issues unless you have a chaotic amount of enemies spawned

Is it possible the script for the enemy is causing the lag? If you disable the script inside the enemies and then spawn in the enemies, is there a noticeable change in the lag?

Currently all the enemies have no scripts within them, a server script handles their movement and the dealing/taking damage, probably due to large numbers, since my TD generates a random path each wave, and the number of enemies is 1*wave*number of entrances (should clarify its 1*wave per entrance)

Weird. If all the enemies are controlled from one server script, there’s only one thing I can think of.

How many events are active in each enemy? After an enemy dies, you should disconnect all events that the enemy is using. You can disconnect them like this:

local connection
connection = enemy.Event:Connect(function()


Hmm I might have forgotten to do that, Lemme rummage through my module and see if I can find them all lol

Ok. I think it more depends on which wave the lag starts to get bad, but if there’s like 5 events in each enemy and there’s close to 1,000 enemies in total after a certain number of rounds, you can see how that would add up quickly :skull:

I appear to disconnect all the connections I make regarding an enemy, so that wasn’t the issue…

Good to check that the events were disconnected, but I have no idea what else it could be
I don’t want to leave you without a reply

I think the problem is probably from the way you resolve the enemies stats and positions. Having an event in every enemy is expensive, yes, however I believe there are more efficient ways you could go about creating your track. For example, assuming enemies follow the same track, you can base each character’s position by the enemy in front of it if it has the same speed (as long as it stays constant).

You might also want to check for memory leaks, because even if you are disconnecting events you may not be properly cleaning up and getting rid of the data. Run some checks with the dev console and benchmark a few things to get an understanding of what’s specifically taking up all the power and that’ll point you in the direction where you can optimise your code. Perhaps you’re updating their movement too often, running expensive events like heartbeat or something like that.