Data Store SetAsync not working with HttpService

Alright, so I’m having some problems when it comes to the HttpService:JSONEncode() line under the saveData function. For some reason it stops the script after the playerInfo:SetAsync() and it does not yield any errors or nothing. The 1 is printed but the 2 is not. I genuinely have no idea what the issue is except for the JSONEncode() function.

If something is unclear please say so and I’ll try my best to make this process easier for all parties involved.

local Players = game:GetService("Players")

local DataStoreService = game:GetService("DataStoreService")
local playerInfo = DataStoreService:GetDataStore("playerInfo")
local HttpService = game:GetService("HttpService")

local System = require(game:GetService("ServerScriptService")["General System"].System)

local DataTable = require(script.dataTable)

function loadProfile(plr)
	playerInfo:SetAsync(plr.userId.."-1", HttpService:JSONEncode(DataTable))
end


function saveData(plr)
	print(1)
	playerInfo:SetAsync(plr.userId.."-1", HttpService:JSONEncode({
		["Stats"] = {

			["Dollars"] = {
				["Value"] =  System.shrinkValue(plr.leaderstats.Dollars.Value);
				["Multiplier"] = 0;
				["Boost"] = 0;
			};

			["Scraps"] = {
				["Value"] = System.shrinkValue(plr.leaderstats.Scraps.Value);
				["Multiplier"] = 0;	
				["Boost"] = 0;
			};

			["Rebirths"] = {
				["Value"] = System.shrinkValue(plr.leaderstats.Rebirths.Value);
				["Multiplier"] = 0;
				["Boost"] = 0;
			};
		};
		["Items"] = {

		};
		["Backpacks"] = {

		};
		["Finders"] = {

		};
		["Pets"] = {

		};
	}))
	print(2)
end

Players.PlayerAdded:Connect(function(plr)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	if playerInfo:GetAsync(plr.userId) == nil then
		loadProfile(plr)
	end
	
	local dollars = Instance.new("IntValue")
	dollars.Name = "Dollars"
	dollars.Parent = leaderstats
	dollars.Value = System.expandValue(HttpService:JSONDecode(playerInfo:GetAsync(plr.userId.."-1")).Stats.Dollars.Value)
	
	local rebirths = Instance.new("IntValue")
	rebirths.Name = "Rebirths"
	rebirths.Parent = leaderstats
	rebirths.Value = System.expandValue(HttpService:JSONDecode(playerInfo:GetAsync(plr.userId.."-1")).Stats.Rebirths.Value)
	
	local scraps = Instance.new("IntValue")
	scraps.Name = "Scraps"
	scraps.Parent = leaderstats
	scraps.Value = System.expandValue(HttpService:JSONDecode(playerInfo:GetAsync(plr.userId.."-1")).Stats.Scraps.Value)
	
end)

Players.PlayerRemoving:Connect(function(plr)
	local s, e = pcall(function()
		saveData(plr)
		print(2)
	end)
	if not s then
		warn(plr.Name.."'s data was not saved correctly on leaving! Error: "..e)
	end
end)

while true do
	wait(120)
	for i,v in pairs(Players:GetChildren()) do
		local s, e = pcall(function()
			saveData(v)
		end)
		if not s then
			warn(v.Name.."'s data was not saved correctly! Error: "..e)
		end
	end
end
1 Like

Is System.shrinkValue a yielding function?

You could try defining the dictionary first and then JSONEncoding to see exactly at what point it stops, too.

2 Likes

There is no need to JSONEncode/JSONDecode tables as Roblox already does it.

2 Likes

Try using a BindToClose function, as the extra call to JSONEncode might be giving the server the tiny bit of extra time it needs to shutdown after the last person leaves.

3 Likes

I’m sorry but I don’t totally understand where I’d implement the BindToClose function. To you mean for it to be separate from everything else and then call the saveData function or be implemented inside of another function?

Yeah. Loop through each player and call saveData, or set up some system that waits for the PlayerRemoving function to save (via Bindables?)

2 Likes

Hmm well at least now it goes through the whole thing but it does not load the saved data. Is it supposed to fire the print(2) and datastore:set() after the disconnection?

The output is like this

Disconnect from ::ffff:127.0.0.1|54618
DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = datastore key :)
  2

Never mind I fixed the issue, thank you so much!

1 Like