Roblox data store broken. Random players losing their stats every server shutdown

Title explains it. I have 6 leaderboards and 1 data store script

Here is the data store script:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore5")
local RunService = game:GetService("RunService")
serverStorage = game.ServerStorage
game.Players.PlayerAdded:Connect(function(plr)

	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "leaderstats"

	local Strength = Instance.new("NumberValue")
	Strength.Name = "Strength"
	Strength.Parent = leaderstats	
	if success then
		Strength.Value = data.Strength
	end
        local success, errormessage = pcall(function()
		DataStore:UpdateAsync(PlayerUserId, function(OldData)
			return data
		end)
	end)

	if success then
		print('Data Successfully Saved!')
	end
	if errormessage then
		print('Data UnSuccessfully Saved!')
		warn(errormessage)
	end
end)
game.Players.PlayerRemoving:Connect(function(plr)
	local PlayerUserId = "Player_"..plr.UserId

	local data = {
		Strength = plr.leaderstats.Strength.Value;
	}

	local success, errormessage = pcall(function()
		DataStore:UpdateAsync(PlayerUserId, function(OldData)
			return data
		end)
	end)

	if success then
		print('Data Successfully Saved!')
	end
	if errormessage then
		print('Data UnSuccessfully Saved!')
		warn(errormessage)
	end
end)
game:BindToClose(function()
	if RunService:IsStudio() then
		return
	end
	for _,plr in pairs(game.Players:GetPlayers()) do
		local PlayerUserId = "Player_"..plr.UserId

		local data = {
			Strength = plr.leaderstats.Strength.Value;
		}

		local success, errormessage = pcall(function()
			DataStore:UpdateAsync(PlayerUserId, function(OldData)
				return data
			end)
		end)

		if success then
			print('Data Successfully Saved!')
		end
		if errormessage then
			print('Data UnSuccessfully Saved!')
			warn(errormessage)
		end
	end
end)

Auto save local script:

while wait(300) do
	game.ReplicatedStorage.AutoSave:FireServer(game.Players.LocalPlayer)
	script.Parent.Parent.PlayerGui.AutoSave.Enabled = true
	wait(5)
	script.Parent.Parent.PlayerGui.AutoSave.Enabled = false
end
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore3")
game.ReplicatedStorage.AutoSave.OnServerEvent:Connect(function(plr)
	local PlayerUserId = "Player_"..plr.UserId

	local data = {
		Strength = plr.leaderstats.Strength.Value;
	}

	DataStore:UpdateAsync(PlayerUserId, function(OldData)
			return data
	end)
end)

Please help me fix this problem since my game had 10 playings and i had to private it just for those reasons

3 Likes

You can use DataModel:BindToClose to save the data before the game shutdown. Also you should use GlobalDataStore:UpdateAsync instead of GlobalDataStore:SetAsync

picture

1 Like

I’d recommend looking at this tutorial:

It’s a very in depth tutorial but you don’t have to do everything with it. It shows you how to make a datastore more secure as well as show you how to use basically everything about datastores. I’d highly recommend reading it as at least for me, it helped me a lot.

You can also look at these as well:

Hope this helps!

2 Likes

This really helped me with same issue as you have, consider looking into it:

1 Like

Uh, so, something that I notice is that if the data doesn’t successfully gather the data, it will not write any data, and when the player leaves, there is a chance that it will overwrite the old data with the blank new one. My recommendation is that you do not save the data if they player is leaving the game and the data wasn’t loaded successfully in the first place. Also, I recommend you “retry” to get the data a couple of times. If that doesn’t work, you can kick the player and prompt them to rejoin. I’ve seen a couple of games follow this approach. Additionally, you could look into DataStore2 since it has retry features and backup features if I am correct. I apologize for the short and rushed reply, as well as any grammatical mistakes, I am from my phone, so apologies. Hope I helped.

1 Like

I used game:BindToClose() but now it saves everyone’s data when someone leaves. And some players are losing their data from it

1 Like

My problem is still not fixed. I have tried game:BindToClose() and a random player lost their stats because of the amount of data store requests (since it fires everytime a player leaves) and i have tried auto data saving but it still didn’t work

And i cannot use DataStore2 since its going to reset my stats.

My problem still isn’t fixed yet. I have been waiting for 6 hours and yet no responses

Make sure you wrap your save functions in the bindToClose function in coroutines. Also, autosaving would greatly reduce your issues.

I tried autosaving then kicking everyone out of the server but it still didn’t work

I have been waiting for 4 days and no one gave me response to fix this bug

Can you show your new saving script with autosaving and bindToClose?

My game had a random bug like this today.
I had to manually replace data.
I would highly suggest saving data every 5 mins so that data loss is less

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore5")
local RunService = game:GetService("RunService")
serverStorage = game.ServerStorage
game.Players.PlayerAdded:Connect(function(plr)

	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "leaderstats"

	local Strength = Instance.new("NumberValue")
	Strength.Name = "Strength"
	Strength.Parent = leaderstats	
	if success then
		Strength.Value = data.Strength
	end
        local success, errormessage = pcall(function()
		DataStore:UpdateAsync(PlayerUserId, function(OldData)
			return data
		end)
	end)

	if success then
		print('Data Successfully Saved!')
	end
	if errormessage then
		print('Data UnSuccessfully Saved!')
		warn(errormessage)
	end
end)
game.Players.PlayerRemoving:Connect(function(plr)
	local PlayerUserId = "Player_"..plr.UserId

	local data = {
		Strength = plr.leaderstats.Strength.Value;
	}

	local success, errormessage = pcall(function()
		DataStore:UpdateAsync(PlayerUserId, function(OldData)
			return data
		end)
	end)

	if success then
		print('Data Successfully Saved!')
	end
	if errormessage then
		print('Data UnSuccessfully Saved!')
		warn(errormessage)
	end
end)
game:BindToClose(function()
	if RunService:IsStudio() then
		return
	end
	for _,plr in pairs(game.Players:GetPlayers()) do
		local PlayerUserId = "Player_"..plr.UserId

		local data = {
			Strength = plr.leaderstats.Strength.Value;
		}

		local success, errormessage = pcall(function()
			DataStore:UpdateAsync(PlayerUserId, function(OldData)
				return data
			end)
		end)

		if success then
			print('Data Successfully Saved!')
		end
		if errormessage then
			print('Data UnSuccessfully Saved!')
			warn(errormessage)
		end
	end
end)```

Wrap this in a coroutine so it saves all the players’ data at once.

Also, I don’t see any autosaving?

Edit: How to use corountines:

if RunService:IsStudio() then
		return
	end
	for _,plr in pairs(game.Players:GetPlayers()) do
        corountine.wrap(function()
		local PlayerUserId = "Player_"..plr.UserId

		local data = {
			Strength = plr.leaderstats.Strength.Value;
		}

		local success, errormessage = pcall(function()
			DataStore:UpdateAsync(PlayerUserId, function(OldData)
				return data
			end)
		end)

		if success then
			print('Data Successfully Saved!')
		end
		if errormessage then
			print('Data UnSuccessfully Saved!')
			warn(errormessage)
		end
        end)()
	end

The auto data save script is in a different script.

I have an auto save script and im using game:closetobind() and players are still losing their stats! I don’t get why its still happening

Sorry for bumping this topic a lot of times but i REALLY need help right now.

1 Like

It has been 18 hours and still no answer.