Answered by
4 years ago Edited 4 years ago
Instead of just generating the whole thing(which will take a long time), I recommend you generate chunks around where all the players are, which is what Minecraft does.
I found this script off the devforum that does exactly what you want, note that it contains some ugly stuff like parenting before changing the properties, but it should do:
01 | local Players = game:GetService( "Players" ) |
07 | local RENDER_DISTANCE = 25 |
10 | local GENERATION_SEED = math.random() |
16 | local function roundToOdd(n) |
18 | return math.floor(n - n % 3 ); |
22 | local function chunkExists(chunkX, chunkZ) |
23 | if not chunks [ chunkX ] then |
26 | return chunks [ chunkX ] [ chunkZ ] |
29 | local function mountLayer(x, heightY, z, material) |
30 | local beginY = -BASE_HEIGHT |
32 | local cframe = CFrame.new(x * 3 + 1 , roundToOdd((beginY + endY) * 3 / 1 ), z * 3 + 1 ) |
33 | local size = Vector 3. new( 3 , (endY - beginY) * 3 , 3 ) |
34 | local p = Instance.new( "Part" , workspace) |
37 | p.Size = Vector 3. new( 3 , 3 , 3 ) |
38 | p.Material = Enum.Material.Grass |
39 | p.BrickColor = BrickColor.new( "Forest green" ) |
42 | function makeChunk(chunkX, chunkZ) |
43 | local rootPosition = Vector 3. new(chunkX * CHUNK_SCALE, 0 , chunkZ * CHUNK_SCALE) |
44 | chunks [ chunkX ] [ chunkZ ] = true |
45 | for x = 0 , CHUNK_SCALE - 1 do |
46 | for z = 0 , CHUNK_SCALE - 1 do |
47 | local cx = (chunkX * CHUNK_SCALE) + x |
48 | local cz = (chunkZ * CHUNK_SCALE) + z |
49 | local noise = math.noise(GENERATION_SEED, cx / X_SCALE, cz / Z_SCALE) |
50 | local cy = noise * BASE_HEIGHT |
51 | mountLayer(cx, cy, cz, Enum.Material.Grass) |
56 | function checkSurroundings(location) |
57 | local chunkX, chunkZ = math.floor(location.X / 3 / CHUNK_SCALE), math.floor(location.Z / 3 / CHUNK_SCALE) |
58 | local range = math.max( 1 , RENDER_DISTANCE / CHUNK_SCALE) |
59 | for x = -range, range do |
60 | for z = -range, range do |
61 | local cx, cz = chunkX + x |
63 | if not chunkExists(cx, cz) then |
70 | game:GetService( "RunService" ).Heartbeat:Connect( function () |
71 | for _, player in pairs (Players:GetPlayers()) do |
72 | if player.Character then |
73 | local humanoidRootPart = player.Character:FindFirstChild( "HumanoidRootPart" ) |
74 | if humanoidRootPart then |
75 | checkSurroundings(humanoidRootPart.Position) |
Hope this helped