Hello! I was wondering if there was a way to speed up this terrain generation, because it takes absolutely forever, and when I don’t put a wait() in here, it just decides to time out the script for going too fast, any help?
module.CreateVoxel = function(x ,y ,z)
local Voxel = Instance.new("Part")
Voxel.Size = Vector3.new(Ratio,Ratio + sizeY,Ratio)
Voxel.Anchored = true
Voxel.Parent = workspace
Voxel.Position = Vector3.new(x,y,z)
--Voxel.Shape = Enum.PartType.Ball
local Material = nil
if y < 0 then
Material = Enum.Material.Sand
else
Material = Enum.Material.Grass
end
workspace.Terrain:FillBlock(Voxel.CFrame, Voxel.Size, Material)
game:GetService("Debris"):AddItem(Voxel, 0.1)
task.wait(0.00000000000001)
end
module.GenerateTerrain = function()
for z = 1, Area2 do
for x = 1, Area2 do
local Content = math.noise(x / 50 + ofsX, z / 50 + ofsZ, 0)
local Erosion = math.noise(x / 60 + ofsX, z / 60 + ofsZ, 0)
module.CreateVoxel(x * Ratio,(Content * (Ymultiplier * Ratio)) - (Erosion * (ErosionY * Ratio)), z * Ratio)
end
end
workspace.Terrain:SetAttribute("Loading", false)
module.GenerateSealevel(58)
end
I believe doing task.wait() gives you the shortest wait possible.
You can probably divide the generation up into chunks;
Let’s say you want to generate 1000 voxels, that takes too long and lags the game. Creating 1 and waiting between each takes ages. The solution would then be to do ex. 100, wait and then do another 100 until they have all been completed.
Side note: I personally think modulescripts should be written like this:
function module.CreateVoxel(x: number, y: number, z: number) -- Any reason you are not using Vector3 to represent this?
... -- Your code here
end
I’m a little stuck trying to implement this. The idea is fairly straight forward and should work theoretically but can you provide insight on how to do this?
I see that you added some additional code, if you modify it to something like this:
local chunkSize = 100
function module.GenerateTerrain()
for z = 1, Area2 do
for x = 1, Area2 do
local Content = math.noise(x / 50 + ofsX, z / 50 + ofsZ, 0)
local Erosion = math.noise(x / 60 + ofsX, z / 60 + ofsZ, 0)
module.CreateVoxel(x * Ratio,(Content * (Ymultiplier * Ratio)) - (Erosion * (ErosionY * Ratio)), z * Ratio)
if x % chunkSize == 0 then -- In this example it will pause after each 100 loops
task.wait()
end
end
end
workspace.Terrain:SetAttribute("Loading", false)
module.GenerateSealevel(58)
end
EDIT: I completely misread. This is how you write module scripts in that way:
local module = {} -- Create the table
function module.CreateVoxel(x: number, y: number, z: number) -- Add function to table
... -- Code
end
return module -- Return table
Edit 2: I misread completely, again (smh). But I hope this will help you anyways.
OK well I was not expecting that. It still takes a minute to load the entire map (it’s absolutely huge) but I just wanna say its insanely fast. Thanks man.
Try experimenting with chunk-sizes. You want to have large enough to let the map load quickly, but small enough to not create insane lag.
You can dynamically change the chunksizes by measuring the time it takes between ticks on the server/client, and increase/decrease the chunksize from that. You’d have to spend some time fine tuning that if you want it to work nicely, but I’m sure you’ll be able to figure that out.