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)
