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

Why is my DataStore only saving one value from the dictionary?

Asked by
sad_eyez 162
6 years ago
Edited 6 years ago

I am working on a datastore, and I it only saves one value, I need help figuring out why, I am not posting all the code, but this is the main part of it, I am using a dictionary for saving the values

Code:

function module.load(plr, stats, name)

    local dataService = game:GetService("DataStoreService"):GetDataStore(name)
    local leaderstats = plr:WaitForChild("leaderstats")

    local key = plr.UserId.. "-key"

    local savedValues = dataService:GetAsync(key)

    if savedValues then
        for index, value in pairs(savedValues) do
            leaderstats[tostring(index)].Value = value
        end
    else
        for index,value in pairs(stats) do
            local values = {}
            values[tostring(index)] = leaderstats[tostring(index)].Value
            dataService:SetAsync(key, values)
        end
    end

end

Here is the bit of code that I believe is messing up:

for index,value in pairs(stats) do
    local values = {}
    values[tostring(index)] = leaderstats[tostring(index)].Value
    dataService:SetAsync(key, values)
end

Heres a warning that is appearing:

16:53:10.544 - Request was throttled. Try sending fewer requests. Key = 83240285-key

1
Try putting line 18 along with the values variable declaration outside the for loop. RayCurse 1518 — 6y
0
Thank you so much sad_eyez 162 — 6y
0
Put it as a answer and I will accept if you would like sad_eyez 162 — 6y

1 answer

Log in to vote
1
Answered by
RayCurse 1518 Moderation Voter
6 years ago
Edited 6 years ago

Due to the nature of data stores, Roblox has set a limit on the number and speed of requests that can be made. This is why you are getting the error. Your for loop is indeed the source of the error as it is making a call to SetAsync() every time it loops. In addition, the values variable will only ever hold one value because it is being defined inside the for loop and will be reset on every iteration.

In order to fix this, your call to SetAsync() along with your values variable declaration should be placed outside the for loop. This would make the code look like this:

local values = {}
for index,value in pairs(stats) do
    values[tostring(index)] = leaderstats[tostring(index)].Value
end
dataService:SetAsync(key, values)

Ad

Answer this question