this is a tutorial on how to improve performance for your games.
-
Use CFrames instead of Constraints or tweens
if you have doors, Drawers or any other movers, its recommanded to use CFrames as the Movers are anchored. -
Attributes vs Values
Always Use Attributes, they are newer and more performant than values.
The only time to use a Value is for Object values because there are no Object Attributes yet. -
Part Vs MeshParts
Overall meshparts are better than parts when it comes to performance and the reason for that is because 1 mesh part can replace a model that has dozens of parts.
Remember to Re-use the same MeshPart id for all the same looking meshes in game(use the same mesh part for all the tree mesh parts in game).
when to use MeshParts:
-
Use when you have a model(Like a tree) you want to place it multiple times in the map. instead of a tree having ~30 parts you cant replace the parts with 1 or 2 mesh parts that would reduce the amount of instances in your game hence its going to run faster.
-
Another use case is if you want to have a smooth and more good looking models like a house/map then you could remove the inner unseen faces that exist between two parts. with these you could save a lot of tri counts
when not to use MeshPart
Some games might require a lot of unique mesh ids because they must look different, in this case use the primitive shapes as loading too many id MeshParts can cause loading and disconnection issues on low internet devices, but a lot of mesh part ids wont decrease the FPS since FPS is dropping because of high amount of tris and instances and not because of id load.
- Use Hashed table unless you have to use array tables
for those who dont know a hashed table is the primitive way of storing variables inside tables.
this is what a hash table looks like:
local MyTable = {}
MyTable[1] = workspace.Part
Uses like table.insert, table.find can be replace by:
MyTable["indexIwantTofindOrAdd"] = workspace.Part
-
Clone object vs Instance.FromExisting()
You dont have to use Clone function if you want to copy a single instance without its children or without children just use Instance.FromExisting(). the clone function goes as:
for i,v in ipairs(object:GetDescendants()) do
local des = Instance.fromExisting(v)
--then assign the part to its relative parent and so on
end
which will load the game slower if you clone large objects.
6 Use the most performant part properties unless you have to use something else
The most performant part properties are:
-
CanCollide, CanQuery, CanTouch all set to false.
-
Anchored set to true
-
Mass set to massless
-
Transparency set to 0 or 1
(same thing goes to meshparts of section 3)
7 Dont replicate unless you have to
A lot of replications might cause more letancy and lag. That exists in any game but less of it means less communication between client and server.
8 Dont use Debris Service
Instead of using Debris service you should use .Destroy with task defer to destroy an object
here is an example:
task.delay(1,function()
game.destroy(workspace.Baseplate)
end)
-
Use event:Connect instead of task.wait or any while wait unless you have to
if your code should fire when something changes or when something happened you check it when a property has changed instead of checking it all the time.
10 Use loops, for loops only when needed to
i see a lot of people use for loops for unneeded things. one of them is pathfinding.
You dont need to use for loops to get all the waypoints every time. Instead use an index or get the third waypoints and move to it only.
Humanoid.MoveTo(path:GetWaypoints()[3].Position
-
*Try to have a reasonable amount of scripts in your game
if you are planning to give each door or npc a script that will cause a lot of performance issues and FPS drop.
Instead make 1 script that controls all doors, npcs or anything that comes in between. Code your class as a hivemind, giving general conditions of what they should do on each condition. -
use fire,smoke, Particles as less as possible on the same time
Too many particles, smoke, or fire can cause a lot of lag and fps drop. When a camera is close to these the game starts to slow down for a reason.
13 Devide your map by chunks
Loading an entire large map will require a lot of instances to be inserted, instead devide the map by chunks and make the irrelevant chunks(far away or not part of the current story) hidden in replicated storage and load it for each player. there the player wont see it and it if they dont need to and that will increase performance.
14 Keep your map close to the origin(0,0,0)
if you are planning to make a game where you travel large distances by thousands or more than that, the calculations will start to bug out this is called floating point error because the number of digits is bigger to the point it wont calculate the decimals. so that cause the game to look glitched.
-
Dont use Humanoids on NPCs
Try to have less players in a game that the players will play it(a lobby can have a larger amount of players but try to limit it in game) because each player has a humanoid.
if your game has a skinned mesh creature, grass, water, or any other type of thing that dont need humanoid then dont use it, Humanoids are very heavy on performance and a lot of them will cause slower FPS in your game.
If you have a creature that roams around, skinned mesh grass, water, a boss npc(like bedwars monarch and golem), or a minion npcs You dont have to use humanoids, you can use custom variables and states to give them health, states and so on. Use AnimationController to manage the animations. They are used to replace Humanoids on NPCs
-
Too many textures can cause loading issues and disconnections for weak internet devices
Dont use too many texture or Decals. Use the built in materials or generate a new material with the material generator.
17 On custom Connection events use : Disconnect()
when firing an event disconnect() it to prevent memory leaks.