Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

3 comments

1

Try putting line 18 along with the values variable declaration outside the for loop.

0

Thank you so much

0

Put it as a answer and I will accept if you would like

1 answer

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.