Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
2

What to do with my old game if the Roblox's Table-saving DataStore is deprecated?

Asked by 2 years ago

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.

1
This code would never have worked with a data store. You save a value in a table which is saved ie {24, 5, 3}. You then index this and try to acces .Name which is incorrect since this data was bit saved. This is for the print statment only. User#5423 5 — 2y
1
Nothing about datastore tables is deprecated. You've obviously SetAsync a number value in a different script. obv we're not gonna fix that cabbler 1937 — 2y
1
The error is saying that DataFile is a number, but you are treating it as a table. Check where you are saving scope to make sure you are saving it as a table. justoboy13 153 — 2y
0
What does your table look like? Dog2puppy 168 — 2y

1 answer

Log in to vote
1
Answered by 1 year ago

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. ;)

Ad

Answer this question