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()