Perclean Performance Module - Review

Hello there!

I’ve recently made a module script of my own called Perclean. It’s nothing that big, it only has a couple of functions. My favourite are EnableLowQualityLighting() and DisableLowQualityLighting().

Here’s the code:

-- @author - Downrest
-- @description - This module script contains functions that can help you optimize your game

--[[
[EXPERIMENTAL] [USES]

[perclean.EnableLowQualityLighting()]
USES: Removes all lighting effects and disables Global shadows with other lighting options.

EXAMPLE: perclean.EnableLowQualityLighting()

-----]

[perclean.DisableLowQualityLighting()]
USES: Reverts all change if low quality lighting is enabled.

EXAMPLE: perclean.DisableLowQualityLighting()

-----]

[perclean.EnableLowQualityRendering()]
USES: Disables reflectance and other additional rendering effects of all parts.

EXAMPLE: perclean.EnableLowQualityRendering()

-----]

[perclean.DisableLowQualityRendering()]
USES: Reverts all change if low quality rendering is enabled.

EXAMPLE: perclean.DisableLowQualityRendering()

-----]

[perclean.CleanInstance(getSource, getInstanceType, getWhitelistedName)]
USES: It removes all specified objects from a specified source (children, not descendants)

EXAMPLE: perclean.CleanInstance(workspace, "TrussPart")
OPTIONAL #1: perclean.CleanInstance(workspace, "TrussPart", "MeNoWantATrussNamedThisRemoved")

-----]

[perclean.ScanScripts(getSource)]
USES: It prints all scripts from a specified source (all descendants that are scripts)

EXAMPLE: perclean.ScanScripts(workspace)
]]

local perclean = {}
local getLighting = game:GetService("Lighting")
local getReplicatedStorage = game:GetService("ReplicatedStorage")
local getWorkspace = workspace
local newInstance = Instance.new

function perclean.EnableLowQualityLighting(isDebuggingEnabled)
	local getLightingFolder
	local getQualityState 
	
	if not getReplicatedStorage:FindFirstChild("PercleanLighting") then
		getLightingFolder = newInstance("Folder")
		getLightingFolder.Name = "PercleanLighting"
		getLightingFolder.Parent = getReplicatedStorage
		
		getQualityState = newInstance("BoolValue")
		getQualityState.Name = "QualityState"
		getQualityState.Parent = getLightingFolder
	else
		getLightingFolder = getReplicatedStorage:FindFirstChild("PercleanLighting")
		getQualityState = getLightingFolder:FindFirstChild("QualityState")
	end
	
	if getQualityState.Value == true then
		error("[PERCLEAN] Low Quality Lighting has already been enabled!")
	end
	
	getLighting.GlobalShadows = false
	getLighting.EnvironmentDiffuseScale = 0
	getLighting.EnvironmentSpecularScale = 0
	getQualityState.Value = true
	
	for i,v in ipairs(getLighting:GetChildren()) do
		if v:IsA("Sky") ~= true then
			v:Clone().Parent = getLightingFolder
			v:Destroy()
		end
	end
	
	for i,v in ipairs(getWorkspace:GetDescendants()) do
		if v:IsA("UnionOperation") or v:IsA("PartOperation") or v:IsA("Part") or v:IsA("TrussPart") or v:IsA("WedgePart") then
			v.CastShadow = false
		end
	end
	
	if isDebuggingEnabled == true then
		warn("[PERCLEAN] LOW QUALITY LIGHTING: ENABLED")
	end
end

function perclean.DisableLowQualityLighting(isDebuggingEnabled)
	local getLightingFolder = getReplicatedStorage:FindFirstChild("PercleanLighting")
	local getQualityState = getLightingFolder:FindFirstChild("QualityState")
	
	if getQualityState.Value == false then
		error("[PERCLEAN] Low Quality Lighting has already been disabled!")
	end
	
	getLighting.GlobalShadows = true
	getLighting.EnvironmentDiffuseScale = 1
	getLighting.EnvironmentSpecularScale = 1
	getQualityState.Value = false

	for i,v in ipairs(getLightingFolder:GetChildren()) do
		if v:IsA("BoolValue") ~= true and v.Name ~= "QualityState" then
			v:Clone().Parent = getLighting
			v:Destroy()
		end
	end
	
	for i,v in ipairs(getWorkspace:GetDescendants()) do
		if v:IsA("UnionOperation") or v:IsA("PartOperation") or v:IsA("Part") or v:IsA("TrussPart") or v:IsA("WedgePart") then
			v.CastShadow = true
		end
	end
	
	if isDebuggingEnabled == true then
		warn("[PERCLEAN] LOW QUALITY LIGHTING: DISABLED")
	end
end

function perclean.EnableLowQualityRendering(isDebuggingEnabled)
	local getRenderingFolder
	local getQualityState 

	if not getReplicatedStorage:FindFirstChild("PercleanRendering") then
		getRenderingFolder = newInstance("Folder")
		getRenderingFolder.Name = "PercleanRendering"
		getRenderingFolder.Parent = getReplicatedStorage

		getQualityState = newInstance("BoolValue")
		getQualityState.Name = "QualityState"
		getQualityState.Parent = getRenderingFolder
	else
		getRenderingFolder = getReplicatedStorage:FindFirstChild("PercleanRendering")
		getQualityState = getRenderingFolder:FindFirstChild("QualityState")
	end

	if getQualityState.Value == true then
		error("[PERCLEAN] Low Quality Rendering has already been enabled!")
	end
	
	getQualityState.Value = true

	for i,v in ipairs(getWorkspace:GetDescendants()) do
		if v:IsA("PartOperation") or v:IsA("Part") or v:IsA("TrussPart") or v:IsA("WedgePart") then
			v.Reflectance = 0
			v.CastShadow = false
		else if v:IsA("UnionOperation") then
				v.Reflectance = 0
				v.SmoothingAngle = 0
				v.CastShadow = false	
				v.RenderFidelity = Enum.RenderFidelity.Performance
			end
		end
	end
	
	if isDebuggingEnabled == true then
		warn("[PERCLEAN] LOW QUALITY RENDERING: ENABLED")
	end
