SetASync() not succeeding or failing?

So, I have a data store that saves a table. It mostly works except that the data it is supposed to save is somehow getting lost when it’s being retrieved. Here is an example: Saved data: [250,"Tiro",0,"Default","0, 0, 0"] | Retrieved Data: [250,"250",null,null]

Here is my code:

--OtherServices
local HttpService = game:GetService("HttpService")

--Data Folders
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SaveInfoFolder = ReplicatedStorage:WaitForChild("SaveInfo")

local CampaignFolder = SaveInfoFolder:WaitForChild("Campaign")
local PlayerDataFolder = CampaignFolder:WaitForChild("PlayerData")
local InventoryFolder = PlayerDataFolder:WaitForChild("Inventory")

local KeyBindingsFolder = SaveInfoFolder:WaitForChild("KeyBindings")

local SettingsFolder = SaveInfoFolder:WaitForChild("Settings")


--DataStore Variables
local DataStoreService = game:GetService("DataStoreService")
local CampaignDataStore = DataStoreService:GetDataStore("CampaignData")
local KeyBindingsDataStore = DataStoreService:GetDataStore("KeyBindingsData")
local SettingsDataStore = DataStoreService:GetDataStore("SettingsData")
local CampaignData = {
	Denarii = 0,
	XP = 0,
	Rank = "Tiro",
	Face = "Default",
	SkinTone = Color3.new(0, 0, 0)
}
local KeyBindingsData = {
	Attack = "LeftMouseButton",
	Block = "RightMouseButton",
	EquipPrimary = "One",
	EquipSecondary = "Two"
}
local SettingsData = {
	BodyDecayCooldown = 30,
	MaximumUnits = 200,
	MainVolume = 0.5,
	MusicVolume = 0.5
}



