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

[Solved]Datastore - Change data inside a datastore where the data is inside a table. Help?

Asked by 6 years ago
Edited 6 years ago

I have been stuck with this problem for about five hours or so...

First time I asked this question, I was incredible tired, that's why I wanna ask the question with a little more explanation of my problem.

Don't worry. I have deleted my first post. No spam. ^^

My problem is:

  • I wanna get the data from a datastore and change something in it.
  • The data that is stored is a table. I wanna change specified parts of the table. Here is my code:
local DataStore = game:GetService("DataStoreService")
local DataList = DataStore:GetDataStore("UserData")

function savedata(dataname, playerid, value)
    DataStore:GetDataStore(dataname):SetAsync(playerid, value)
    --print("Datastore: ", value)
end

--This is the default data, will be set for new players
DefaultData = { 
    ["Level"] = 2,
    ["Xp"] = 0,
    ["Coins"] = 0,
    ["Gems"] = 0,
};




--Setting default data to new players
game:GetService("Players").PlayerAdded:Connect(function(plr) 
    pcall(function()
        local Data = DataList:GetAsync(plr.UserId)

        if Data == nil then
            Data = DefaultData
        end
        savedata("UserData", plr.UserId, Data)
    end)
end)


--This is what I've tried to do, but totally failed with
local Data = DataList:GetAsync(plr.UserId)
savedata(Data.Xp, plr.UserId, 100)
savedata(Data["Xp"], plr.UserId, 100)

I understand that I made some questions in my last post, and I want to answer them here.


Q. Do you want to change the data inside the "DefaultData" table?

A. No, I do not want to change the data inside the "DefaultData" table. I wanna change the data inside the table inside the "UserData" datastore.


Q. Do you want a way to change the data after its created

A. Yes, In a way, I want that. I want to get the data from the datastore, and change the data that I get from the datastore, in other words, save it with different information.


Q. Do you need help with getting the values inside the table?

A. No. I know how to get the values, but not how to change it.

Thanks in advance

~WaterFoox

1 answer

Log in to vote
0
Answered by 6 years ago

This?

local Data = DataList:GetAsync(plr.UserId)

Data.XP = 100
0
The problem is, that you now have to save it back up to the datastore. Could you show me how? On line 4 to 7 I made a function to save it back up, and if you look at the bottom of my code, I am calling that function, but for some reason, I can't get it right. TheWaterFoox 255 — 6y
0
I just solved it myself. Heh. Thank you for adding an answer anyway. c; TheWaterFoox 255 — 6y
Ad

Answer this question