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 6 years ago
Edited 6 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:

01local DataStoreService = game:GetService("DataStoreService")
02local ServerData = {0, 0, 0, 0, 0, 0 ,0}
03local DataStore1 = DataStoreService:GetDataStore("CnsData"..version)
04local DataStore2 = DataStoreService:GetDataStore("StrData"..version)
05local DataSto.... (you get the point)
06 
07local DataFetchSuccess,ErrorMessage = pcall(function()
08    ServerData[1] = DataStore1:GetAsync(tostring(Player.UserId))
09    ServerData[2] = DataStore2:GetAsync(tostring(Player.UserId))
10    ServerData[3] = DataStore3:GetAsync(tostring(Player.UserId))
11    ServerData[4] = DataStore4:GetAsync(tostring(Player.UserId))
12    ServerData[5] = DataStore5:GetAsync(tostring(Player.UserId))
13    ServerData[6] = DataStore6:GetAsync(tostring(Player.UserId))
14    ServerData[7] = DataStore7:GetAsync(tostring(Player.UserId))
15end)

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:

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

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

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

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