Common laggy code/functions to avoid in a fast looping server script?

My game has many very fast infinite loops that have to exist. The problem is that these loops are responsible for causing the game to be basically unplayable server side. So my question is, what are some common things to avoid in these loops in order to prevent lag?

My looping scripts are often 1000+ lines long, so it’s very hard for me to find out what’s causing the lag.

Organization, events and modules are the key here. Say, for example, you have two scripts which continuously check a value to detect when a round ends(_G.ended). You can have a single module listening for that change, and have it fire some kind of "RoundEnded" bindable event, so the other scripts which rely on it only have to listen for that(Module.RoundEnded.Event:Connect(function()). Also most of the time, loops have event based solutions, for example "RoundEnded" could be a BoolValue so scripts can listen to the .Changed event instead of looping. Lastly, the client is valuable too, it’s a good practice to move any effect related code to the client and have some kind of replication system, visuals aren’t a thing the server should handle.

1 Like

Interesting, I’ll have to try re-writing some of my code.