Reducing lag with loop scripts?

I own a tower defense game in which users place towers to defend against zombies. All of the towers have a script containing a while loop to detect nearby zombies and waits a range of 2 - 0.05 seconds. After around 20 - 30 of these troops are placed, the game begins to experience lag, causing delays where all scripts seem to slow down or completely stop for half a second. Any idea on how I can fix this or reduce the lag?

6 Likes

Instead of having several concurrent scripts running at the same time, perhaps you should have a single, master script running in ServerScriptService that controls the actions of all of them. You could loop through Workspace to find the towers you need.

If you really need the random towers, you could do something along the lines of waiting a random amount of time then setting off a random half of the towers, or something of the sort.

Having too many threads running at the same time tends to slow down a game a whole lot.

4 Likes

Try using a array to save all of the delays. Then, instead of using 20 while loops, you can combine them into one.

Your method will really optimize it! I also recommend for him to use for _,v in ipairs instead of pairs to optimize the loop. And before checking for each item’s magnitude, it would first check for the name of Zombie and then check its PrimaryPart Magnitude with the tower! Because if you have a large map, it can really impact those loops!! I hope this helped! A second method(it will never lag like this but you will sacrifice security) is to make those loops onto the client and then send them with a FireServer event!!(keep in mind expert and skilled exploiters can exploit such! except if you somehow implement a really good method of secondary security!)

1 Like

Is ipairs faster than pairs? Also, since everything is handled on the server, do you think it would still lag if implement his suggestion, have the main script update an int value in the troop, then have the troop’s script check if it changes and handle the rest? It doesn’t seem like the most efficient way but it would be easier than completely remaking my troop system.

1 Like

Actually, pairs is faster than ipairs. @Crazyman32 did some experimenting and found this:

2 Likes

for the video, I’d see that there is too much physics occurring on your server. You can change how physics is handled by using :SetOwnerShip() for the part that is moving on the client. It would be mostly safe as this would be only physics but it would kinda impact the client’s performance(if the client has a really weak device) Network Ownership | Documentation - Roblox Creator Hub
for an explanation on how to use it! As for making it on the client, the tables would loop inside a localscript and would fire to server(remote events will be throttled a lot but better than nothing and it would be hard to exploit!)
As for the table loops I am not really sure about it I just heard that before. The difference of them would be small though and wouldn’t really impact! So use pairs I guess.

1 Like