Cannot store Array in data store. Data stores can only accept valid UTF-8 characters

Well, I’ve seen some topics about this error on the DevForum but I don’t understand what I have to do

Script
local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local AFK = RS.Events.AFK
local Warn = RS.Events.AssetsLoadedOutput

local Titles

local DataStoreService = game:GetService("DataStoreService")

local TitlesData = DataStoreService:GetDataStore("TitlesData")

local function ChangeAFKProperty(Player, Value)
	Player:SetAttribute("AFK", Value)
	-- print("AFK = ", Value)
end

local function AssetsLoaded(Player)
	Player:SetAttribute("AssetsLoaded", true)
end

Players.PlayerAdded:Connect(function(Player)
	local stats = Instance.new("Folder")
	stats.Parent = Player
	stats.Name = "leaderstats"
	
	local Owls = Instance.new("IntValue")
	Owls.Value = 8000
	Owls.Name = "Owls"
	Owls.Parent = stats
	
	Titles = Instance.new("Folder")
	Titles.Name = "Titles"
	Titles.Parent = Player
	
	local Data = {"nothing"}
	local success, errormessage = pcall(function()
		Data = TitlesData:GetAsync(Player.UserId) 
	end)
	
	if success then 
		for _,Object in pairs(Data) do
			Object.Parent = Titles
		end
	else
		print("Error while getting your data")
		warn(errormessage)
	end
	
	Player:SetAttribute("AFK", false)
	Player:SetAttribute("InGame", false)
	Player:SetAttribute("AssetsLoaded", false)
	Player.CharacterAdded:Connect(function()
		Player:SetAttribute("InGame", false)
		Player:SetAttribute("AssetsLoaded", false)
	end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local TitleData = {}
	for _,Object in pairs(Titles:GetChildren()) do
		table.insert(TitleData, Object)
	end
	TitlesData:SetAsync(Player.UserId, TitleData)
end)

AFK.OnServerEvent:Connect(ChangeAFKProperty)
Warn.OnServerEvent:Connect(AssetsLoaded)

There are some extra things about the Game, but the main thing is that Datastore doesn’t save the Titles.

Context: When buying an item, the server adds the Item to the Player in a folder, the Client detects this and adds it to his Inventory with ChildAdded, and when the Player exits I want everything inside that Folder to save, but it gives this error.
What can I do?

You can’t save game objects to a datastore, you can save their name, or some other unique id, and when you load a player, check the name that contains his datastore and give him the right things

You’re trying to save instances into a Datastore, in the connection to PlayerRemoving.

Datastores can’t hold instances. To store them, we do serialization (storing the properties with a table). Basically, you need to serialize Object and then store it.

So I have to load the features and instantiate them? In this case, Titles.

yes, like

Save

TitlesData:SetAsync(Player.UserId, {
    "Title1",
    "Title2",
})
--[[
    You save a table of strings that the player has
]]

Load

local Data = TitlesData:GetAsync(Player.UserId)
for _, value in pairs(Data) do
   if value == "Title1" then
     -- TODO: Creating Title1 instance
   elseif value == "Title2" then
     -- TODO: Creating Title2 instance
   end
end
1 Like

Can’t you do x:GetFullName()? To get it into the table?

1 Like

Like, in general I just need the Name, nothing more, nothing less, is it possible for me to use the Array only to save the Name, does the DataStore accept it?

Yeah. If you want the path then x:GetFullName() probably works.

1 Like