Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Array is not allowed in DataStore?

Asked by 6 years ago

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)

2 answers

Log in to vote
0
Answered by
lukeb50 631 Moderation Voter
6 years ago

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
0
Arrays can't be saved in DataStores. whenallthepigsfly 541 — 6y
0
Exactly. Try reading the wiki and or actually writing code and you may be surprised as to what an Array is. lukeb50 631 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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

Answer this question