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

How would I save/load whole Table from Module script?

Asked by
B_rnz 171
5 years ago
Edited 5 years ago

Hello there! I have had multiple people tell me how I should :GetAsync()/SetAsync() Player data through the whole table and not key by key. To prevent throttle, yes, but I need a clear answer as to 'how' do I save/load data throughout the table. ( Because I am also experiencing data throttle using key-by-key method when player leaves. Which also causes my studio to crash.)

Module Script:

return {
    PlayerData = { Coins = 0, Diamonds = 0, Experience = 0, Kills = 0, Level = 0, EXPneeded = 0 }
}

Saving-Data Script:

local Datastore = game:GetService("DataStoreService"):GetDataStore("PlayerDataV1");
local DataModule = require(game.ReplicatedStorage.PlayerData)

game.Players.PlayerAdded:Connect(function(plr)
    Datastore:GetAsync(plr.UserId,DataModule.PlayerData)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    Datastore:SetAsync(plr.UserId,DataModule.PlayerData) -- the 'Coins = 0' when value changed not Setting when player leaves.
end)

(Extra Info if needed)

• The 'Coins = 0' when the value is changed, doesn't set when player leaves. So something is wrong with SetAsync.

0
A) Its a module script. Which means its not in each individual player B) It is just return and nothing is connecting to the script? Also its easier to assign stats to each player individually. voidofdeathfire 148 — 5y
0
Also note that if you use api access on studio theres a slight chance your studio might crash. voidofdeathfire 148 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

The answer is quite simple, you just need to directly save the table (if it is a dictionary) to a datastore like such:

local DataStore = game:GetService("DataStoreService"):GetOrderedDataStore("PlayerDataV1");
local DataModule = require(game.ReplicatedStorage.PlayerData)
Datastore:SetAsync(plr.UserId,DataModule.PlayerData)

However, if it is an array or an mixed table, this method probably won't be avaliable, as datastores have to be UTF_8 character

For the current type of table you have, this method should work

To get the data, you can simply do:

Datastore:GetAsync(plr.UserId)

which is a dictionary, so you would be able to get individual stats via a generic for loop

Hopefully this helped!

0
Wait, I did an test on the GetAsync() and SetAsync() using print(yes/no). For the GetAsync part I got yes, but when it SetAsync I get a no.. What am I doing wrong? B_rnz 171 — 5y
0
Getasync() takes the values from the datasave while setasync() saves values voidofdeathfire 148 — 5y
0
Yeah, but the values from the dictionaries aren't really saving.. B_rnz 171 — 5y
0
can you explain what you mean by that? Dictionaries, along with all the values inside of them should be able to be saved in datastores theking48989987 2147 — 5y
View all comments (2 more)
0
Okay, I edited the post so I could be more clear. B_rnz 171 — 5y
0
But more importantly, when the value inside of dictionary is changed, it doesn't keep that value whenever the player leaves. B_rnz 171 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Well, I rewrote your code:

Here is the code:

local DataStore = game:GetService("DataStoreService") 
local ds = DataStore:GetDataStore("Staff")

game.Players.PlayerAdded:connect(function(player) 
    local leader = Instance.new('Folder',player)
    local Coins = Instance.new("IntValue",leader)
    local Diamonds = Instance.new("IntValue",leader)
    local Experience = Instance.new("IntValue",leader) 
    local Kills = Instance.new("IntValue",leader) 
    local Level = Instance.new("IntValue",leader) 
    local EXPNeeded = Instance.new("IntValue",leader)   
    leader.Name = 'Stats'
    Coins.Name = "Coins"
    Diamonds.Name = 'Diamonds'
    Experience.Name = "Experience"
    Kills.Name = 'Kills'
    Level.Name = "Level"
    EXPNeeded.Name = "EXPNeeded"

    EXPNeeded.Value = ds:GetAsync(player.UserId) or 1
    Level.Value = ds:GetAsync(player.UserId) or 1
    Kills.Value = ds:GetAsync(player.UserId) or 1
    Experience.Value = ds:GetAsync(player.UserId) or 1
    Diamonds.Value = ds:GetAsync(player.UserId) or 1
    Coins.Value = ds:GetAsync(player.UserId) or 1 
    while wait(1) do
        ds:SetAsync(player.UserId, Coins.Value) 
wait(1)
        ds:SetAsync(player.UserId, Level.Value) 
wait(1)
        ds:SetAsync(player.UserId, Kills.Value)
wait(1) 
        ds:SetAsync(player.UserId, Experience.Value) 
wait(1)
        ds:SetAsync(player.UserId, EXPNeeded.Value)
wait(1)
        ds:SetAsync(player.UserId, Diamonds.Value) 

    end
end)

Happy Scripting! I did test this and it does work. Comment if you have any more questions

0
Doing that just overrides the previous value you saved theking48989987 2147 — 5y
0
This works, but I already knew how to do this.. I just wanted a different way of coding. Thanks though! B_rnz 171 — 5y
0
depends if it overwrites then it did save. so it does its job voidofdeathfire 148 — 5y
0
but if it just saves one of the values, how does it do its job? What is the point of the other values then? theking48989987 2147 — 5y
0
um it saves all the values lol voidofdeathfire 148 — 5y

Answer this question