Pointers to reduce lag in your game

Have you had a laggy game? Does your game lag for others as well?

This problem can be caused by different components in your game and I will be going through and teaching you about what might those causes be and how you can fix them to make your game run faster!

Causes

Some causes of why your game may potentially run slower or lag is because:

  • There are loads of parts in your game (Some parts may overlap and lag).

  • Mesh cars/meshes can lag your game.

  • Scripts can lag your game (doing wait() or creating a loop can seriously damage your game).

  • Harmful plugins can inject viruses into your game and scripts. Some root causes of viruses entering your game could include code, free models and plugins.

  • Overlapping parts/code can interfere with the rest of the scripts in the game and cause lag.

Solutions

While some causes listed are hard to fix, there are some which are fairly easy to overcome.

  • You can try and reduce the amount of parts in your game. This could include grouping all the parts, removing unnecessary parts or include less parts.

  • Meshes are a very big cause to lag in a lot of games. One way to avoid lag is to try and limit the amount of meshes you are using in your game. You can try and make one whole mesh car without huge details. One example could be the cars in ER:LC. They contain one mesh model (with no inside detail) and makes the game less laggy for everyone.

  • Scripts that contain wait() will often wait forever or maybe just lag the entire game and the rest of the scripts. Avoid doing this. You can use wait(1) or insert a number in the brackets to tell the script how long to wait. Adding something like wait(0.1) won’t do much as it’s practically the same as adding wait(). You can find more information in this awesome and information post: Avoiding wait() and why.

@LucasMZ_RBX has a great explanation of the wait() topic. Lucas’ reply

  • If you are trying to loop through something or creating a general loop, make sure to ALWAYS add a wait(#) (# meaning number more than 0.5 at least) at the end of the loop or even breaking it (`break; / return;) . Not adding a wait and letting it loop will certainly create a lot of lag for everyone.

  • Viruses can indeed cause severe lag and damage your game. You can avoid viruses by the source and not install any plugins that are not highly known or made by a verified developer. Try to make everything in your game yourself and avoid using free models. In SOME cases, free models are needed such as lights, seats, tables or other small props but you should try and make everything in your game your own.

  • Overlapping code can also interfere with other codes and cause a load of traffic in your game. No, not the car traffic your thinking of, but code traffic. If one code is stalling and won’t run, the rest of the scripts linked to your code won’t run either.

Conclusion

Doing or trying some of these solutions can make your game run without or without some lag.

Try and see if it’s your game lagging and not your own computer. If it’s your computer being slow and lagging, then try and remove unnecessary files or cache. You can read this article by ROBLOX that explains computer lag and how you can remove it: How to Reduce Lag and Speed Up Play

20 Likes

Not as long as they aren’t used excessively.

Unless you are using while / repeat loops with expensive code inside them; it’s not going to be to big affect.

Most of the solutions listed require major game changes, and some are common sense.

4 Likes

I’ve never had a issue with wait() being laggy. I don’t know where the hell you’re getting this from.

3 Likes

I would just never use wait with small values anyways

1 Like

I’ve used while watit() loops with the default interval for all of the time I’ve known Luau and I haven’t had a single issue with it.

2 Likes

Overall great resource, some of it is sorta well known but still, great topic. I do think you are incorrect about your statement on meshparts.

While yes this is correct, meshes are most likely a big cause of lag in most games, the solution isn’t to just stop using them and/or limiting yourself.

When used correctly, meshes can actually increase performance and rendering speeds. You’re supposed to use meshes when you’re reusing the similar (same mesh id) object over and over again or you’re trying to eliminate triangles.

To explain similar mesh ids, pretend you’re making a game and you have 100 trees. If every tree trunk has different mesh ids it will cause your game to take longer to load, especially depending on internet as when you load into the game your device has to download these meshes. If every tree trunk had the same mesh id, loading times would be faster as the engine would see that they’re similar and download one mesh id then reuse it for the other 99 trees.

To explain removing triangles, it’s as simple as it sounds. Triangles cause framerate lag. The more triangles on screen, the more math and computing your device has to do. When your device starts to overloaded with triangles (depends on your device) it will start to slow down and decrease your framerate, or frames per second (FPS). It might be beneficial to some developers to create complex objects in Blender instead of Roblox because in Blender you can reduce triangles and help with rendering speeds.

Adding onto this, you can make a low poly car and apply smooth shading (in Blender). The car mesh will appear high tris even though it isn’t just because of the illusion that smooth shading gives off.

4 Likes

I heard of people rendering game play how is that usually done?

1 Like

im pretty sure wait() waits 0.30 seconds so instead of 0.1 inside it (0.1 waits 10 milliseconds) you could do 0.01 which waits 1 millisecond

2 Likes

Doing wait(.01) won’t have a different result than wait().

Every “wait for certain amount of seconds” function works by checking every frame, and sees if those seconds have passed.

With Roblox’s wait, it checks that ~0.03 (1 / 30) seconds, which on 60 FPS means that it’s checking that it’s 2x less than every frame, and it gets worse as the more frames you’re able to render, as that difference gets more impactful, and it will lag behind more and more.

There are apparently some other issues with wait just being glitchy, weird apparently too, not only that.

Anyhow, most of the time you don’t NEED to use wait.

Is it a question of changing / doing something everytime?

  • Use RunService events like RenderStepped, Stepped, Heartbeat and connect a function which does what you want to it.

Is it a question of having to check every time to see if something is true / if there’s something somewhere?

That’s called polling, which is like asking the computer every time “Are we there yet?”

  • You can use a custom Signal module (to create custom events) which allows you to use :Wait() which just yields the current thread until that event is fired, for example.

If you ABSOLUTELY need to use a Wait function, then this is at least decent compared to Roblox’s.

local RunService = game:GetService("RunService")

local function Wait(n)
    n = typeof(n) == 'number' and n or 0

    local spent = 0
    repeat
        spent += RunService.Heartbeat:Wait()
    until spent >= n

    return spent, os.clock()
end
4 Likes