i can’t find any particular reason for this but whenever i attempt loading chunks all of the blocks appear at the same coordinate
this is really frustrating and i cannot find a fix to it
im working on infinite voxel terrain but everytime the code places a block with the chunk system i’ve made it all goes in a specific spot for each chunk and i’ve tried storing the position in a table but still can’t get it to work
here’s my code for the world generation;
local blockContainer = workspace.World
local intlimit = 100
local seed = Random.new():NextInteger(-intlimit, intlimit)
local chunks = game.ServerStorage.storage.req.chunks
local clamprange = 0.5
local fogScreenSize = 16 -- default 100
local resolution = 100 -- default 100
local frequency = 1.5 -- default 3
local amplitude = 10 -- default 12
local spawnchunkvector = Vector3.new(0, 0, 0)
function getHeight(x, z)
local noiseHeight = math.noise((x/resolution*frequency), seed, (z/resolution*frequency))
noiseHeight = math.clamp(noiseHeight, -clamprange, clamprange) + 0.5
return noiseHeight
end
function incsnap(n, to)
return math.floor(n/to + 0.5) * to
end
function placeblock(block:string, pos:Vector3)
return require(game.ReplicatedStorage.bin.shared.mc_modules.mappings).placeblock(block, pos)
end
function placetree(origin, height)
local tree = Instance.new("Model")
local stump = placeblock("log", origin)
local toplog = stump
stump.Parent = tree
-- make logs & stump
spawn(function()
for y = 0, height do
local log = placeblock("log", origin + Vector3.new(0, 3, 0))
origin += Vector3.new(0, 3, 0)
log.Parent = tree
toplog = log
end
end)
-- make leaves
spawn(function()
end)
return tree
end
--[[function old_generate()
blockContainer:ClearAllChildren()
for x = 0, fogScreenSize do
for z = 0, fogScreenSize do
local y = getHeight(x, z)
local blocktype = "grass"
local block = placeblock(blocktype, Vector3.new(x*3, incsnap(math.round((y*amplitude * 6)), 3), z*3))
block.Parent = blockContainer
print("Generated block @ "..tostring(block.Position))
if (Random.new():NextInteger(1, 100) == 1) then
local tree = placetree(block.Position + Vector3.new(0, 3, 0), Random.new():NextInteger(3, 5))
tree.Parent = blockContainer
print("Spawned tree @ "..tostring(block.Position))
end
end
game:GetService("RunService").Heartbeat:Wait()
end
end]]
function cacheChunk(chunk:Folder)
local templateblockdata = {
['blocktype'] = nil;
['position'] = nil;
['rot'] = nil;
}
local memory = {['chunkdata'] = {
['blocks'] = {
}
}}
for i, b in pairs(chunk:GetChildren()) do
if b:IsA("BasePart") then
local blockdata = templateblockdata
blockdata.blocktype = b.Name
blockdata.position = {
['X'] = b.Position.X;
['Y'] = b.Position.Y;
['Z'] = b.Position.Z;
}
blockdata.rot = b.Orientation
table.insert(memory.chunkdata.blocks, blockdata)
end
end
--print("ChunkDataCache:n"..tostring(memory.chunkdata))
require(chunks).chunks[chunk.Name] = memory.chunkdata
end
function generateChunk(pos:Vector3)
local folder = Instance.new("Folder")
folder.Name = tostring(pos)
for x = 0, fogScreenSize do
for z = 0, fogScreenSize do
local y = getHeight(x, z)
local blocktype = "grass"
local blockpos = Vector3.new((x*3) + pos.X, incsnap(math.round((y*amplitude * 6)), 3), (z*3) + pos.Z)
local block = placeblock(blocktype, blockpos)
block.Parent = folder
--print("Generated block @ "..tostring(block.Position))
if (Random.new():NextInteger(1, 100) == 1) then
local tree = placetree(block.Position + Vector3.new(0, 3, 0), Random.new():NextInteger(3, 5))
tree.Parent = folder
--print("Spawned tree @ "..tostring(block.Position))
end
end
--game:GetService("RunService").Heartbeat:Wait()
end
cacheChunk(folder)
return folder
end
function loadChunk(chunk:string)
if require(chunks).chunks[chunk] ~= nil then
local chunkdata = require(chunks).chunks[chunk]
local chunkfolder = Instance.new("Folder")
chunkfolder.Name = chunk
chunkfolder.Parent = workspace.World
for _, b in pairs(chunkdata.blocks) do
local blocktype = b.blocktype
local pos = Vector3.new(b.position.X, b.position.Y, b.position.Z)
local world_block = placeblock(blocktype, pos)
world_block.Orientation = b.rot
world_block.Parent = chunkfolder
end
else
-- invalid chunk identifier
error("Invalid chunk ID!")
end
end
function unloadChunk(chunk:Folder)
cacheChunk(chunk)
chunk:Destroy()
end
-- test code
generateChunk(spawnchunkvector)
loadChunk(tostring(spawnchunkvector))
generateChunk(Vector3.new(0, 0, 16))
loadChunk("0, 0, 16")
spawn(function()
while true do
task.wait(1)
unloadChunk(blockContainer[tostring(spawnchunkvector)])
task.wait(1)
loadChunk(tostring(spawnchunkvector))
end
end)
-- loadChunk(Vector2.new(0, 0)) -- make sure spawn is loaded --Vector3.new(-(fogScreenSize / 2), 0, -(fogScreenSize / 2))
function saveGame()
if game.PlaceId ~= 17309654844 then
game:GetService("AssetService"):SavePlaceAsync()
else
print("Cannot save during tests")
end
end