So I have an old survival game back in 2017, I've created a DataStore script that saves tables:
You may have the full script here:
local Storage = game:GetService('ReplicatedStorage') local ItemsFolder = Storage:WaitForChild('ItemsFolder') local DataStoreService = game:GetService("DataStoreService") local InventoryData = DataStoreService:GetDataStore("PlayerInventory") local ItemsList = {} for i,item in pairs(ItemsFolder:GetChildren()) do table.insert(ItemsList,item.Name) end game.Players.PlayerAdded:connect(function(plr) local scope = "Player_"..plr.UserId local InvFolder = Instance.new('Folder') InvFolder.Name = "InventoryFolder" local PlrItemObj = {} for i=1,#ItemsList do local Newvalue = Instance.new("IntValue") Newvalue.Name = ItemsList[i] table.insert(PlrItemObj,Newvalue) Newvalue.Parent = InvFolder end local DataFile = InventoryData:GetAsync(scope) if DataFile then for i=1,#PlrItemObj do print("Name: "..PlrItemObj[i].Name.." / Value:"..PlrItemObj[i].Value) PlrItemObj[i].Value = DataFile[i] end else local DataToSave = {} for i=1,#PlrItemObj do table.insert(DataToSave,PlrItemObj[i].Value) end InventoryData:SetAsync(scope,DataToSave) end InvFolder.Parent = plr end)
At the line "PlrItemObj[i].Value = DataFile[i]"
I get this error "attempt to index local 'DataFile' (a number value)"
So is there another way to fix the DataStore?
P/s: If you're suggesting to save the data values one by one then you can't since the game have more than 100 items then it would throttle the firing due to limited DataStoreService requests.
I would recommend turning it into JSON format right before saving it via datastore. Once the data is called you can convert it back to its table form.
A really good article on how to do this is through the following: https://www.robloxdev.com/api-reference/function/HttpService/JSONEncode
If you have any questions or issues, please contact me. ;)