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.
01 | local datastore = game:GetService( "DataStoreService" ) |
02 | hs = game:GetService( "HttpService" ) |
03 | default_data = { [ "Money" ] = 4000 , |
12 | game.Players.PlayerAdded:connect( function (plr) |
13 | ds 1 = datastore:GetDataStore( "GameData123" , "01" ) |
16 | local leaderstats = Instance.new( "Folder" ,plr) |
17 | leaderstats.Name = "leaderstats" |
18 | local money = Instance.new( "IntValue" , leaderstats) |
22 | money.Value = ds 1 :GetAsync( tostring (plr.UserId)) [ 1 ] or 0 |
24 | if money.Value = = 0 then |
26 | ds 1 :SetAsync( tostring (plr.UserId), default_data) |
32 | player_money = ds 1 :GetAsync( tostring (plr.UserId)) [ 1 ] |
33 | player_houses = ds 1 :GetAsync( tostring (plr.UserId)) [ 2 ] |
35 | function Update(money, standard, normal, high, lux) |
40 | [ "Standard" ] = standard, |
50 | moneyy.Changed:connect( function () |
52 | ds 1 :UpdateAsync( tostring (plr.UserId), Update(player_money, player_houses [ 1 ] , player_houses [ 2 ] , player_houses [ 3 ] , player_houses [ 4 ] )) |