DataStore an instance?

Hello, I want to datastore a folder. This folder has many contents in it and it would be tedious to use a lot of Instance.new() in my DataStore script, so want to save the folder aand it’s children. The folder is in ReplicatedStorage, by the way. Here’s my script.

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("OtherStats")

local function onPlayerJoin(player)  -- Runs when players join
	local otherstats = game.ReplicatedStorage.OtherStats

	local playerUserId = "Player_" .. player.UserId  --Gets player ID
	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data
	if data then
		local newstats = data['Stats']
		newstats.Parent = player
	else
		local newcopy = otherstats:Clone()
		newcopy.Parent = player
	end
end

local function onPlayerExit(player)  --Runs when players exit
	local savestats = player:FindFirstChild("OtherStats")
	local success, err = pcall(function()
		local playerUserId = "Player_" .. player.UserId
		playerData:SetAsync(playerUserId, savestats) --Saves player data
	end)
	if not success then
		warn('Could not save data!')
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

When I leave the game, I get the message “Could not save data!”
What’s wrong with my script or is it possible to save instances? I’m fairly sure It’s possible to save instances, after all, games like Blockate and Miners’ Haven datastore your builds.

yes, studio access to API services is enabled.

1 Like

Sadly you cannot save instances to DataStores, Blockate and Miners’ Haven use methods of Serialization. You should loop through the folder’s contents, put it in a table, and save the table.

It is not possible to save instances. Not entirely sure how they save builds.

But the way I like to save builds is saving the data of each part into a nested table. I save the part name to a table, and then that part name is its own table of color, material, size, position, etc. into a table, and then I encode it with HttpSerivce and save the string. Upon joining, I’ll use HttpService to decode the saved string and apply the data from the table.

1 Like