Roblox Studio: Dynamic Terrain Generation with Perlin Noise

Here's an updated Lua script that generates terrain dynamically around the player using Perlin noise (math.noise):


-- Script to create dynamic terrain using Perlin noise in Roblox Studio

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

-- Configuration
local CHUNK_SIZE = 32 -- Size of each terrain chunk
local RENDER_DISTANCE = 3 -- 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)

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

-- Function to create terrain for a single chunk
local function createChunk(chunkX, chunkZ)
    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)
            
            Terrain:FillBlock(
                CFrame.new(worldX, height/2, worldZ),
                Vector3.new(1, height, 1),
                Enum.Material.Grass
            )
        end
    end
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 (you might want to optimize this for performance)
while true do
    for _, player in ipairs(Players:GetPlayers()) do
        updateTerrain(player)
    end
    wait(5) -- Update every 5 seconds
end

This script does the following:

  1. Uses Perlin noise (math.noise) to generate terrain heights.
  2. Creates terrain in chunks around the player's position.
  3. Updates the terrain when a player's character spawns.
  4. Continuously updates the terrain as the player moves.

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 terrain generate dynamically around the player.

Note: This script generates terrain continuously, which might impact performance. For a production game, you'd want to optimize this further, possibly by caching generated chunks and only updating when the player moves to a new chunk.