GetAsnyc not returning correct result

  1. What do you want to achieve?
    I want to create a autosaving system

  2. What is the issue?
    The data is not saving, and theres no error message

  3. What solutions have you tried so far?
    I did try pcalling and retrying and HttpService:JSONEncode, but none of these working

My script:

local datatosave = {
			[1] = donateddata.Value;
			[2] = datafold.IsNew.Value;
			[3] = datafold.Weapon.Value;
			[4] = datafold.Equipped.Hair.Value;
			[5] = datafold.Equipped.Leg.Value;
			[6] = {datafold.Equipped.LegColor.Value.r, datafold.Equipped.LegColor.Value.g, datafold.Equipped.LegColor.Value.b};
			[7] = datafold.Equipped.Shirt.Value;
			[8] = {datafold.Equipped.ShirtColor.Value.r, datafold.Equipped.ShirtColor.Value.g, datafold.Equipped.ShirtColor.Value.b};
			[9] = {datafold.Equipped.SkinColor.Value.r, datafold.Equipped.SkinColor.Value.g, datafold.Equipped.SkinColor.Value.b};
			[10] = ownedskins; --table
			[11] = equippedskins; --table
			[12] = ownedweapons --table
		} --creating a table that includes player datas
		table.foreach(datatosave[12], print) --printing out ownedweapons
		local function TryUntilSuccess(waittime, callback)
			local success, response
			local count = 0
			repeat
				if count >= 1 then
					warn("Retrying, count:", count, " Error:", response)
					task.wait(waittime)
				end
				success, response = pcall(callback)
				count = count + 1
			until success
			return success, response
		end
		TryUntilSuccess(7, function()
			gameData:SetAsync(plrDataKey, datatosave) -- trying to save the data
		end)
		table.foreach(gameData:GetAsync(plrDataKey)[12], print) -- printing out saved data

it seems have no problem, i tried to run it but theres output.

  --below is whats inside 12 in my data table
  21:42:46.937  1 true  -  Server - Datas:109
  21:42:46.937  3 true  -  Server - Datas:109
 --below is 12 in data table which is what am i getting with GetAsync
  21:42:47.986  1 true  -  Server - Datas:126

You see, SetAsync is not throwing any error but the GetAsync is not returning the result im looking for
(sorry for bad english, im not a english speaker)

I believe you need to serialize your table so its a single string value.
https://developer.roblox.com/en-us/api-reference/function/HttpService/JSONEncode

Edit: Then of course de-serialize it when you get it back from the datastore.

I tried and I got the same result as before

gameData:SetAsync(plrDataKey,HttpService:JSONEncode(datatosave))

then:

local myData = gameData:GetAsync(plrDataKey)
print(HttpService:JSONDecode(myData))

script:

local datatosave = {
			[1] = donateddata.Value;
			[2] = datafold.IsNew.Value;
			[3] = datafold.Weapon.Value;
			[4] = datafold.Equipped.Hair.Value;
			[5] = datafold.Equipped.Leg.Value;
			[6] = {datafold.Equipped.LegColor.Value.r, datafold.Equipped.LegColor.Value.g, datafold.Equipped.LegColor.Value.b};
			[7] = datafold.Equipped.Shirt.Value;
			[8] = {datafold.Equipped.ShirtColor.Value.r, datafold.Equipped.ShirtColor.Value.g, datafold.Equipped.ShirtColor.Value.b};
			[9] = {datafold.Equipped.SkinColor.Value.r, datafold.Equipped.SkinColor.Value.g, datafold.Equipped.SkinColor.Value.b};
			[10] = ownedskins;
			[11] = equippedskins;
			[12] = ownedweapons
		} --creating a table that includes player datas
		table.foreach(datatosave[12], print) --printing out ownedweapons
		local function TryUntilSuccess(waittime, callback)
			local success, response
			local count = 0
			repeat
				if count >= 1 then
					warn("Retrying, count:", count, "\nError:", response)
					task.wait(waittime)
				end
				success, response = pcall(callback)
				count = count + 1
			until success
			return success, response
		end
		TryUntilSuccess(7, function()
			gameData:SetAsync(plrDataKey, http:JSONEncode(datatosave)) -- trying to save data
		end)
		local mydata =  http:JSONDecode(gameData:GetAsync(plrDataKey))
		table.foreach(mydata[12], print) -- printing out saved data

output:

  23:06:47.742  1 true  -  服务器 - Datas:110
  23:06:47.742  3 true  -  服务器 - Datas:110

  23:06:48.459  1 true  -  服务器 - Datas:128

its same as before

Hmm. I wonder if there isn’t a problem with the callback function setup you have going. Can you set and retrieve the data without using the callback methods you have, just to verify it.

I tried and its same as before

script:

local datatosave = {
			[1] = donateddata.Value;
			[2] = datafold.IsNew.Value;
			[3] = datafold.Weapon.Value;
			[4] = datafold.Equipped.Hair.Value;
			[5] = datafold.Equipped.Leg.Value;
			[6] = {datafold.Equipped.LegColor.Value.r, datafold.Equipped.LegColor.Value.g, datafold.Equipped.LegColor.Value.b};
			[7] = datafold.Equipped.Shirt.Value;
			[8] = {datafold.Equipped.ShirtColor.Value.r, datafold.Equipped.ShirtColor.Value.g, datafold.Equipped.ShirtColor.Value.b};
			[9] = {datafold.Equipped.SkinColor.Value.r, datafold.Equipped.SkinColor.Value.g, datafold.Equipped.SkinColor.Value.b};
			[10] = ownedskins;
			[11] = equippedskins;
			[12] = ownedweapons
		} --creating a table that includes player datas
		table.foreach(datatosave[12], print) --printing out ownedweapons
		gameData:SetAsync(plrDataKey, http:JSONEncode(datatosave)) -- trying to save data
		local mydata =  http:JSONDecode(gameData:GetAsync(plrDataKey))
		table.foreach(mydata[12], print) -- printing out saved data

output:

  23:19:44.912  1 true  -  Server - Datas:110
  23:19:44.912  3 true  -  Server - Datas:110
 --after GetAsnyc
  23:19:45.629  1 true  -  Server - Datas:113

i have no idea about why is this not working

whats the structure of the “ownedweapons” table?

Its like this : {[1]=true,[3]=true}

So this may be an issue with the missing index. Change the 3 to a 2 or just add in a 2 index with a blank value.

1 Like

alright, i will try it. but if player gets more “weapons” then the “owned weapons” table will be modified (like this : {[1]=true, [3]=true, [6]=true, [7]=true}) then how to fill the gaps?

Its never a good idea to skip indices. So you can instead do:

weapons = {
	[1] = {Id = 1, Owned = true},
	[2] = {Id = 3, Owned = true},
	[3] = {Id = 7, Owned = true},
}

Or Even:

weapons = {
	[1] = true,
	[2] = false,
	[3] = true,
}

It worked! thank you very much

1 Like