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