Can't save dictionary/table

I’ve been trying to make a datastore and everything in the code seems to be correct, however I can’t save any arrays, tables, or dictionaries. I have already searched the forum and gotten the same answer time and time again. the same exact answer already implemented into my code. I have API services on, and I am genuinely stumped on what the problem could possibly be.

This script returns this error:

DataStoreService: CantStoreValue: Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters. API: SetAsync
local players = game:GetService("Players")

local datastoreService = game:GetService("DataStoreService")
local datastore = datastoreService:GetDataStore("PlayerData")

local repStorage = game:GetService("ReplicatedStorage")

local killers = repStorage:WaitForChild("Killers")
local survivors = repStorage:WaitForChild("Survivors")

players.PlayerAdded:Connect(function(plr)
	local selectedKiller = Instance.new("ObjectValue", plr)
	selectedKiller.Name = "Killer"
	selectedKiller.Value = killers.Copy

	local killerSkin = Instance.new("ObjectValue", plr)
	killerSkin.Name = "KillerSkin"
	--killerSkin.Value = skins.Killer.Copy.Brickbattler

	local selectedSurvivor = Instance.new("ObjectValue", plr)
	selectedSurvivor.Name = "Survivor"
	selectedSurvivor.Value = survivors.Jack

	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"

	local money = Instance.new("NumberValue", leaderstats)
	money.Name = "Money"

	local corruption = Instance.new("NumberValue", leaderstats)
	corruption.Name = "Corruption"

	local inRound = Instance.new("BoolValue", plr)
	inRound.Name = "InRound"
	
	local data
	local success, err = pcall(function()
		data = datastore:GetAsync(plr.UserId)
	end)
	
	if success and data then
		money.Value = data[1]
		selectedKiller.Value = data[2]
		selectedSurvivor.Value = data[3]
	else
		print("Player: " .. plr.Name .. " has no data.")
	end
end)

local function saveData(plr:Player)
	local dataToSave = {
		["Money"] = plr.leaderstats.Money.Value,
		["Killer"] = plr.Killer.Value,
		["Survivor"] = plr.Survivor.Value
	}
	
	local success, err = pcall(function()
		datastore:SetAsync(plr.UserId, dataToSave)
	end)
	
	if success then
		print("Data Saved, " .. plr.Name)
	else
		print("Data Not Saved, " .. plr.Name .. ", Error code " .. err)
	end
end

players.PlayerRemoving:Connect(function(plr)
	saveData(plr)
end)

game:BindToClose(function()
	for _, plr in pairs(players:GetPlayers()) do
		saveData(plr)
	end
end)

I’m guessing it’s because your trying to store something like a CFrame, color, etc. Datastores can only store basic types (numbers, strings, booleans, arrays, and dictionaries). So if you wanted to store a CFrame, you would have to save the X, Y, Z, as seperate values, like this:

local partCFrame = {
      ["X"] = 928,
      ["Y"] = 183,
      ["Z"] = 537,
}

(This is probably because of your plr.Killer, and plr.Survivor values, since I’m assuming those are player types, instead of saving the Killer and Survivor, save their userIds.)

Turns out the fix was to save the name of the Killer/Survivor rather than the Killer/Survivor itself, thanks for the help!

1 Like