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

Using Arrays For Data Store?

Asked by 5 years ago
Edited 5 years ago

I am in the process of developing a script that saves player data. This is a portion of how the data is loaded in when the player joins:

local DataStoreService = game:GetService("DataStoreService")
local ServerData = {0, 0, 0, 0, 0, 0 ,0}
local DataStore1 = DataStoreService:GetDataStore("CnsData"..version)
local DataStore2 = DataStoreService:GetDataStore("StrData"..version)
local DataSto.... (you get the point)

local DataFetchSuccess,ErrorMessage = pcall(function()
    ServerData[1] = DataStore1:GetAsync(tostring(Player.UserId))
    ServerData[2] = DataStore2:GetAsync(tostring(Player.UserId))
    ServerData[3] = DataStore3:GetAsync(tostring(Player.UserId))
    ServerData[4] = DataStore4:GetAsync(tostring(Player.UserId))
    ServerData[5] = DataStore5:GetAsync(tostring(Player.UserId))
    ServerData[6] = DataStore6:GetAsync(tostring(Player.UserId))
    ServerData[7] = DataStore7:GetAsync(tostring(Player.UserId))
end)

This system works fine. The data is loaded from the server into an array within the script. I would like to know if it is possible to place the data calls in an array like this:

local DataStore = {DataStoreService:GetDataStore("example"..version),   
                 DataStoreService:GetDataStore("example1"..version), ect}

With this I would think it's possible to load the data like this:

local DataFetchSuccess,ErrorMessage = pcall(function()
    for i = 1, 7 do
        ServerData[i] = DataStore[i]
    end
end)

This is how I originally had programmed it but nothing would save or load. Is there something I can fix to make this possible?

Answer this question