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

Cannot store Array in datastore. Datastores can only accept valid UTF-8 characters. How to fix this?

I'm trying to save datastores with tables, the only error is "Cannot store Array in data store. Data stores can only accept valid UTF-8 characters." (Line 54)

Here is my entire script:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TestDataStore = DataStoreService:GetDataStore("emp43")

local data = {
	["Steps"] = 0,
	["Level"] = 1,
	["Races"] = 0
}

Players.PlayerAdded:Connect(function(Player)
	local DataFromStore = nil
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	for name, value in pairs(data) do
		local new = Instance.new("NumberValue")
		new.Name = name
		new.Value = value
		new.Parent = leaderstats
	end
	
	local s, e = pcall(function()
		DataFromStore = TestDataStore:GetAsync("uid-" .. Player.UserId)
	end)
	
	if s then
		print("Getting Data For: " .. Player.Name)
	elseif e then
		warn("Error Getting Data For: " .. Player.Name)
	end
	
	if DataFromStore then
		for name, value in pairs(DataFromStore) do
			Player.leaderstats[name].Value = value
		end
		print("Data Loaded For: " .. Player.Name)
	else
		print("Created New Data For: " .. Player.Name)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local DataForStore = {}
	
	for name, value in pairs(Player.leaderstats:GetChildren()) do
		table.insert(DataForStore, value)
		DataForStore[value.Name] = value.Value
		print(unpack(DataForStore))
	end
	
	TestDataStore:SetAsync("uid-" .. Player.UserId, DataForStore)
end)

1 answer

✓ Accepted answer

Alright, here's an edited answer with a script to fufull your needs, it just simplifies your current script a bit.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TestDataStore = DataStoreService:GetDataStore("emp43")

local data = {
    ["Steps"] = 0,
    ["Level"] = 1,
    ["Races"] = 0
}

Players.PlayerAdded:Connect(function(Player)
	for i,v in pairs(data) do
		local new = Instance.new("NumberValue") --Or use an IntValue, preference based.
		new.Name = i
		new.Parent = leaderstats --Assuming you are using leaderstats folder.
		new.Value = TestDataStore:GetAsync(Player.UserId) or v
	end
	Players.PlayerRemoving:Connect(function(Player)
		local savedata = function(Player)
			for name,value in pairs(Player.leaderstats:GetChildren()) do
				return value
			end
		end
		TestDataStore:SetAsync(Player.UserId, savedata(Player))
	end)
	while wait(60) -autosave every 60 seconds
		local savedata = function(Player)
			for name,value in pairs(Player.leaderstats:GetChildren()) do
				return value
			end
		end
		TestDataStore:SetAsync(Player.UserId, savedata(Player))
	end
end)

Adjust the script to your needs

7 comments

0

ok

0

it still isnt working

0

Did you adjust your script to your needed settings? If not, maybe check what's going wrong

0

The error went away now, but it isnt saving the data

0

Are your API Services on?

0

yes

0

I'm going to edit my answer with a script that simplifies everything and hopefully fufills your needs.

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