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:
Key changes from the previous version:
generatedChunks
table to keep track of which chunks have been created.createChunk
function to generate full columns of blocks, similar to Minecraft.RunService.Heartbeat
for continuous terrain updates, which is more efficient than a while loop.To use this script in Roblox Studio:
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.