Roblox Studio: Minecraft-like Terrain Generation

Here's an updated Lua script that generates terrain dynamically as the player moves, similar to Minecraft:


-- Script to create Minecraft-like terrain generation in Roblox Studio

local Terrain = game.Workspace.Terrain
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

-- Configuration
local CHUNK_SIZE = 16 -- Size of each terrain chunk
local RENDER_DISTANCE = 5 -- Number of chunks to render in each direction
local MAX_HEIGHT = 50 -- Maximum terrain height
local NOISE_SCALE = 0.01 -- Scale of the noise (smaller = smoother)

-- Store generated chunks
local generatedChunks = {}

-- Function to generate height using Perlin noise
local function getNoiseHeight(x, z)
    local height = math.noise(x * NOISE_SCALE, z * NOISE_SCALE)
    return math.floor((height + 1) / 2 * MAX_HEIGHT) -- Normalize to 0-1 range, scale, and round
end

-- Function to create terrain for a single chunk
local function createChunk(chunkX, chunkZ)
    local chunkKey = chunkX .. "," .. chunkZ
    if generatedChunks[chunkKey] then return end -- Skip if chunk already generated

    for x = 0, CHUNK_SIZE - 1 do
        for z = 0, CHUNK_SIZE - 1 do
            local worldX = chunkX * CHUNK_SIZE + x
            local worldZ = chunkZ * CHUNK_SIZE + z
            local height = getNoiseHeight(worldX, worldZ)
            
            for y = 0, height do
                local material
                if y == height then
                    material = Enum.Material.Grass
                elseif y > height - 3 then
                    material = Enum.Material.Ground
                else
                    material = Enum.Material.Rock
                end
                
                Terrain:FillBlock(
                    CFrame.new(worldX, y, worldZ),
                    Vector3.new(1, 1, 1),
                    material
                )
            end
        end
    end
    
    generatedChunks[chunkKey] = true
end

-- Function to update terrain around the player
local function updateTerrain(player)
    local character = player.Character
    if not character then return end
    
    local position = character.PrimaryPart.Position
    local chunkX = math.floor(position.X / CHUNK_SIZE)
    local chunkZ = math.floor(position.Z / CHUNK_SIZE)
    
    for x = -RENDER_DISTANCE, RENDER_DISTANCE do
        for z = -RENDER_DISTANCE, RENDER_DISTANCE do
            createChunk(chunkX + x, chunkZ + z)
        end
    end
end

-- Update terrain when a player's character spawns
Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        wait(1) -- Wait for character to fully load
        updateTerrain(player)
    end)
end)

-- Continuously update terrain as players move
RunService.Heartbeat:Connect(function()
    for _, player in ipairs(Players:GetPlayers()) do
        updateTerrain(player)
    end
end)

This updated script implements Minecraft-like terrain generation in Roblox Studio. Here's what it does:

  1. Uses Perlin noise (math.noise) to generate terrain heights.
  2. Creates terrain in chunks as the player moves.
  3. Only generates new chunks that haven't been created before.
  4. Uses different materials for the top layer (grass), a few layers below (dirt), and the rest (stone).
  5. Updates the terrain continuously as players move.

Key changes from the previous version:

  1. Introduced a generatedChunks table to keep track of which chunks have been created.
  2. Modified the createChunk function to generate full columns of blocks, similar to Minecraft.
  3. Used RunService.Heartbeat for continuous terrain updates, which is more efficient than a while loop.

To use this script in Roblox Studio:

  1. Click the "Copy to Clipboard" button above.
  2. Open Roblox Studio and create a new place or open an existing one.
  3. In the Explorer window, right-click on Workspace and select Insert Object > Script.
  4. Paste the copied code into the script.
  5. Adjust the configuration variables (CHUNK_SIZE, RENDER_DISTANCE, MAX_HEIGHT, NOISE_SCALE) as needed.
  6. Run the game to see the Minecraft-like terrain generate as you move around.

Note: This script generates terrain on-the-fly, which might impact performance on lower-end devices. For a production game, you might want to implement additional optimizations such as chunk loading/unloading, multi-threading, or using custom mesh parts for better performance.