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?

22:10:43.925 - Cannot store Array in DataStore 22:10:43.931 - Stack Begin 22:10:43.934 - Script 'ServerScriptService.Store', Line 57 22:10:43.935 - Stack End

local DataStore = game:GetService("DataStoreService")
local Ds1 = DataStore:GetDataStore("Characters")
game.Players.PlayerAdded:connect(function(Player)
	Player:WaitForChild("Backpack")

	      local Gstuff = Instance.new("Folder",Player.Backpack)
	      Gstuff.Name = "Gamestuff"
	      local Chars = Instance.new("Folder",Gstuff)
	      Chars.Name = "Characters"
	     local Create = Instance.new("Folder",Chars)
	      Create.Name = "Folder"
		local NumChars = Instance.new("IntValue",Chars)
	      NumChars.Name = "Number"
	     local BodyColors = Instance.new("BrickColorValue",Create)
	           BodyColors.Name = "BColors"
	           BodyColors.Value = BrickColor.new("Brick yellow")
	     local HairColor = Instance.new("BrickColorValue",Create)
	           HairColor.Name = "HairColor"
	           HairColor.Value = BrickColor.new("Buttermilk")
	     local HairId = Instance.new("StringValue",Create)
               HairId.Name = "HairId"
	           HairId.Value = "Bald"
	     local Shirt = Instance.new("StringValue",Create)
	           Shirt.Name = "Shirt"
	     local Pants = Instance.new("StringValue",Create)
	           Pants.Name = "Pants"
	     local Face = Instance.new("StringValue",Create)
	           Face.Name = "Face"
	           Face.Value = "rbxassetid://167133081"
	     local Gender = Instance.new("StringValue",Create)
	           Gender.Name = "Gender"
	           Gender.Value = "Male"
	     local Class = Instance.new("StringValue",Create)
	           Class.Name = "Class"
	     local Level = Instance.new("IntValue",Create)
	           Level.Name = "Level"
	           Level.Value = 0
	
	 local key = "Player-"..Player.userId
	 local SavedValues = Ds1:GetAsync(key)
	
	 if SavedValues then
		--Save Format -- {BodyColors,HairColor,HairId,Shirt,Pants,Face,Gender,Class,Level,NumChars}
		BodyColors.Value = SavedValues[1]
		HairColor.Value = SavedValues[2]
		HairId.Value = SavedValues[3]
		Shirt.Value = SavedValues[4]
		Pants.Value = SavedValues[5]
		Face.Value = SavedValues[6]
		Gender.Value = SavedValues[7]
		Class.Value = SavedValues[8]
		Level.Value = SavedValues[9]
		Level.Value = SavedValues[10]
	else 
		local ValuesToSave = {BodyColors.Value,HairColor.Value,HairId.Value,Shirt.Value,
		Pants.Value,Face.Value,Gender.Value,Class.Value,Level.Value,NumChars.Value}
		Ds1:SetAsync(key, ValuesToSave)
	end
	end)

2 answers

✓ Accepted answer

You are trying to store a BrickColor data constructor. A table is stored in a JSON format and a BrickColor object cannot be stored in a JSON.

We can simply have to save the data that is contained within the object, the simplest method is to use the bricks name as we can use the following constructor for the BrickColor object BrickColor.new(string val).

Loading example:-

BodyColors.Value = BrickColor.new(SavedValues[1]) -- this uses the string constructor to make the brickcolor

Saving example:-

local ValuesToSave = {BodyColors.Value.Name} -- this gets the BrickColor objects and accesses its name which is what we will save, this name is a string
Ds1:SetAsync(key, ValuesToSave)

I hope this helps.

You're allowed to save array's in dataStores, you're just not allowed to save Userdata.

The only things that can be in the array that you save are strings, numbers, and booleans.

You'd have to save your stuff like this:

Data = {
	Level = Level.Value,
	HairColor = {
		HairColorValue.Value.r,
		HairColorValue.Value.g,
		HairColorValue.Value.b
	}
	--etc
}

For loading we can then index it like so:

local level = Data[1]
local HairColor = Color3.new(Data[2][1],Data[2][2],Data[2][3]) -- or fromRGB. Not sure.
--etc

Any questions? Post a comment below ;)

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