Datastore sometimes work, and sometimes doesn't

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?

I am making a save system with datastore. It should save a leaderstat value.

  1. What is the issue?
    The issue is that that it doesn’t always save. There’s around a 70% success rate. I am not sure how to send an image or video describing the situation, but when leaving and rejoining on Studio, there’s a chance it won’t save. The error is on Player too.

  2. What solutions have you tried so far?

I have tried looking into a couple devforum posts, however they had different issues from me. They had a problem of saving too many different objects and variables at the same time, whereas I only have one value in my code that is breaking.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

Players.PlayerAdded:Connect(function(Player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player

	local Points = Instance.new("IntValue")
	Points.Name = "Points"
	Points.Parent = leaderstats

	local Data

	local success, errormessage = pcall(function()

		Data = myDataStore:GetAsync(Player.UserId, Player.leaderstats.Points)

	end)


	if success then

		Points.Value = Data

	elseif errormessage then

		print("failed to find data")
		warn(errormessage)

	end



end)

Players.PlayerRemoving:Connect(function(Player)

	local success, errormessage = pcall(function()

		myDataStore:SetAsync(Player.UserId,Player.leaderstats.Points.Value)

	end)

	if success then

		print("successfully saved!")

	elseif errormessage then

		print("not successful")
		warn(errormessage)

	end

end)

Thank you for your help!
(Note- when the value saves, it should print “successfully saved!”)

Use game:BindToClose. The server is likely shutting down before players.PlayerRemoving fires/has time to save.

1 Like

I suggest also adding in an autosave whenever you’re handling datastores. It’s very good practice and will prevent a decent amount of data loss

I see, I did a bit of research into the topic, and tried writing some code for it. However, it does not seem to give a successful save on its own. Can you please check what I am doing wrong?
Edit: I just realized another problem is that the value doesn’t load in, and the game saves it at 0 upon leaving, removing all the data. Do you know the potential fix to this?

game:BindToClose(function()
	
	local Player = game.Players:FindFirstChildOfClass("Player") --Assuming there is just one player left
	
	local success, errormessage = pcall(function()

		myDataStore:SetAsync(Player.UserId,Player.leaderstats.Points.Value)

	end)

	if success then

		print("successfully saved!")

	elseif errormessage then

		print("not successful")
		warn(errormessage)

	end
	
end)

You need to iterate over all of the players, then save their data in addition to players.PlayerRemoving.