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

Datastoring tables not saving the value?

Asked by
B_rnz 171
5 years ago
Edited 5 years ago

I am Datastoring tables, having a bit of an error using :SetAsync() function. When I use the SetAsync function, it doesn't really set the PlayerData is the Module script for that player. I need help, the output doesnt output any errors.

Module Script:

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

Script:


local DataStore = game:GetService("DataStoreService"):GetOrderedDataStore("PlayerDataV1"); local DataModule = require(game.ReplicatedStorage.PlayerData) game.Players.PlayerAdded:Connect(function(plr) for i, v in pairs(DataModule.PlayerData) do DataStore:GetAsync(plr.UserId, v) end end) game.Players.PlayerRemoving:Connect(function(plr) for i, v in pairs(DataModule.PlayerData) do DataStore:SetAsync(plr.UserId, v) -- v; the value of the instances when looped through the table. end end)
0
Also, I test this out by changing the value of the Module script, as it is for NEW players. B_rnz 171 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

GetAsync will attempt to retrieve the stored value, but you aren't doing anything with the value it returns - it doesn't automatically put the data into the table, you have to do that explicitly: DataModule.PlayerData[i] = DataStore:GetAsync(plr.UserId, i) (note: don't use v in GetAsync since you want to send the key (which you named 'i'). ex, 'i' might be "Coins" and 'v' might be 0 - you want to say GetAsync(plr.UserId, "Coins"), not GetAsync(plr.UserId, 0)!)

However, for efficiency you should save/load the whole table at once, not key-by-key!

Read through https://developer.roblox.com/articles/Saving-Player-Data for a tutorial on how to do this.

0
So you're saying to save/load table one by one, and not have SetAsync()/GetAsync() save/load all values at once? B_rnz 171 — 5y
0
Thanks, this works! But I see what you mean, as in whenever I leave the game, it crashes/takes long time to load. So I am not going to save all at once. B_rnz 171 — 5y
0
The idea is to save a single table with many key/value pairs using a single SetAsync call. So long as all the keys are strings (or they're the numbers 1, 2, 3, ...), this will work. chess123mate 5873 — 5y
Ad

Answer this question