Can't save an array to datastore?

I’m trying to save a table full of owned ranks and for some reason it isn’t working. It keeps on saying I can’t save a table even though I’m clearly saving a table full of values within the same script. Am I doing something wrong? .I tried saving the table to a seperate datastore but no difference is made. It keeps on saying it can only accept UTF-8 characters.

How would I fix this, the datastore is a fundamental part of my game and with this error occuring, its breaking other parts too.

Code:

local DS = game:GetService("DataStoreService") --- Gets Datastore Service
local CSS = DS:GetDataStore("ChaosSpleefStore_1")
local OwnedDatastore = DS:GetDataStore("OwnedDatastore")
local ownedPlaceHolder = {}
game.Players.PlayerAdded:Connect(function(plr)
	local owned = {}
	

	local ldbrd = Instance.new("Folder")
	ldbrd.Name = "leaderstats"
	ldbrd.Parent  = plr
	
	local CurrentlyEquipped =Instance.new("StringValue")
	CurrentlyEquipped.Parent = plr
	CurrentlyEquipped.Name = "CE"
	
	local benefits = Instance.new("IntValue")
	benefits.Parent = plr
	benefits.Name = "Benefits"

	local spleefCoins = Instance.new("IntValue")
	spleefCoins.Parent = plr
	spleefCoins.Name = "SpleefCoins"

	local wins = Instance.new("IntValue")
	wins.Parent = ldbrd
	wins.Name = "Wins"
	

	local playeruserid = "Player_"..plr.UserId
	local data = {}
	local owned
	local s, e = pcall(function()
		data = CSS:GetAsync(playeruserid)
		owned = OwnedDatastore:GetAsync(playeruserid)
	end)
	wait()
	if s then
		if data ~= nil  and owned ~= nil then
			spleefCoins.Value = data[1] 
			wins.Value = data[2]
			benefits.Value = data[3] or 1
			CurrentlyEquipped.Value = data[4] or require(game.ReplicatedStorage.ListOfAllRanks)[1]
			ownedPlaceHolder = owned
			game.ReplicatedStorage.SendOwnedThings:FireClient(plr, ownedPlaceHolder)

		else
			spleefCoins.Value = 0
			wins.Value = 0
		end
	else
		error(e)
	end

end)

game.ReplicatedStorage.UpdateOwned.OnServerEvent:Connect(function(ownedThing)
	ownedPlaceHolder = ownedThing
end)

function Save()
	local players = game.Players:GetPlayers()
	for _, player in pairs(players) do
		local userId = "Player_"..player.UserId
		local data = {player.SpleefCoins.Value, player.leaderstats.Wins.Value, player.Benefits.Value, player.CE.Value, ownedPlaceHolder}
		if data then
			-- wrap in pcall to handle any errors
			local success, result = pcall(function()
				-- SetAsync yields so will stall shutdown (@-@)
				CSS:SetAsync(userId, data)
				OwnedDatastore:SetAsync(userId, ownedPlaceHolder)
			end)
			if not success then
				warn(result)
			else
				return true
			end   
		end
	end


end

while wait(10) do
	if Save()then
		print("Saved Data")
	end
end
game.Players.PlayerRemoving:Connect(function(player)
	Save()
end)

game:BindToClose(Save)

Help?

P.S.This is all handled in one script

1 Like

I think you need to have a dictionary rather than an array, ie you need to have strings for keys in your data table rather than numbers.

You currently have

"data = {player.SpleefCoins.Value, player.leaderstats.Wins.Value, player.Benefits.Value, player.CE.Value, ownedPlaceHolder} "

which should be something like

" data = {Coins = player.SpleefCoins.Value, Wins = player.leaderstats.Wins.Value, ...}"

instead.

1 Like

DataStores accept either. It’s actually recommended to use an array instead as the data cost would be smaller. However, I’m not sure why there’s a UTF error.

1 Like