Well firstly, if you don’t have an already vast or well understanding of datastores, you probably shouldn’t be dwelling in something as sophisticated as a saving/loading chunk system. I’m not trying to be discouraging - I’m just being realistic.
Boast aside, to save chunks with data within them you’ll first want to save an array of chunks in a datastore. Something along the lines of this:
-- services
local datastoreService = game:GetService("DataStoreService")
local players = game:GetService("Players")
-- variables
local DATASTORE_KEY = "ChunkData/"
local DEFAULT_DATA = {}
local chunkDatabase = datastoreService:GetDataStore("ChunkDatabase")
-- functions
local function playerAdded(player: Player)
-- guard clause to make sure the function isn't called twice on the rare occurence
if (player:GetAttribute("LoadedInChunkData")) then return end
player:SetAttribute("LoadedInChunkData", true)
-- get the chunk data
local chunkData = chunkDatabase:GetAsync(DATASTORE_KEY .. player.UserId)
-- guard clause to make sure that if there's no data, it defaults to a regular table
if (not chunkData) then chunkData = DEFAULT_DATA end
print(chunkData)
end
players.PlayerAdded:Connect(playerAdded)
-- for loop to catch any players which loaded in before the script initalization
for index, player in ipairs(players:GetPlayers()) do
playerAdded(player)
end
The output of running this code will give us an array looking something like this (of course if the data existed. It wouldn’t actually print this):
{
{
x = 1, -- this is the x position of the chunk
y = 1, -- and the y position of the chunk
metadata = {
items = {}, -- this metadata table contains additional data such as what is contained within the chunk; chairs, tables, loose items, etc...
},
},
{
x = 2, -- this is the x position of the chunk
y = 1, -- and the y position of the chunk
metadata = {
items = {}, -- this metadata table contains additional data such as what is contained within the chunk; chairs, tables, loose items, etc...
},
},
..., -- and so on for however many chunks you have
}
With this data you can create chunks. You can use the metadata within each ‘chunk data’ to reconstruct the loose items, models, etc… If you don’t know how to save loose items, or models in a datastore, you can read a tutorial about serialization.
This is all I can really say because I have no idea what you want your game to look like. I don’t know if it’s multiplayer, or single player. I don’t know if you save loose items, models or the latter. The list goes on.
I don’t particularly have much time as I don’t forum post for a living, but if you have any additional questions, feel free to ask.