Error while saving data (Saving data from a folder)

Hi I know there is another post about this but I do not understand what it is saying at all.

I’m getting this error when I try to save:

DataStoreService: CantStoreValue: Cannot store in data store. Data stores can only accept valid UTF-8 characters. API: UpdateAsync, Data Store: playerData

Heres my code, DT is a folder in ReplicatedStorage that is a template for the folder the data will be saved in, it looks like this:

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("playerData")

local RS = game:GetService("ReplicatedStorage")
local PLRS = game:GetService("Players")

local DT = RS:WaitForChild("DataTemplate")

local keyPrefix = "ID_"

function save(plr)
	local key = keyPrefix .. tostring(plr.UserId)
	local data = {}
	
	for i, obj in pairs(plr:WaitForChild("Data"):GetDescendants()) do
		if obj.ClassName ~= "Folder" then
			table.insert(data, {
				obj.Name,
				obj.Value,
				obj.Parent,
				obj.ClassName
			})
		end
	end
	
	local success, err
	
	repeat
		success, err = pcall(function()
			DS:UpdateAsync(key, function()
				return data
			end)
		end)
		
		task.wait()
	until success
	
	if not success then
		warn("Failed to save data! Err Message: " .. tostring(err))
	end
end

function load(plr)
	local key = keyPrefix .. tostring(plr.UserId)
	local data
	local success, err
	
	repeat
		success, err = pcall(function()
			data = DS:GetAsync(key)
		end)
	until success or not PLRS:FindFirstChild(plr.Name)
	
	if not data then return end
	if success then
		for i, obj in ipairs(data) do
			local newInstance = Instance.new(obj[4])
			newInstance.Name = obj[1]
			newInstance.Value = obj[2]
			for a,v in pairs(plr:WaitForChild("Data"):GetDescendants()) do
				if v.Parent.Name == obj[3].Name then obj.Parent = v end
			end
		end
	else
		warn("Failed to load data! Error Message: " .. err)
	end
end

-----------------------

game.Players.PlayerAdded:Connect(function(plr)
	local dataFolder = DT:Clone()
	dataFolder.Parent = plr
	dataFolder.Name = "Data"
	load(plr)
end)

game.Players.PlayerRemoving:Connect(save)

game:BindToClose(function()
	for i, plr in ipairs(PLRS:GetPlayers()) do
		save(plr)
	end
end)
1 Like

Your data is encoded to JSON, which is text. Text is represented by computers under the 8-bit Unicode Transformation Format. There are certain things in memory that cannot be naturally represented as text, such as userdatas, instances, objects, and functions.

You’re attempting to store the parent of an instance, which is not something you can naturally do. Serialization is the process of converting and storing the least amount of information to an encodable format for reconstruction at a later date. You’ll need to serialize the parent, or structure your data to naturally represent a hierarchy

Got it, I’ll try something about it and see if it works.

Worked, thank you so much. It was as simple as changing .Parent to .Parent.Name while storing data.

This works for now, but there are better ways to represent your data. It will require a greater work from you, so as you grow, I recommend coming back to serialization and developing better methods of saving and loading different data

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.