Earlier today, I’ve been trying to make a chunk generation script. However, it seems my chunk generation won’t detect players entering a chunk (the first chunk, oddly, works properly). What approach should I use to fix this?
My script:
local Platforms = {"x0y0"}
local function GenerateBlock(Type, Position, Offset)
local Block = game.ReplicatedStorage.Objects:FindFirstChild(Type):Clone()
Block:PivotTo(Position + Vector3.new(0, Offset * 3, 0))
Block.Parent = workspace.World
end
function GetChunkPos(Pos:Vector3)
local function RoundVector(Vector)
local X = math.round(Vector.X)
local Y = math.round(Vector.Y)
local Z = math.round(Vector.Z)
return Vector3.new(X, Y, Z)
end
return RoundVector(Pos / Vector3.new(90, 90, 90)) * Vector3.new(90, 90, 90)
end
local function GenerateChunk(Position)
GenerateBlock("Bedrock", Position, -10)
for i = 1, 3 do
GenerateBlock("Dirt", Position, -4 + i)
end
for i = 1, 6 do
GenerateBlock("Stone", Position, -10 + i)
end
GenerateBlock("Grass", Position, 0)
end
local function GenerateSquare(Position)
local X = -1
local Y = -1
for _ = 1, 20 do
X += 1
Y = -1
for _ = 1, 20 do
Y += 1
GenerateChunk(CFrame.new(Position + Vector3.new(X * 3, 0--[[(math.round(((X + Position.X) / N / 3) / 3) * 3) + (math.round(((Y + Position.Y) / N / 3) / 3) * 3)]], Y * 3)))
end
end
local Part = Instance.new("Part")
Part.Anchored = true
Part.Parent = workspace
Part.Position = (Position + Vector3.new(30, 0, 30))
while wait(0.1) do
for i, Player in pairs(game.Players:GetPlayers()) do
if Player.Character and (Player.Character:FindFirstChildOfClass("Humanoid").RootPart.Position - (Position + Vector3.new(30, 0, 30))).Magnitude <= 90 then
local ChunkPos = Vector3.new(GetChunkPos(Player.Character:FindFirstChildOfClass("Humanoid").RootPart.Position.X), 0, GetChunkPos(Player.Character:FindFirstChildOfClass("Humanoid").RootPart.Position.Z))
local Name ="x".. ChunkPos.X .. "y" .. ChunkPos.Z + 90
if not table.find(Platforms, Name) then
table.insert(Platforms, Name)
task.spawn(GenerateSquare, ChunkPos + Vector3.new(0, 0, 60))
print(Name)
return
end
end
end
end
end
task.spawn(GenerateSquare, Vector3.new())