IncrementAsync is not saving

local success, new = pcall(function()
return test:IncrementAsync(tostring(userId), tonumber(saving))
end)

I tried without pcall and the dev console error code said 502 etc.

Analysis


The error appears to be internal.

Error X occurred when processing on Roblox servers. Depending on the response, you may want to retry the request at a later time.

Source


Can you give more info on what you’re trying to save or what the rest of your code looks like? The error you got (502) means that Roblox couldn’t save anything to DataStore.

A tip in the future - you should wrap the actual method with a pcall rather than an anonymous function. It looks cleaner and helps out with readability.

local success, result = pcall(test.IncrementAsync, test, tonumber(saving))

It would also be more helpful if you explicitly explained your problem. Saying that you got “error 502 code etc” isn’t helpful enough. That being said, your console should give you the information you need to attempt to solve this request.

Error Code 502 is “API services rejected with error: e”, where e is the reason as to why the request was rejected. Typically this comes along with an HTTP 403 access denied status call, at least from my experience with DataStore errors.

The only root cause I know for an EC502/HTTP403 combination is that you are attempting to use DataStores in a team create for a place that you are not the owner of. This is done for security purposes. If you are using a team create, you should request the owner to handle DataStores.

Another issue could be that you do not have Studio API Access enabled, though that should be a straightforward error for you to resolve - simply go to the configure page of the game (not the place) and toggle it in Basic Settings.

image

If none of this fixes your problem, I apologise, but that’s the problem with providing vague information on your thread. Always try to be as detailed as you can with your problem and include relevant information so we can help you solve your issue. A screenshot of the console would help the most.

1 Like

Btw the game is mine and API is enabled.Here’s the part of my code, just experimenting Data stores.

elseif type == “increment” then
local success, new = pcall(function()
return test:IncrementAsync(userId, 1)
end)
if success then
infoText(“Saved”, 0, 255)
else
infoText(“No Data”, 255)
end

I got No Data, so I tried to take out pcall so I can get a error code and take pic of it.

It seems your original value your trying to increment isn’t a number. The data store is erroring because it’s trying to increment something that can’t be incremented.

I replaced the variable and put a number 1 in the value, and still doesn’t save with same code.

Print the return of test:GetAsync(userId). If it’s not a number, use SetAsync to force it to a numerical value or RemoveAsync to purge it. Once that’s done, try running your increment code again. The error code has a very literal comment in it - Existing Value Not Numeric. IncrementAsync will not succeed if it tries to increment a non-number.

Even if you set the increment amount as 1, it still won’t work if the existing value of that key is non-numerical (hence the name IncrementAsync). It will try to add 1 to the existing value but will fail since the existing value is non-numerical.

As colbert2677 said, you can use GetAsync() to see whether the existing value of the key is a number or not, then either use RemoveAsync() to get rid of that key and make a new one with the value actually being numerical, or use SetAsync() and force the value to be numerical.

EDIT: If you do call GetAsync() to see if the value is a number, make sure its formatted as one. It’s possible that it might return a number, but its formatted as a string. Just use tonumber() to format it as a number instead of a string and then SetAsync() afterwards.