I have an issue where my unfixed editable mesh wont get destroyed. This is causing issues because im now running out of memory way too fast and can only render in 4 chunks before the memory is completely out
the module;
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AssetService = game:GetService("AssetService")
-- Config modules
local config_modules = ReplicatedStorage:WaitForChild("Config")
local EnvironmentConfig = require(config_modules:WaitForChild("Environment"))
local VoxelsConfig = require(config_modules:WaitForChild("Voxels"))
-- Modules
local modules = ReplicatedStorage:WaitForChild("Modules")
local mixed_modules = modules:WaitForChild("Mixed")
local Chunks = require(mixed_modules:WaitForChild("Chunks"))
-- Chunks container folder
local chunks_container: Folder = workspace:FindFirstChild("Chunks") or Instance.new("Folder")
chunks_container.Name = "Chunks"
chunks_container.Parent = workspace
local VOXEL_VERTS = {
Vector3.new(0, 0, 0),
Vector3.new(1, 0, 0),
Vector3.new(1, 1, 0),
Vector3.new(0, 1, 0),
Vector3.new(0, 0, 1),
Vector3.new(1, 0, 1),
Vector3.new(1, 1, 1),
Vector3.new(0, 1, 1)
}
local VOXEL_TRIS = {
{1, 3, 2}, {1, 4, 3},
{5, 6, 7}, {5, 7, 8},
{1, 5, 8}, {1, 8, 4},
{2, 3, 7}, {2, 7, 6},
{4, 8, 7}, {4, 7, 3},
{1, 2, 6}, {1, 6, 5}
}
-- Module
local ChunkRenderer = {}
function ChunkRenderer.Render(ChunkID: number)
local chunk_data = EnvironmentConfig.Chunks[ChunkID]
if not chunk_data then return end
local chunk_folder = chunks_container:FindFirstChild(tostring(ChunkID))
if not chunk_folder then
chunk_folder = Instance.new("Folder")
chunk_folder.Name = tostring(ChunkID)
chunk_folder.Parent = chunks_container
end
for _, obj in ipairs(chunk_folder:GetChildren()) do
obj:Destroy()
end
local chunk_size = EnvironmentConfig.ChunkSize
local voxel_size = EnvironmentConfig.VoxelSize
local chunk_pos = Chunks.GetPositionFromID(ChunkID)
local world_origin = Vector3.new(
chunk_pos.X * chunk_size * voxel_size,
chunk_pos.Y * chunk_size * voxel_size,
chunk_pos.Z * chunk_size * voxel_size
)
local unfixed_mesh = AssetService:CreateEditableMesh()
for voxel_pos, voxel_data in pairs(chunk_data) do
local base_pos = voxel_pos * voxel_size
local ids = {}
for i, v in ipairs(VOXEL_VERTS) do
ids[i] = unfixed_mesh:AddVertex(base_pos + v * voxel_size)
end
for _, tri in ipairs(VOXEL_TRIS) do
unfixed_mesh:AddTriangle(ids[tri[1]], ids[tri[2]], ids[tri[3]])
end
end
unfixed_mesh:RemoveUnused()
local fixed_mesh: EditableMesh = AssetService:CreateEditableMeshAsync(Content.fromObject(unfixed_mesh), {FixedSize = true})
unfixed_mesh:Destroy()
local meshpart = AssetService:CreateMeshPartAsync(Content.fromObject(fixed_mesh), {
CollisionFidelity = Enum.CollisionFidelity.Box,
FluidFidelity = Enum.FluidFidelity.UseCollisionGeometry
})
meshpart:ApplyMesh(meshpart)
meshpart.CFrame = CFrame.new(world_origin)
meshpart.Name = "ChunkMesh"
meshpart.CanCollide = false
meshpart.CanQuery = false
meshpart.Anchored = true
meshpart.Parent = chunk_folder
if unfixed_mesh then
print("still here dawg")
unfixed_mesh:Destroy()
warn(unfixed_mesh)
-- these print, meaning the unfixed mesh is still here
end
end
return ChunkRenderer
