Datastores not saving on exit

I have a data saving system which is integral to my game as many items, and skills/ values must be saved to make the game worth playing at all. Many times players will also find themselves in a situation where leaving the game may be the best course of action. This being said, it’s very important that the data saves when the player leaves the game. What confuses me about this issue is that the saving works as I have periodic saves in case of a failure to save upon leaving the game HOWEVER despite using the same saving function just under different events it doesn’t accurately save the players info upon logging. (NOTE, this is not an issue with the server closing before data is able to be saved as I’ve run this with 2 player tests and in actual roblox servers with people left behind.)

local function save(player)
	local chr = player.Character or player.CharacterAdded:Wait()
	local items = {player.Values.Exp.FistExp.Value, player.Values.Exp.HeavyExp.Value, player.Values.Exp.SwordExp.Value, player.Values.Days.Value, player.Values.Race.Value, player.Values.Lives.Value, player.Values.Silver.Value, player.Values.Armor.Value, player.Values.Weapon.Value, player.Values.Location.Value, player.Values.Class.Value, player.Values.Class.Progression.Value}
	for i,v in pairs(player.Backpack:GetChildren()) do
		table.insert(items, v.Name)
	end
	local key = "User-"..player.userId

	datastore:SetAsync(key, items)
	--player.PlayerGui.Loading.Enabled = true
	--player.PlayerGui.Loading.Frame.ImageLabel.Visible = true
	--player.PlayerGui.Loading.Frame2.Check.Visible = false
	local Data 
	local Success = pcall(function()
		Data = datastore:GetAsync(key)
	end)
	--wait(math.random(2,4)) ik its corny

	if Success then
		if Data then
			print("Saved: " .. key, items)
			--player.PlayerGui.Loading.Frame.ImageLabel.Visible = false
			--player.PlayerGui.Loading.Frame2.Check.Visible = true
			--player.PlayerGui.Loading.Frame2.Check.Rotation = -45
			--wait(1)
			--player.PlayerGui.Loading.Enabled = false
		else
			warn("Failed to save.")
			--player.PlayerGui.Loading.Frame.ImageLabel.Visible = false
			--player.PlayerGui.Loading.Frame2.Failed.Visible = true
			--wait(1)
			--player.PlayerGui.Loading.Enabled = false
		end
	end
end
game.Players.PlayerRemoving:Connect(function(player)

	local vals = player.Values

	if vals.CombatTag.Value == true then
		vals.Silver.Value = 0
		vals.Weapon.Value = "Fist"
	end
	
	save(player)
	
end)

Would also appreciate if anybody could explain game:BindToClose() and how to use it in this scenario.

1 Like

Have you checked the pcall error?

local Success, err = pcall(function() 
	-- Pcalls return a second value this could be an error or anything you put to return in the function
	Data = datastore:GetAsync(key)
end)

print(err)

Send the error and we check what is going on :pray:

1 Like

Ah, I found the issue. I believe it was infinite yielding on the character as it should be gone before the player is. Going to make some adjustments just to confirm.

2 Likes

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