LB Performance - A simple module that optimize your game performance

LB Performance
Roblox package

Introduction
LB Performance is a module that will optimize your game performance. I spent around a few hours reading lots of different posts in order to create this module. It’s my first time posting a module I made in the dev forum. If you have any questions, feel free to comment below.

Code

-- // FileName: LBPerformance.lua
-- // Written by: LingBlack87661
-- // Description: A simple module that optimize the game performance.

-- < Services > --
local RS2: RunService = game:GetService("RunService")
local LS: Lighting = game:GetService("Lighting")

-- < Variables > --
local LBPerformance: table = {}; -- Module itself
local LowPolySetting: table = { -- the setting of low poly
    BasePart = {
        -- [Feel free to change this part to what you want the parts to be] --
        Material = Enum.Material.SmoothPlastic, -- Personally think that `SmoothPlastic` is better than other materials in performance
        CastShadow = false, -- Small boost since it won't get casts a shadow
    },

    MeshPart = {
        -- [Feel free to change this part to what you want the mesh parts to be] --
        Material = Enum.Material.SmoothPlastic, -- Personally think that `SmoothPlastic` is better than other materials in performance
        CastShadow = false, -- Small boost since it won't get casts a shadow
        RenderFidelity = Enum.RenderFidelity.Automatic, -- Reduce overall game performance by decreasing the details of mesh part
        CollisionFidelity = Enum.CollisionFidelity.Box, -- Change the collision models behave to a box. Less works on client
    },
};
local LightingSetting: table = {
    GlobalShadows = false, -- Shadows are not drawn and no distinction is made between indoor and outdoor areas.
    -- Technology = Enum.Technology.Voxel, -- Rendering less shadows (After testing, I found out that we cannot change `Technology` to Voxel in game)
};
local StatesLibrary: table = { -- A state library for LBPerformance.DisableHumanoidStates
    FallingDown = Enum.HumanoidStateType.FallingDown,
    Ragdoll = Enum.HumanoidStateType.Ragdoll,
    GettingUp = Enum.HumanoidStateType.GettingUp,
    Jumping = Enum.HumanoidStateType.Jumping,
    Swimming = Enum.HumanoidStateType.Swimming,
    Freefall = Enum.HumanoidStateType.Freefall,
    Flying = Enum.HumanoidStateType.Flying,
    Landed = Enum.HumanoidStateType.Landed,
    Running = Enum.HumanoidStateType.Running,
    RunningNoPhysics = Enum.HumanoidStateType.RunningNoPhysics,
    StrafingNoPhysics = Enum.HumanoidStateType.StrafingNoPhysics,
    Climbing = Enum.HumanoidStateType.Climbing,
    Seated = Enum.HumanoidStateType.Seated,
    PlatformStanding = Enum.HumanoidStateType.PlatformStanding,
    Dead = Enum.HumanoidStateType.Dead,
    Physics = Enum.HumanoidStateType.Physics,
    None = Enum.HumanoidStateType.None,
};

-- [Strings] --
-- Strings are expensive in Luau
local BasePart: string = "BasePart"
local MeshPart: string = "MeshPart"
local Texture: string = "Texture"
local Decal: string = "Decal"

-- [Assigning properties to a instance] --
local function ChangeProperties(Object: Instance, Information: table)
    -- [Loop the `Information` table and assign property to the object] --
    for Property, Value in next, Information do
        Object[Property] = Value
    end
end

-- [Optimize the lighting in game] --
function LBPerformance.OptimizeLighting()
    if RS2:IsClient() then -- Check is the function called on client
        ChangeProperties(LS, LightingSetting) -- change the properties of Lighting
    end
end

-- [Low poly function] --
function LBPerformance.LowPoly(...: Instance)
    if RS2:IsClient() then -- Check is the function called on client
        local ParentArray: {Instance} = {...}; -- Saving all arguments in the table as an array
        for index = 1, #ParentArray do -- Based on the array to loop the codes inside the scope
            local Parent: Instance = ParentArray[index] -- Get the instance in the array
            for _, Object in pairs(Parent:GetDescendants()) do -- A loop to get everything inside the instance
                if Object:IsA(BasePart) then -- Check up on the class
                    ChangeProperties(Object, LowPolySetting[BasePart]); -- change the properties of base part
                elseif Object:IsA(MeshPart) then -- Check up on the class
                    ChangeProperties(Object, LowPolySetting[MeshPart]); -- change the properties of base part
                elseif Object:IsA(Decal) or Object:IsA(Texture) then -- Check up on the class
                    -- [Feel free to change this part to what you want the textures and decals to be] --
                    Object:Destroy() -- Destroy the decals and textures to optimize the performance on client
                end
            end
        end
    end
