Ways to Prevent Lag & Optimize Your Game

I need some tips on ways to prevent lag. The dos and don’ts. What should I avoid altogether when making a game.
Here are some of the objects that’ll be in my game: Trees, buildings, cars, guns, houses.

12 Likes

Well, you could make an setting that determines the level of quality the game will have. Such as, low, medium, high, etc.

Graphics Quality

Low/Medium/High

Although, I do not really know how to prevent lag. You could try, somehow, making a system where you can also determine how far does your rendering reaches, and the speed things will render.

1 Like

I’ve been researching this topic for a long time now, safe enough to say I have some accurate information.

Building

  • Use Unions for similar objects (repetitive chairs, tables, trees, etc)
  • Avoid using textures in areas where players will not be able to visually see it.
  • Overall, avoid too many parts, I believe there was something called greedy mesh where if you had multiple parts that could be merged it will merge it.
  • Set BasePart and Meshes CollisionFidelity to Hull.
  • Anchor parts that don’t need to be moved, this will remove physic calculations from that part.
  • Transparency will also have an impact on your game
  • Streaming enabled

Scripting

  • Clean up your RBXConnections and instances to avoid memory leaks.
  • Compress code and use more efficient methods/algorithms for certain tasks
  • Use functions and local function and variables
  • Avoid redundant variables (they take up memory)
  • Coroutine over spawn
  • Use loops carefully and well.

Graphical User Interface

  • Don’t just move the Frames off your screen, toggle visibility
  • Clean up after using TweenService if you ever use it

There are plenty of links that explain in more detail:

https://developer.roblox.com/en-us/articles/Improving-Performance

37 Likes

Not to be rude or anything, but aren’t unions performance-heavy? I remember people comparing them with meshes and turns out, meshes are faster

edit: I know how you can turn a simple group of parts into one mesh and reduce lag…? I haven’t tested it but it’s like this (I made a topic about it a long, long time ago, and now I use Cinema4D for meshes and texturing.):

7 Likes

Oh yes, I forgot to mentioned that you should always use Meshes over Unions, it’s fine to be a last minute thing but you shouldn’t make unions as a habit.

Sorry I forgot to mention it. I’ve also read that Unions are better in certain cases where repetitive objects in small quantities are used for example like wooden chairs.

1 Like

How exactly do you clean up after TweenService? And, what exactly do you mean by clean up connections and instances?

This is a reply that I often find help people, see if any of the following matches with your game:

1 Like

When using TweenService to create a tween, it actually creates an object which you can destroy.

local TweenService = game:GetService("TweenService")

local Something = TweenService:Create(Arg1, Arg2, Arg3)
Something:Play()
Something:Destroy() -- clean it up

As for connections and instances, it’s the same thing, you don’t want useless stuff in your game or else it will end up crashing at once point because you have all this useless garbage in your world.

For cleaning up instances, if you clone or create a instance and no longer need it then destroy it. As for a connection, be careful where you put it. A example is if you were to place a InputBegan connection in StarterCharacterScripts, this would create more connections overtime because the old one does not get cleaned up.

local Connection
Connection = RBXConnection(function()
--> If you only want this to work once;
Connection:Disconnect()
Connection = nil
end)
3 Likes

That is a thing I’ve never knew was a thing until now, even the developer hub wiki: https://developer.roblox.com/en-us/api-reference/class/TweenService/index.html
doesnt go into any detail about it or that it event remotely exists other than the functions list, thanks for calling that out :+1:

7 Likes

Would that also apply to loaded animation tracks? Or is that something we should be careful around?

Nope, but for optimization for animation tracks, I suggest having to load all animations upon respawn or character added. Then have a module to return the table of animations.

The benefits of this is that you’re not reloading animations over and over as well and you have more control over your animations because your local scripts are able to access the same animations.

In the end, if it’s already loaded use it.