--Code
game.Players.PlayerAdded:Connect(function(Player)
	local success, data = pcall(function()
		local Data = CampaignDataStore:GetAsync(Player.UserId)
		print(Data) --Returns [250,"250",null,null]
		DeserializedData1 = HttpService:JSONDecode(Data)
		print(DeserializedData1)
	end)
	local success, data = pcall(function()
		local Data = KeyBindingsDataStore:GetAsync(Player.UserId)
		DeserializedData2 = HttpService:JSONDecode(Data)
	end)
	local success, data = pcall(function()
		local Data = SettingsDataStore:GetAsync(Player.UserId)
		DeserializedData3 = HttpService:JSONDecode(Data)
	end)
	
	DeserializedData1 = DeserializedData1 or CampaignData
	DeserializedData2 = DeserializedData2 or KeyBindingsData
	DeserializedData3 = DeserializedData3 or SettingsData
	
	CampaignData = DeserializedData1
	KeyBindingsData = DeserializedData2
	SettingsData = DeserializedData3
	
	print("Data Collected")
	print(CampaignData)
	
	for i, data in PlayerDataFolder:GetChildren() do
		if data.Name == "Denarii" then
			print(data)
			print(data.Value)
			data.Value = CampaignData[1]
		elseif data.Name == "XP" then
			print(data)
			print(data.Value)
			data.Value = CampaignData[2]
		elseif data.Name == "Rank" then
			print(data)
			print(data.Value)
			print(CampaignData[3])
			data.Value = CampaignData[3]
		elseif data.Name == "Face" then
			print(data)
			print(data.Value)
			data.Value = CampaignData[4]
		elseif data.Name == "SkinTone" then
			print(data)
			print(data.Value)
			data.Value = CampaignData[5]
		end
	end
	for i, data in KeyBindingsFolder:GetChildren() do
		if data.Name == "Attack" then
			data.Value = KeyBindingsData[1]
		elseif data.Name == "Black" then
			data.Value = KeyBindingsData[2]
		elseif data.Name == "EquipPrimary" then
			data.Value = KeyBindingsData[3]
		elseif data.Name == "EquipSecondary" then
			data.Value = KeyBindingsData[4]
		end
	end
	for i, data in SettingsFolder:GetChildren() do
		if data.Name == "BodyDecayCooldown" then
			data.Value = SettingsData[1]
		elseif data.Name == "MaximumUnits" then
			data.Value = SettingsData[2]
		elseif data.Name == "MainVolume" then
			data.Value = SettingsData[3]
		elseif data.Name == "MusicVolume" then
			data.Value = SettingsData[4]
		end
	end
	
	while wait(300) do
		CampaignData[1] = tonumber(PlayerDataFolder.Denarii.Value)
		CampaignData[2] = PlayerDataFolder.Rank.Value
		CampaignData[3] = tonumber(PlayerDataFolder.XP.Value)
		CampaignData[4] = tonumber(PlayerDataFolder.SkinTone.Value)

		KeyBindingsData[1] = KeyBindingsFolder.Attack.Value
		KeyBindingsData[2] = KeyBindingsFolder.Block.Value
		KeyBindingsData[3] = KeyBindingsFolder.EquipPrimary.Value
		KeyBindingsData[4] = KeyBindingsFolder.EquipSecondary.Value

		SettingsData[1] = tonumber(SettingsFolder.BodyDecayCooldown.Value)
		SettingsData[2] = tonumber(SettingsFolder.MaximumUnits.Value)
		SettingsData[3] = tonumber(SettingsFolder.MainVolume.Value)
		SettingsData[4] = tonumber(SettingsFolder.MusicVolume.Value)
		
		local SerializedCampaignData = HttpService:JSONEncode(CampaignData)
		local SerializedKeyBindingsData = HttpService:JSONEncode(KeyBindingsData)
		local SerializedSettingsData = HttpService:JSONEncode(SettingsData)

		local success, data = pcall(function()
			CampaignDataStore:SetAsync(Player.UserId, SerializedCampaignData)
		end)
		local success, data = pcall(function()
			KeyBindingsDataStore:SetAsync(Player.UserId, SerializedKeyBindingsData)
		end)
		local success, data = pcall(function()
			SettingsDataStore:SetAsync(Player.UserId, SerializedSettingsData)
		end)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	CampaignData[1] = tonumber(PlayerDataFolder.Denarii.Value)
	CampaignData[2] = PlayerDataFolder.Rank.Value
	CampaignData[3] = tonumber(PlayerDataFolder.XP.Value)
	CampaignData[4] = PlayerDataFolder.Face.Value
	CampaignData[5] = tostring(PlayerDataFolder.SkinTone.Value)
	
	print(PlayerDataFolder.SkinTone.Value)

	KeyBindingsData[1] = KeyBindingsFolder.Attack.Value
	KeyBindingsData[2] = KeyBindingsFolder.Block.Value
	KeyBindingsData[3] = KeyBindingsFolder.EquipPrimary.Value
	KeyBindingsData[4] = KeyBindingsFolder.EquipSecondary.Value

	SettingsData[1] = tonumber(SettingsFolder.BodyDecayCooldown.Value)
	SettingsData[2] = tonumber(SettingsFolder.MaximumUnits.Value)
	SettingsData[3] = tonumber(SettingsFolder.MainVolume.Value)
	SettingsData[4] = tonumber(SettingsFolder.MusicVolume.Value)

	local SerializedCampaignData = HttpService:JSONEncode(CampaignData)
	local SerializedKeyBindingsData = HttpService:JSONEncode(KeyBindingsData)
	local SerializedSettingsData = HttpService:JSONEncode(SettingsData)
	print(CampaignData)
	print(SerializedCampaignData) --Returns [250,"Tiro",0,"Default","0, 0, 0"]

	local success, data = pcall(function()
		CampaignDataStore:SetAsync(Player.UserId, SerializedCampaignData)
	end)
	local success, data = pcall(function()
		KeyBindingsDataStore:SetAsync(Player.UserId, SerializedKeyBindingsData)
	end)
	local success, data = pcall(function()
		SettingsDataStore:SetAsync(Player.UserId, SerializedSettingsData)
	end)
end)

Edit:
View my reply: SetASync() not succeeding or failing? - #2 by MRBOGO_YT

I figured out that the problem is that the data is having trouble saving. But Idk how that’s happening. I need help with that now I guess.

Doing this yields no prints as well:

	local success, errorMessage = pcall(function()
		CampaignDataStore:SetAsync(Player.UserId, SerializedCampaignData)
		print(":)")
	end)
	if not success then
		print(errorMessage)
	end
	local success, data = pcall(function()
		KeyBindingsDataStore:SetAsync(Player.UserId, SerializedKeyBindingsData)
	end)
	if not success then
		print(errorMessage)
	end
	local success, data = pcall(function()
		SettingsDataStore:SetAsync(Player.UserId, SerializedSettingsData)
	end)
	if not success then
		print(errorMessage)
	end

If you’re trying in Studio add a BindToClose (event of game) with the Data Saving things and a delay. Studio acts different than Client Servers.

I also tried in the actual game, and it didn’t change anything there. If I’m understanding you right that is what you were asking?

This just went in one ear and out the other…

BindToClose is an event that you can connect to detect when the server begins shutdown (e.g. all players have left or you pressed the Stop button in Studio).

Try adding the datasaving code in a fot loop (that loops through each player) and see if it saves there. Ofcourse you’d need to add a bit of task.waiting at the bottom so the data saving requests can properly send. It’s complicated put into words, think about the server not having enough time to save.

1 Like

So this could be the problem then because my game is a Singleplayer game (1 player per server) every time a player leaves it starts the server shutdown, Correct?

Wow, I didn’t think adding in such a small piece of code could fix such a (seemingly) huge problem! Thanks!

Saving code:

game:BindToClose(function()
	task.wait(5)
	print("Done")
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.