Hey guys! I was wondering if you could help me here... modelTwitter is a folder and basically it has lots of different boolvalues and stringvalues etc. in it - I encode the values and put them in a table to save and only use one datastore request. The problem is, for some reason it doesn't always save and doesn't error :(
Any ideas why? It usually works but is not consistent enough to use at the moment!
Thanks!
for _, child in pairs(modelTwitter:GetChildren()) do for _, saved in pairs(utility:JSONDecode(tab)) do if rawequal(saved.Name, child.Name) then child.Value = saved.Value end end child.Changed:connect(function(property) local tab = {} for _, v in pairs(child.Parent:GetChildren()) do tab[#tab + 1] = {Name = v.Name, Value = v.Value} end local encoded = utility:JSONEncode(tab) dataStore:SetAsync(player.userId, encoded) end) end
PS: I am also trying to get a version that saves OnPlayerRemoving so I can save the folder either when they leave or onChanged depending on the data! If anyone can help me fix this one and help with an OnPlayerRemoving one I would be really grateful :)
The DataStore limit is something like 20 + 5 * #players per minute. I'm not sure if the 20 is correct, but the 5 is. That means, you could more or less, for every player, save their data 5 times per minute. Of course saving every 12s might be too quick, I prefer something like a whole minute.
DataStores also allow you to directly save/load tables, no need for JSON. There are some limits on that, but nothing too bad. You can use {Money=123,Title="Minion"} just fine.
This is some code I wrote that should work with your folder structure:
local function save(player) local folder -- GET FOLDER local data = {} for k,v in pairs(folder:GetChildren()) do data[v.Name] = v.Value end for i=1,3 do if pcall(DS.SetAsync,DS,player.userId,data) then return print("Data saved for",player) end end warn("Couldn't save data for",player) end local function load(player) local folder -- GET FOLDER local suc,data for i=1,3 do local s,e = pcall(DS.GetAsync,DS,player.userId) if s then suc,data = s,e break end end if suc and data then for k,v in pairs(data) do local val = folder:FindFirstChild(k) if val then val.Value = v end end return print("Data loaded for",player) elseif suc then -- Player doesn't have any saved data (yet) else warn("Couldn't load data for",player) end end local Players = game:GetService("Players") Players.PlayerAdded:connect(function(plr) -- CREATE FOLDER load(plr) end) Players.PlayerRemoving:connect(function(plr) save(plr) end) -- Auto-save every minute while wait(60) do for k,v in pairs(Players:GetPlayers()) do coroutine.wrap(save)(v) end end