end

-- [Disable humanoid state type easily to optimize the game performance] --
function LBPerformance.DisableHumanoidStates(hum: Humanoid, ...: any)
    local StatesArray: {string} = {...};
    for _, State in pairs(StatesArray) do
        hum:SetStateEnabled(StatesLibrary[State], false)
    end
end

-- [Optimize Particle:Emit()] --
function LBPerformance.Emit(Particle: ParticleEmitter, Amount: IntValue)
	if RS2:IsClient() then -- Check is the function called on client
		local SavedQualityLevel: Enum = UserSettings().GameSettings.SavedQualityLevel -- Get the user graphics quality level that set by the client.
        Amount = Amount or 16 -- Default is 16
		if SavedQualityLevel == Enum.SavedQualitySetting.Automatic then -- A check up on SavedQualityLevel
			local Compressor: IntValue = 1/2 -- Basically equal to 5
			Particle:Emit(Amount * Compressor) -- Basically amount divided by 2 and emit it
		else -- if not Automatic
			local Compressor: IntValue = SavedQualityLevel.Value/10 -- Get the exact number of SavedQualityLevel and divide by 10 which is the maxium of the SavedQualityLevel
			if Particle then -- a little check up on the particle itself
				Particle:Emit(Amount * Compressor) -- Emit it
			end
		end
	end
end

-- [Fully using all the LBPerformance functions to optimize the game] --
function LBPerformance:FullOptimize(Information: table)
    local LowPoly: table = Information.LowPoly or {}; -- Basically, a table that store the arguments of LBPerformance.LowPoly
    local DisableHumanoidStates: table = Information.DisableHumanoidStates or {}; -- Basically, a table that store the arguments of LBPerformance.DisableHumanoidStates
    self.LowPoly(unpack(LowPoly)) -- Call the LBPerformance.LowPoly
    self.OptimizeLighting() -- Call the LBPerformance.OptimizeLighting
    if DisableHumanoidStates[1] then -- If the first argument of the `DisableHumanoidStates` does exist, the codes in the scope will run
        self.DisableHumanoidStates(unpack(DisableHumanoidStates)) -- Call the LBPerformance.DisableHumanoidStates
    end
end

return LBPerformance -- eof

API

local LBPerformance: {
    DisableHumanoidStates: function,
    Emit: function,
    FullOptimize: function,
    LowPoly: function,
    OptimizeLighting: function,
};

LBPerformance.OptimizeLighting() [Client only]
Basically, it turns off the Lighting.GlobalShadows. The shadows won’t be drawn and no distinction is made between indoor and outdoor areas. You can customize the LightingSetting to change and add more properties.

LBPerformance.LowPoly(...: Instance) [Client only]
...: Instance → The instance descendants will become low poly.
You can customize the BasePart and MeshPart via the LowPolySetting. You can also customize the Decal and Texture via the LBPerformance.LowPoly(...: Instance) function.

LBPerformance.DisableHumanoidStates(hum: Humanoid, ...: any) [Client and Server]
hum: Humanoid → Humanoid
...: any → The name of the HumanoidStateType in string
Disabling unused HumanoidStateType will improve your game performance.

LBPerformance.Emit(Particle: ParticleEmitter, Amount: IntValue) [Client only]
Particle: ParticleEmitter → The particle that you want to emit.
Amount → The number of particles to emit (Default = 16) [Optional]
It’s similar to the ParticleEmitter:Emit but it’s an optimized version that considers your SavedQualityLevel

LBPerformance:FullOptimize(Information: table) [Client only]

local Information: {
    DisableHumanoidStates: table,
    LowPoly: table,
};

DisableHumanoidStates: table → Basically, the arguments of LBPerformance.DisableHumanoidStates(hum: Humanoid, ...: any) [Optional]

LowPoly: table → Basically, the arguments of LBPerformance.LowPoly(...: Instance) [Optional]

Basically, using LBPerformance.OptimizeLighting(), LBPerformance.LowPoly(...: Instance) and LBPerformance.DisableHumanoidStates(hum: Humanoid, ...: any) in a function.

16 Likes

I really like this kind of thing, as optimization is one of the important parts when creating a game. I have an idea to supplement this post with a screenshot/video for visual changes, without a module, with a module. I would also really like it to be presented as an in-game function, that is, so that you can include textures in the game or leave them, and so on

1 Like

Thank you so much! I will consider adding some screenshots/videos to the post and I agree that presented as an in-game function would be useful.

2 Likes