Hey there! I'm having a bit of trouble dealing with DataStores. While trying to initially set the values for a player, I am given the error: Array is not allowed in DataStore. I was wondering what I was doing wrong and if someone could inform me in which path to take to correct this. Thank you!
This is a regular script located in ServerScriptService if that is of any use.
local dataStore = game:GetService("DataStoreService"):GetDataStore("GeneralStatsTest") game.Players.PlayerAdded:connect(function(player) local key = "playerTest-"..player.userId local kills = Instance.new("IntValue", player) kills.Name = "Kills" local deaths = Instance.new("IntValue", player) deaths.Name = "Deaths" deaths.Value = 1 local credits = Instance.new("IntValue", player) credits.Name = "Credits" local getSaved = dataStore:GetAsync(key) if getSaved then kills.Value = getSaved[1] deaths.Value = getSaved[2] credits.Value = getSaved[3] else local valuesForSaving = {kills.Value, deaths.Value, credits.Value} dataStore:SetAsync(key,valuesForSaving) end end)
I'm guessing these are the lines that are erroring.
local valuesForSaving = {kills.Value, deaths.Value, credits.Value} dataStore:SetAsync(key,valuesForSaving)
The first line is what is causing your troubles. In order to format your data for saving, you would want it to read something like
local valuesForSaving = {Kills=kills.Value, Deaths=deaths.Value, Credits=credits.Value}
When you want to read the values, you would use this:
kills.Value=getSaved.Kills
DataStores can only save raw values, like strings or numbers. To save and load an array, you have to encode it into a string before saving, and then decode it upon retrieval. Thankfully, Roblox includes a tool for this: http://wiki.roblox.com/index.php?title=JSON
Also, when using GetAsync, you usually want to wrap it in a pcall(), just in case it fails. https://wiki.roblox.com/index.php?title=API:Class/GlobalDataStore/GetAsync