Not sure how to datastore a model

Hello developers, im currently trying to make a datasave script save a model with all the items the players has purchased in the game but im not really sure on how to. I tried doing data.purchasedmodels = game.Workspace.PurchasedItems but it didnt work. If anyone could help me out, it would be very appreciated!

datasave script:

local datastoreservice = game:GetService("DataStoreService")

local datastore = datastoreservice:GetDataStore("DataStore")

MPS = game:GetService("MarketplaceService")

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = 0
	cash.Parent = folder


	local data

	local success,errorMsg = pcall(function()
		data = datastore:GetAsync(player.UserId)
	end)

	if data ~= nil then
		
		if data.Cash then
			cash.Value = data.Cash
		end

	end
	
	game:BindToClose(function()
		for i, player in pairs(game.Players:GetPlayers()) do
			local data = {}

			data.Cash = player.leaderstats.Cash.Value
			
		end
	end)
	
	game.Players.PlayerRemoving:Connect(function(player)

		local data = {}

		data.Cash = player.leaderstats.Cash.Value
		
		local success,errorMsg = pcall(function()
			datastore:SetAsync(player.UserId,data)
		end)

		if success then
			print("Success")
		end

		if errorMsg then
			print("Error found while trying to save data (are roblox datastore servers down?)"..errorMsg)
		end

	end)
end)

2 Likes

Here is a great beginner tutorial for implementing data stores, and explains every step of the process very easily:

1 Like

DataStores can only save utf8 elements so you can’t save instances in a datastore. To do this you will need to use serialization and deserialization

2 Likes