#Script for duplicating a group (making chunks) leaves a gap between chunks Could anyone help to fix?

1 messages · Page 1 of 1 (latest)

cursive gale
#
local Players = game:GetService("Players")
local MAP = workspace:WaitForChild("MAP")

local folder = workspace:FindFirstChild("STREAMED_MAP") or Instance.new("Folder", workspace)
folder.Name = "STREAMED_MAP"

local chunks = {}

local bboxCFrame, bboxSize = MAP:GetBoundingBox()
local STEP_X = bboxSize.X
local STEP_Z = bboxSize.Z

local minCorner = bboxCFrame.Position - (bboxSize / 2)
local minX = minCorner.X
local minZ = minCorner.Z

local function getChunkIndex(pos, minEdge, step)
    return math.floor((pos - minEdge) / step)
end

local function key(x, z)
    return x .. "," .. z
end

local function spawnChunk(x, z)
    local k = key(x, z)
    if chunks[k] then return end

    local clone = MAP:Clone()
    clone.Parent = folder

    local offset = Vector3.new(
        minX + x * STEP_X + STEP_X/2,
        0,
        minZ + z * STEP_Z + STEP_Z/2
    )

    local origCFrame = MAP:GetPivot()
    clone:PivotTo(CFrame.new(offset) * CFrame.Angles(0, origCFrame:ToEulerAnglesYXZ()))

    chunks[k] = clone
end

local function updateChunks(hrp)
    local pos = hrp.Position

    local cx = getChunkIndex(pos.X, minX, STEP_X)
    local cz = getChunkIndex(pos.Z, minZ, STEP_Z)

    local SIDE = 2
    local FRONT = 3
    local BACK = 1

    local needed = {}

    for x = cx - SIDE, cx + SIDE do
        for z = cz - BACK, cz + FRONT do
            needed[key(x, z)] = true
            spawnChunk(x, z)
        end
    end

    for k, chunk in pairs(chunks) do
        if not needed[k] then
            chunk.Parent = nil
            chunks[k] = nil
        end
    end
end

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local hrp = character:WaitForChild("HumanoidRootPart")
        while character.Parent do
            updateChunks(hrp)
            task.wait(0.4)
        end
    end)
end)
#

This is the map.