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.
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!
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