I've been trying to make procedural terrain based off of Roblox's example on the developer hub. However, I get major lag spikes when the character moves. My code is the following:
EDIT: On further inspection, it seems to not work past the render distance of the player. Meaning, it will generate 2 chunks around the player at the start (the render distance), then not generate anything else at all.
local CHUNK_SIZE = 64
local RENDER_DISTANCE = CHUNK_SIZE * 2
local CHUNK_MAX_HEIGHT = 0
local function chunkExists(chunkX, chunkZ)
if not chunks[chunkX] then
chunks[chunkX] = {}
end
return chunks[chunkX][chunkZ]
end
function checkSurroundings(location)
local range = math.max(1, RENDER_DISTANCE)
for x = location.X - range, location.X + range do
for z = location.Z - range, location.Z + range do
local checkingPoint = Vector3.new(math.floor((location.X / 4) + x), 0, math.floor((location.Z / 4) + z))
local chunkX, chunkY, chunkZ = round(checkingPoint.X), CHUNK_MAX_HEIGHT, round(checkingPoint.Z)
print(chunkX, chunkZ)
local cx = chunkX
local cy = CHUNK_MAX_HEIGHT
local cz = chunkZ
if not chunkExists(cx, cz) then
--IRRELEVANT CODE
makeChunk(cx, cy, cz)
end
end
end
end
game.Players.PlayerAdded:Connect(function(player)
local db = false
player.CharacterAdded:Connect(function()
local char = player.Character
local hrp = char:WaitForChild("HumanoidRootPart")
local pos = hrp.Position
hrp:GetPropertyChangedSignal("Position"):Connect(function()
if pos.X ~= hrp.Position.X or pos.Z ~= hrp.Position.Z then
if db == false then
checkSurroundings(hrp.Position)
pos = hrp.Position
db = true
wait(1)
db = false
end
end
end)
end)
end)
Note: Some code has been removed to keep the script relevant

5 comments
I think you removed makeChunk function...
I did. However, that is not important to the code as it only generates the terrain / perlin noise.
I can't see nothing that might lag, normally, loops with low wait times cause lags
What about the edit I made?
EDIT: On further inspection, it seems to not work past the render distance of the player. Meaning, it will generate 2 chunks around the player at the start (the render distance), then not generate anything else at all.