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

Datastore table JSON encoding not working consistently? [UNANSWERED]

Asked by 9 years ago

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!

01for _, child in pairs(modelTwitter:GetChildren()) do
02       for _, saved in pairs(utility:JSONDecode(tab)) do
03           if rawequal(saved.Name, child.Name) then
04               child.Value = saved.Value
05           end
06       end
07child.Changed:connect(function(property)
08           local tab = {}
09 
10           for _, v in pairs(child.Parent:GetChildren()) do
11               tab[#tab + 1] = {Name = v.Name, Value = v.Value}
12           end
13 
14           local encoded = utility:JSONEncode(tab)
15           dataStore:SetAsync(player.userId, encoded)             
16       end)
17   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 :)

1 answer

Log in to vote
1
Answered by
einsteinK 145
9 years ago

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:

01local function save(player)
02    local folder -- GET FOLDER
03    local data = {}
04    for k,v in pairs(folder:GetChildren()) do
05        data[v.Name] = v.Value
06    end
07    for i=1,3 do
08        if pcall(DS.SetAsync,DS,player.userId,data) then
09            return print("Data saved for",player)
10        end
11    end
12    warn("Couldn't save data for",player)
13end
14 
15local function load(player)
View all 49 lines...
Ad

Answer this question