How do I improve voxel destruction performance?

I wanted to make a game where you could like destroy parts and stuff, it works well but the only thing is that performance is VERY bad. Studio freezes for like 1 second whenever I destroy a part, how can I improve performance for this?

Scripts:

-- ReplicatedStorage/VoxelModule

local module = {  }

function module.destroy(Part:BasePart, callback:(BasePart) -> ())
	local vsize = Vector3.one * 0.5
	
	local size = Part.Size
	local amount = Vector3.new(size.X / vsize.X, size.Y / vsize.Y, size.Z / vsize.Z)
	
	if amount.X % 1 > 0 or amount.Y % 1 > 0 or amount.Z % 1 > 0 then
		print('decimal')
	else
		local hs = size / 2
		for x = 1, amount.X do
			for y = 1, amount.Y do
				for z = 1, amount.Z do
					local voxel = Part:Clone()
					voxel.Size = vsize
					voxel:ClearAllChildren()
					local offset = Vector3.new(x, y, z) * vsize
					voxel.Position += Vector3.new(hs.X + vsize.X/2, hs.Y + vsize.Y/2, hs.Z + vsize.Z/2) - offset
					voxel.Parent = workspace.Voxels
					
					callback(voxel)
				end
			end
		end
	end
	
	Part:Destroy()
end

return module
-- ServerScriptService/Script

local Storage = game:GetService('ReplicatedStorage')

local Module = require(Storage.VoxelModule)
local rand = Random.new()

local hitPos = workspace.HitPosition.Position
local radius = 5

for i = 5, 0, -1 do
	task.wait(1)
	
	print(i)
end

workspace.HitPosition:Destroy()
Module.destroy(workspace.Part, function(voxel)
	local dist = (voxel.Position - hitPos).Magnitude
	
	if dist < radius + math.random() * (radius / 2) then
		if math.random() < 0.9 then
			voxel:Destroy()
			
			return
		end
		
		voxel.Anchored = false
		voxel.AssemblyAngularVelocity = rand:NextUnitVector() * 100
		voxel.AssemblyLinearVelocity = (hitPos - voxel.Position) * radius
	end
end)

Not sure about actual script performance, but after the initial pause it looks like physics throttling to me. You could try changing workspace.PhysicsSteppingMethod to Adaptive. Otherwise your only real option is to decrease the amount of voxels (limiting debris or making everything larger except for the explosion) to the point where it doesn’t throttle.

1 Like

I tried setting it to adaptive, still same results sadly. But I was thinking of doing something like this:

image

image

Only problem is that I have no idea how I can recreate the parts shown in the image using scripts.

GOD DAMN that’s a lot of voxels. Not quite sure there’s any easy fix for that

, but I also recommend doing what @kyjikyji said.

I’d lower voxels as a whole, as many players probably cannot handle that load without lag.

1 Like

you could try something like this?
I made this in apple notes app so definitely not 100 percent going to work…

i tried to make it check for the longest sides, and to limit it to a specific point.

-- ReplicatedStorage/VoxelModule

local module = {}

function module.destroy(Part: BasePart, callback: (BasePart) -> ())
    local vsize = Vector3.one * 0.5

    local size = Part.Size
    local amount = Vector3.new(size.X / vsize.X, size.Y / vsize.Y, size.Z / vsize.Z)

    if amount.X % 1 > 0 or amount.Y % 1 > 0 or amount.Z % 1 > 0 then
        print('decimal')
    else
        local hs = size / 2

        local longSides = {X = amount.X, Y = amount.Y, Z = amount.Z}
        local maxSide = math.max(longSides.X, longSides.Y, longSides.Z)
        for key, value in pairs(longSides) do
           
if value == maxSide then
longSides[key] = math.ceil(value * (2 / 3))
end
end
    for x = 1, longSides.X do
        for y = 1, longSides.Y do
            for z = 1, longSides.Z do
                local voxel = Part:Clone()
                voxel.Size = vsize
                voxel:ClearAllChildren()
                local offset = Vector3.new(x, y, z) * vsize
                voxel.Position += Vector3.new(hs.X + vsize.X / 2, hs.Y + vsize.Y / 2, hs.Z + vsize.Z / 2) - offset
                voxel.Parent = workspace.Voxels

                callback(voxel)
            end
        end
    end
end

Part:Destroy()
end

return module

I think I might just resize the voxels to 1 stud. Also, I tried to use this but I couldn’t really get it to work. (I get what it’s trying to do though)

1 Like

try increase voxel size or try using greedy meshing and try making voxel groups.i suggest groups or clumps as it would make it look extra realistic.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.