Datastore wiping if I leave and rejoin too quickly

  1. What do you want to achieve?
    I want to fix my datastore so a save doesn’t wipe if I rejoin too quickly. I want to keep it simple without getting into profileservice or datastore2.

  2. What is the issue?
    The title says it all. If any player leaves and rejoins too quickly, their data gets wiped.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("TEST0.20")

Players.PlayerAdded:Connect(function(player)
	local Data = nil
	local success, errormessage = pcall(function()
		Data = Saver:GetAsync(tostring(player.UserId))
	end)

	if success then
		if Data then
			for i, v in pairs(Data) do
				if not player:FindFirstChild(i) then
					player:WaitForChild("leaderstats"):WaitForChild(i).Value = v
				else
					player:FindFirstChild(i).Value = v
				end
			end
		end
	else
		player:Kick("Your data didn't save, rejoin after a few moments.")
		error(errormessage)
	end
end)

local function Save(player)
	local SavedData = {}
	for _, v in pairs(player.leaderstats:GetChildren()) do
		SavedData[v.Name] = v.Value
	end

	for _, v in pairs(player:GetChildren()) do
		if v:IsA("NumberValue") or v:IsA("StringValue") then
			SavedData[v.Name] = v.Value
		end
	end

	local success, errormessage = pcall(function()
		Saver:SetAsync(tostring(player.UserId), SavedData)
	end)
	if not success then
		error(errormessage)
	end
end

Players.PlayerRemoving:Connect(Save)

game:BindToClose(function()
	
	for _, v in pairs(Players:GetPlayers()) do
		Save(v)
	end
end)
1 Like

I really recommend using custom datastore modules like Datastore 2 or Profile Service

Using these modules are not very complicated and it prevents data loss and being ratelimited. They give out very good tutorials on what to do.

1 Like

At the top of the loading script, you need a BoolValue (parented to the player) to signal to the saving function that the data has loaded. Set the value to true at the very end of your loading function.

Then, in the saving function, right at the top, check the value of the BoolValue. If it is true (to signal data loading failed or didn’t finish), don’t continue with the save.

1 Like

the kick part/error part of the loading script never seems to trigger, so setting the value to true doesn’t work.

I believe the issue lies in the actual saving time or process

1 Like

I’m trying to switch to it, but it’s pretty difficult

1 Like

Not really. All you have to do is update the save data and get data.

If you want an easy transition use datastore 2.

1 Like

I’ve done a bit of research on both and a lot of people say that profileservice is better

1 Like

Well in some cases it might be but using datastore 2 is alot easier to set up.

1 Like

I’m hesitant. Is there really no way to fix the issue using the script I already have?

1 Like

This is all you have to do:

local Players = game:GetService("Players")
local DataStore2 = require('path to datastore 2')

Players.PlayerAdded:Connect(function(Player)
	local success, Data = pcall(function()
		local DataStore = DataStore2('Data', Player)
		local ResultData = DataStore:Get()

		if ResultData ~= nil then
			for i, v in pairs(ResultData) do
				if not Player:FindFirstChild(i) then
					Player:WaitForChild("leaderstats"):WaitForChild(i).Value = v
				else
					Player:FindFirstChild(i).Value = v
				end
			end	
		end
	end)
	
	if success then
		print('Successfully Got Data For ' ..Player.Name.."!")
	else
		Player:Kick("Your data didn't save, rejoin after a few moments.")
		error(Player)
	end
end)

local function Save(player)
	local SavedData = {}
	for _, v in pairs(player.leaderstats:GetChildren()) do
		SavedData[v.Name] = v.Value
	end

	for _, v in pairs(player:GetChildren()) do
		if v:IsA("NumberValue") or v:IsA("StringValue") then
			SavedData[v.Name] = v.Value
		end
	end

	local success, errormessage = pcall(function()
		local DataStore = DataStore2('Data', player)
		DataStore:Set(SavedData)
	end)
	if not success then
		error(errormessage)
	end
end

Players.PlayerRemoving:Connect(Save)

game:BindToClose(function()

	for _, v in pairs(Players:GetPlayers()) do
		Save(v)
	end
end)
1 Like

It kicks when a player joins for the first time. How do I remedy that?

Also how would I change the datastore (so like for example, changing it from published datastore to testing datastore)

1 Like

do this. Its because the player has no data inside that datastore.

What do you mean by this?

for the kicking out this should fix it:

Players.PlayerAdded:Connect(function(Player)
	local success, Data = pcall(function()
		local DataStore = DataStore2('Data', Player)
		local ResultData = DataStore:Get()

		if ResultData ~= nil then
			for i, v in pairs(ResultData) do
				if not Player:FindFirstChild(i) then
					Player:WaitForChild("leaderstats"):WaitForChild(i).Value = v
				else
					Player:FindFirstChild(i).Value = v
				end
			end
		end
	end)
	
	if success then
		print(Data)
	else
		Player:Kick("Your data didn't save, rejoin after a few moments.")
		error(Player)
	end
end)
1 Like

Like, say for example, something terrible happens in one datastore, so I want to change it to another version, resetting data or changing it to the data that was saved in that version, how would I do that?

1 Like

I dont think you can revert the version BUT you can create a backup function incase the data breaks somehow.

1 Like

This tends to happen when you’re in the studio.

1 Like

Well, it happened in game too, but vo3pal’s datastore seemed to do the trick

1 Like

Alright.

Also one last question if you don’t mind:
I’m having this issue (not sure if it’s related to datastore)

RunService:UnbindFromRenderStep removed different functions with same reference name utility-focus-state-inspect-Ajitto 2 times.

I don’t know why this issue is happening or where it’s coming from, but it happens when I reset

1 Like

I’ve learned to not call up the data store the moment they start a game. Give that a few moments and make sure you have a drop out of the game way to handle a data save …

game:BindToClose() function

1 Like

Not sure, create a new forum about this. If you have your solution mark it!

1 Like

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