Hi All,
Recently I’ve been working on a mining system which uses 3D noise to generate caves. This works by having the server recognize that the user has mined a block, checks adjacent blocks using a 3D array, and using a noise function, determines whether if it is a cave. If it is, then the it carves out a cave using recursion, checking if it reaches a block that doesn’t satisfy the noise function, generates it, then returns.
However, this sometimes results in thousands of blocks being generated at once on the server, and at times, activity on the script that encompasses this functionality reaches as high as 10%, which causes noticeable freezing for all clients for up to half a second.
(example of this happening)
I found that using :Clone() consistently was the cause over 90% of the time spent creating a new ore. I’ve seen posts that say using Instance.new() and creating a replica of that ore from scratch by setting the same properties is actually faster, but because I sometimes have particles / textures in these ores, and they are so different from one another, I haven’t really considered it. I also still want to keep the almost instant cave generation for seamlessness from the player’s perspective. Is there anything I can do to optimise, or is using :Clone() still the best option? Thanks.
(what is causing the bulk of the performance issue)
--tim is defined at the top of the function for generating an ore
--t is the Ore Table in a module containing all ores
local tim2 = os.clock()
local p = OreStorage[t.Name]:Clone() do
p.CFrame = start + Vector3.new(OSize*x,-OSize*y,OSize*z) --Line1
t.SpawnFunc(p) --Line2 (adds particles, light, etc.)
BlockCoords[x][y][z] = 1 --Line3 (sets position in 3d array to block)
p.Parent = workspace.Mine.Ores --Line4
end
local EndTime = os.clock()
print("Ran whole script in " .. EndTime-tim)
print("Created Ore in " .. EndTime-tim2)
return i
(from seperate testing of each line, time in seconds to execute)
(general result, again using time in seconds to execute)