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

How do I get datastore to save arrays?

Asked by 6 years ago

I'm trying to save an array in the datastore but when I try to set a variable as the saved array, the values return as nil. Can someone tell me what I'm doing wrong?

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("okdata")

game.Players.PlayerAdded:connect(function(plr)
    local key = "id-"..plr.UserId
    local saves = ds:GetAsync(key)

    local points = Instance.new("NumberValue", plr)
    points.Name = "Points"
    points.Value = 0
    local class = Instance.new("StringValue", plr)
    class.Name = "Class"
    class.Value = "Fighter"
    local color = Instance.new("StringValue", plr)
    color.Name = "Color"
    color.Value = "Bright green"

    points.Value = saves[1]
    class.Value = saves[2]
    color.Value = saves[3]
end)

game.Players.PlayerRemoving:connect(function(plr)
    local key = "id-"..plr.UserId
    local savearray = {plr.Points.Value, plr.Class.Value, plr.Color.Value}
    ds:SetAsync(key,savearray)
end)
0
I see nothing wrong with this script... Weird.. GetGlobals 343 — 6y

1 answer

Log in to vote
0
Answered by
cabbler 1942 Moderation Voter
6 years ago

The first retrieved data will always be nil since you never saved. Prepare for all data to be nil. i.e. local saves = ds:GetAsync(key) or {}

Ad

Answer this question