end

function perclean.DisableLowQualityRendering(isDebuggingEnabled)
	local getRenderingFolder = getReplicatedStorage:FindFirstChild("PercleanRendering")
	local getQualityState = getRenderingFolder:FindFirstChild("QualityState")

	if getQualityState.Value == false then
		print(getQualityState.Value)
		error("[PERCLEAN] Low Quality Rendering has already been disabled!")
	end
	
	getQualityState.Value = false
	
	for i,v in ipairs(getWorkspace:GetDescendants()) do
		if v:IsA("PartOperation") or v:IsA("Part") or v:IsA("TrussPart") or v:IsA("WedgePart") then
			v.Reflectance = 0
			v.CastShadow = true
		else if v:IsA("UnionOperation") then
				v.Reflectance = 0
				v.SmoothingAngle = 0
				v.CastShadow = true	
				v.RenderFidelity = Enum.RenderFidelity.Automatic
			end
		end
	end
	if isDebuggingEnabled == true then
		warn("[PERCLEAN] LOW QUALITY RENDERING: DISABLED")
	end
end

function perclean.CleanInstance(getSource, getInstanceType, getInstanceFunction, getWhitelistedName, isDebuggingEnabled)
	if getSource == nil and getInstanceType == nil and getInstanceFunction == nil and getWhitelistedName == nil then
		error("[PERCLEAN] Function parameters cannot be empty or nil!")
	end
	if getSource == nil then
		error("[PERCLEAN] Argument #1 cannot be empty or nil!")
	end
	if getInstanceType == nil then
		error("[PERCLEAN] Argument #2 cannot be empty or nil!")
	end
	if getInstanceType ~= nil and typeof(getInstanceType) ~= "string" then
		error("[PERCLEAN] Argument #2 has to be a string or type of Roblox Instance!")
	end
	if getInstanceFunction == nil then
		error("[PERCLEAN] Argument #3 cannot be empty or nil!")
	end
	if getInstanceFunction ~= nil and typeof(getInstanceFunction) ~= "string" then
		error("[PERCLEAN] Argument #3 has to be either 'GetChildren' or 'GetDescendants'")
	end
	if getInstanceFunction ~= nil then
		if getInstanceFunction ~= "GetChildren" and getInstanceFunction ~= "GetDescendants" then
			error("[PERCLEAN] Argument #3 has to be either 'GetChildren' or 'GetDescendants'")
		end
	end
	if getWhitelistedName ~= nil and typeof(getWhitelistedName) ~= "string" then
		error("[PERCLEAN] Argument #4 has to be a string or name of object!")
	end
	
	if getInstanceFunction == "GetChildren" then
		if getWhitelistedName == nil then
			for i,v in ipairs(getSource:GetChildren()) do	
				if v:IsA(getInstanceType) then
					warn("[PERCLEAN] CLEANED: " .. tostring(v))
					v:Destroy()
				end
			end
		else
			for i,v in ipairs(getSource:GetChildren()) do	
				if v:IsA(getInstanceType) and v.Name ~= getWhitelistedName then
					warn("[PERCLEAN] CLEANED: " .. tostring(v))
					v:Destroy()
				end
			end
		end
	elseif getInstanceFunction == "GetDescendants" then
		if getWhitelistedName == nil then
			for i,v in ipairs(getSource:GetDescendants()) do	
				if v:IsA(getInstanceType) then
					if isDebuggingEnabled == true then
						warn("[PERCLEAN] CLEANED: " .. tostring(v))
					end
					v:Destroy()
				end
			end
		else
			for i,v in ipairs(getSource:GetDescendants()) do	
				if v:IsA(getInstanceType) and v.Name ~= getWhitelistedName then
					if isDebuggingEnabled == true then
						warn("[PERCLEAN] CLEANED: " .. tostring(v))
					end
					v:Destroy()
				end
			end
		end
	end
end

function perclean.ScanScripts(getSource, isDebuggingEnabled)
	if getSource == nil then
		error("[PERCLEAN] Function parameters cannot be empty or nil!")
	end
	
	for i,v in ipairs(getSource:GetDescendants()) do	
		if v:IsA("LuaSourceContainer") then
			if isDebuggingEnabled == true then
				warn("[PERCLEAN] SCANNED: " .. tostring(v:GetFullName()))
			end
		end
	end
end

return perclean

I don’t really have any more ideas on what to put inside this module. I would like to know if there is any way to make it better? And if so, how?

All I can really think of is adding a variable to toggle debugging and datastores. Also, if you can do it in-game, change the RenderFidelity of MeshParts

1 Like

Sadly I’m pretty sure you can’t change RenderFidelity with scripts or while the game is running, you can only change it before running the game on Roblox Studio.

1 Like

Question, what do you mean by “toggle debugging”?

image
etc

1 Like

Ah, I see. Well in that case I’ll add it in.

1 Like

Alright, I’ve added optional debugging for every single function.