Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

"Cannot store Array in data store. Data stores can only accept valid UTF-8 characters." How to fix?

Uh hello, Also I tried to do a Value saving script also it saves multiple and I became this error: Cannot store Array in data store. Data stores can only accept valid UTF-8 characters. How could I fix this?

Script: l

ocal DS = game:GetService("DataStoreService")

local PetDS = DS:GetDataStore("PetDataStore")

local PetsTable = {}

local AUTOSAVE_INTERVAL = 60

  

game.Players.PlayerAdded:Connect(function(Player)

local PetsFolder = Instance.new("Folder")

PetsFolder.Name = "Pets"

PetsFolder.Parent = Player

local key = Player.UserId

PetsTable = PetDS:GetAsync()

if PetsTable then

for _,p in pairs (PetsTable) do

local In = Instance.new("IntValue")

In.Name = p["Name"]

In.Parent = PetsFolder

In.Value = p["Value"]

end

end

end)

  

game.Players.PlayerRemoving:Connect(function(Player)

for _,p in pairs (PetsTable) do

table.remove(PetsTable,p)

end

for _,i in pairs (Player.Pets:GetChildren()) do

if i then

table.insert(PetsTable,i)

PetsTable[i] = {}

table.insert(PetsTable[i],"Name")

table.insert(PetsTable[i], "Value")

PetsTable[i]["Name"] = i.Name

PetsTable[i]["Value"] = i.Value

print(PetsTable[i]["Name"])

PetDS:SetAsync(Player.UserId, PetsTable)

end

end

end)

Would be great!

1 comment

0

call SetAsync outside after the loop

1 answer

Since arrays aren't allowed in datastores, you can get around this by encoding them.

-- Change line 69

PetDS:SetAsync(Player.UserId, PetsTable) -- Not allowed 
PetDS:SetAsync(Player.UserId,game:GetService("HttpService"):JSONEncode(PetsTable)) -- Allowed

This will turn your array into a string, allowing it to be used in datastores.

This discussion is preserved as it was. New answers, comments, and votes are closed.