How to save Arrays?

So, i want salve all StringsValues of this Folder

Screenshot_6

I tried many methods, but no one works

Someone can help me? Thanks <3

4 Likes

You need to serialise it into an actual table.

Folders and instance values are not saveable into DataStores.

You can take a look at resources on the Developer Hub or search for similar posts on the forums.

You can use

local data = {}
for i,v in pairs(plr.Stats.Owned.Backpack:GetChildren() do
    data[v.Name] = v.Value
end

to save it as a dictionary.
Then you can use datastores to save the dictionary.

Thx, i tried this but dont work

What is wrong ?

local ServerStorage = game:GetService("ServerStorage")
local DataStoreService = game:GetService("DataStoreService")
local islandData = DataStoreService:GetDataStore("IslandData")

game.Players.PlayerAdded:Connect(function(player)
	
	local IslandsUnlocked = Instance.new("Folder")
	IslandsUnlocked.Name = "IslandsUnlocked"
	IslandsUnlocked.Parent = player
	
	local IslandsToSave = {}
	
    for i,v in pairs(IslandsUnlocked:GetChildren()) do
    IslandsToSave[v.Name] = v.Value
    end

    local PlayerUserId = "player_"..player.UserId

    local iData

    local islandsucess, islanderror = pcall(function()
	    iData = islandData:GetAsync(PlayerUserId, IslandsToSave)
	end)

    if islandsucess then
	
	print("Island Sucess")
	
	for _,x in pairs(IslandsToSave) do
		local Variable = Instance.new("StringValue")
		Variable.Parent = IslandsUnlocked
		Variable.Name = x
		Variable.Value = x
	end
	
	else
		
		warn(islanderror)
	
	end
	
end)

local function SaveData(player)
	
	local PlayerUserId = "player_"..player.UserId
	
	local IslandsToSave = {}
	
	for i,v in pairs(player.IslandsUnlocked:GetChildren()) do
		IslandsToSave[v.Name] = v.Value
	end
	
	local iData
	local islandsucess, islanderror = pcall(function()
		iData = islandData:SetAsync(PlayerUserId, IslandsToSave)
	end)
	
	if islandsucess then
		
		print("Sucess saved island")
		
		for _,x in pairs(IslandsToSave) do
			local Variable = Instance.new("StringValue")
			Variable.Parent = player.IslandsUnlocked
			Variable.Name = x
			Variable.Value = x
		end
		
	else
		
		warn(islanderror)
		
	end

end

game.Players.PlayerRemoving:Connect(function(player)
	SaveData(player)
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		SaveData(player)
	end
end)

I was about to ask this too, I’m making a forum thing where it stores a table in datastore service, and I get an error saying “Cannot store Array in data store. Data stores can only accept valid UTF-8 characters”

pcall() only returns one value, which would be success. GetAsync() only takes one parameter: the key. The for loop going through IslandsToSave have variables storing the x and y of IslandsToSave dictionary. Theres more stuff that needs to be fixed; that’s just some.

try i,v in next instead of i,v in pairs

pcall can return two or more variables: the success boolean and anything that is returned by the function passed into it, assuming it did not error.

function returnsNothing()
	print("f1")
end

function returnsValue()
	print("f2")
	return 100
end

function returnsTuple()
	print("f3")
	return "hello", "world"
end

function runtimeError()
	print("f4")
	game.Workspace:Cross(Vector3.new())
end

local a, b, c

a, b = pcall(returnsNothing)
print(a) --> true
print(b) --> nil

a, b = pcall(returnsValue)
print(a) --> true
print(b) --> 100

a, b, c = pcall(returnsTuple)
print(a) --> true
print(b) --> hello
print(c) --> world

a, b = pcall(runtimeError)
print(a) --> false
print(b) --> Cross is not a valid member of Workspace
1 Like