Rendering system

Hello.

I have made a rendering local script and I just want to see who likes it as how efficient it is for lag wise. The reason I’m wondering is because I’m a part of the trucking game and it has over 100k parts and idk if this script is helping it in a way to reduce lag.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspacee = game:GetService("Workspace")
local Character = script.Parent

local properties = {
	RenderCache = ReplicatedStorage:WaitForChild("RenderCache"),
	HumanoidRootPart = Character:WaitForChild("HumanoidRootPart"),
	RenderDistance = 60000,
	Render = Workspacee.GameParts,
	Parts = {}
}

local function Scan()

		for _, v in next,properties["RenderCache"]:GetChildren() do
			table.insert(properties["Parts"], v)
		end

		for _, v in next,properties["Render"]:GetChildren() do
			table.insert(properties["Parts"], v)
			v.Parent = properties["RenderCache"]
		end

end

local function GetPart(object)

	if object:IsA("BasePart") or object:IsA("UnionOperation") or object:IsA("SpecialMesh") or object:IsA("MeshPart") then
		return object
	else
		for _, obj in next,object:GetDescendants() do
			local part = GetPart(obj)
			if part then
				return part
			end
		end
	end
	return nil
end

local function Update()
	for _, v in next,properties["Parts"] do
		local success, Part = pcall(GetPart, v)
		if success and Part then
			local Distance = (Part.CFrame.p - properties["HumanoidRootPart"].CFrame.p).Magnitude
			Distance = math.floor(Distance + 0.5)
			if Distance <= properties["RenderDistance"] then
				v.Parent = properties["Render"]
			else
				v.Parent = properties["RenderCache"]
			end
		end
	end
end

local function UpdateTable()
	properties["Render"].Changed:Connect(function(PartAdded)
		table.insert(properties["Parts"], PartAdded)
	end)
end


Scan()
Update()
UpdateTable()

The best way to test this is just to disable the script and compare frame rate with the script on.

A few aesthetic points though:

Are we using camelCase or PascalCase?

I don’t think the next part is strictly necessary, and there should be a more specific naming for v.

UnionOperation and MeshPart inherit from BasePart so those checks can be removed.

Why is this in a pcall? GetPart doesn’t do anything that would error.

1 Like

Ok thank you for reviewing it :slight_smile:

I’m not much of a coder but I believe a special mesh is an object that goes inside a BasePart and isnt actually its own part so you’re un/rendering something that was already going to be un/rendered as its parent is a BasePart

Non coding wise, if you have any meshes that dont Need precise collisions then set it to ‘box’ or ‘hull’, this really helps with lag

1 Like

If you want anything that has alot of moving parts to be fast on roblox you need to use :BulkMoveTo()

1 Like