|
| 1 | +-- Server Script |
| 2 | + |
| 3 | +local CHUNK_SIZE = 50 |
| 4 | +local VIEW_DISTANCE = 3 -- Number of chunks to keep loaded ahead and behind |
| 5 | + |
| 6 | +local function generateChunk(x, z) |
| 7 | + local chunk = Instance.new("Part") |
| 8 | + chunk.Size = Vector3.new(CHUNK_SIZE, 10, CHUNK_SIZE) -- Basic chunk size |
| 9 | + chunk.Position = Vector3.new(x * CHUNK_SIZE, 0, z * CHUNK_SIZE) |
| 10 | + chunk.Anchored = true |
| 11 | + chunk.Parent = workspace.World |
| 12 | + |
| 13 | + -- Simple heightmap using a mathematical function (replace with noise) |
| 14 | + for i = 1, CHUNK_SIZE do |
| 15 | + for j = 1, CHUNK_SIZE do |
| 16 | + local xPos = x * CHUNK_SIZE + i |
| 17 | + local zPos = z * CHUNK_SIZE + j |
| 18 | + local height = math.sin(xPos/10) * math.cos(zPos/10) * 5 -- Example function |
| 19 | + local block = Instance.new("Part") |
| 20 | + block.Size = Vector3.new(1, height, 1) |
| 21 | + block.Position = Vector3.new(xPos, height/2, zPos) |
| 22 | + block.Anchored = true |
| 23 | + block.Parent = chunk |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + return chunk |
| 28 | +end |
| 29 | + |
| 30 | +local function updateWorld(playerPos) |
| 31 | + local chunkX = math.floor(playerPos.X / CHUNK_SIZE) |
| 32 | + local chunkZ = math.floor(playerPos.Z / CHUNK_SIZE) |
| 33 | + |
| 34 | + -- Generate chunks around the player |
| 35 | + for x = chunkX - VIEW_DISTANCE, chunkX + VIEW_DISTANCE do |
| 36 | + for z = chunkZ - VIEW_DISTANCE, chunkZ + VIEW_DISTANCE do |
| 37 | + local chunkExists = false |
| 38 | + for i, existingChunk in ipairs(workspace.World:GetChildren()) do |
| 39 | + if existingChunk.Name == "Chunk" then |
| 40 | + local chunkPosition = existingChunk.Position |
| 41 | + if chunkPosition.X == x * CHUNK_SIZE and chunkPosition.Z == z * CHUNK_SIZE then |
| 42 | + chunkExists = true |
| 43 | + break |
| 44 | + end |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + if not chunkExists then |
| 49 | + local newChunk = generateChunk(x, z) |
| 50 | + newChunk.Name = "Chunk" |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + -- Delete chunks that are too far away |
| 56 | + for i, existingChunk in ipairs(workspace.World:GetChildren()) do |
| 57 | + if existingChunk.Name == "Chunk" then |
| 58 | + local chunkPosition = existingChunk.Position |
| 59 | + local distanceX = math.abs(chunkPosition.X - playerPos.X) |
| 60 | + local distanceZ = math.abs(chunkPosition.Z - playerPos.Z) |
| 61 | + |
| 62 | + if distanceX > CHUNK_SIZE * VIEW_DISTANCE or distanceZ > CHUNK_SIZE * VIEW_DISTANCE then |
| 63 | + existingChunk:Destroy() |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | +end |
| 68 | + |
| 69 | +game:GetService("RunService").Heartbeat:Connect(function() |
| 70 | + if game.Players.LocalPlayer and game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart") then |
| 71 | + local playerPosition = game.Players.LocalPlayer.Character.HumanoidRootPart.Position |
| 72 | + updateWorld(playerPosition) |
| 73 | + end |
| 74 | +end) |
| 75 | + |
0 commit comments