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

Can Somebody help me Understand how to save,read and update Tables in DataStore?

Asked by 6 years ago
Edited 6 years ago

My Datastore is organized like:Datastore Name>Scope>PlayerId as key>Actual Data. So Key for each players data is their Id.

I know that When you set the key like I do it creates an empty list. What I'm having trouble with is reading this data and rewriting it.

I tried using Http Service and JSONDecode, JSONEncode but that way it saves one string and to access that you have to Decode and then take values from table manually. I just need help to know how to save tables correctly and do it in best way

This is my script which doesn't even work properly.

local datastore = game:GetService("DataStoreService")
hs = game:GetService("HttpService")
default_data = {["Money"] = 4000,
    ["Houses"] = {
        ["Standard"] = false,
        ["Normal"] = false,
        ["High"] = false,
        ["Lux"] = false
    }
    }

game.Players.PlayerAdded:connect(function(plr)
    ds1 = datastore:GetDataStore("GameData123","01")


    local leaderstats = Instance.new("Folder",plr)
    leaderstats.Name = "leaderstats"
    local money = Instance.new("IntValue", leaderstats)
    money.Name = "Coins"


    money.Value = ds1:GetAsync(tostring(plr.UserId))[1] or 0

    if money.Value == 0 then
        moneyy.Value = 4000
        ds1:SetAsync(tostring(plr.UserId), default_data)

    end



    player_money = ds1:GetAsync(tostring(plr.UserId))[1]--I try to get money
    player_houses = ds1:GetAsync(tostring(plr.UserId))[2]--I try to get Houses as table

    function Update(money, standard, normal, high, lux)
        money = money.Value
        local new_value = {
            ["Money"] = money,
            ["Houses"] = {
                ["Standard"] = standard,
                ["Normal"] = normal,
                ["High"] = high,
                ["Lux"] = lux
                }
            }       
        return new_value 
    end


    moneyy.Changed:connect(function()
        print("Changed")
        ds1:UpdateAsync(tostring(plr.UserId), Update(player_money, player_houses[1], player_houses[2], player_houses[3], player_houses[4]))--Then I use this values here.
    end)


end)

Answer this question