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

Why am I getting the output 'Cannot store Array in DataStore'?

Asked by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

I've been trying to test out DataStore, but I keep getting this error. It refers to both the saving and the loading script. It works fine in-game (saving to a value, loading from it), but it doesn't save to the DataStore, so the stats don't remain for the next time it's played.

I looked over the lines output referred to, but I couldn't figure it out. Could someone help me find out what's wrong with it?

Also, is there a better way to save data to DataStore than saving when the player leaves? Preferably on a button's mouseclick?

The saving script:

local DataStore = game:GetService("DataStoreService"):GetDataStore("PDS")

game.Players.PlayerRemoving:connect(function(player)

    local key = "player_" ..player.userId


    local valuesToSave = {player.leaderstats.Level.Value, player.GameInfo.Experience.Value, player.GameInfo.Coins.Value, player.GameInfo.Color.Value, player.GameInfo.SaveData.Value}
    DataStore:SetAsync(key, valuesToSave)

end)

The loading script:

local DataStore = game:GetService("DataStoreService"):GetDataStore("PDS")

game.Players.PlayerAdded:connect(function(player)
    repeat wait() until player.Character

    local stats = Instance.new("IntValue", player)
    stats.Name = "leaderstats"

    local level = Instance.new("IntValue", stats)
    level.Name = "Level"

    local gameinfo = Instance.new("IntValue", player)
    gameinfo.Name = "GameInfo"

    local xp = Instance.new("NumberValue", gameinfo)
    xp.Name = "Experience"

    local coins = Instance.new("NumberValue", gameinfo)
    coins.Name = "Coins"

    local color = Instance.new("BrickColorValue", gameinfo)
    color.Name = "Color"

    local gamedata = Instance.new("BoolValue", gameinfo)
    gamedata.Name = "SaveData"

    local key = "player_" ..player.userId

    local savedValues = DataStore:GetAsync(key)

    if savedValues then
        -- Save Format: {level, xp, coins, color, data}
        level.Value = savedValues[1]
        xp.Value = savedValues[2]
        coins.Value = savedValues[3]
        color.Value = savedValues[4]
        gamedata.Value = savedValues[5]
    else
        local valuesToSave = {level.Value, xp.Value, coins.Value, color.Value, gamedata.Value}
        DataStore:SetAsync(key, valuesToSave)
    end

end)

Thanks in advance!

EDIT: I also got an output about HTTP 404.

0
Note: I did test this at the actual place rather than studio, the output was the same. Pyrondon 2089 — 8y

Answer this question