DataStore Problem (ToolSaver)

I have no idea of what happned but my all datastore just disappeared anyone know maybe what the problem?

local ToolFolder = game:GetService("ServerStorage"):FindFirstChild("SavedTools")
local DataStoreService = game:GetService("DataStoreService")
local SaveData = DataStoreService:GetDataStore("SieDffsagtfa32")

game.Players.PlayerAdded:Connect(function(Player)
	local ToolData = SaveData:GetAsync(Player.UserId)
	
	local BackPack = Player:WaitForChild("Backpack")
	local StarterGear = Player:WaitForChild("StarterGear")
	
	if ToolData ~= nil then
		for i, v in pairs(ToolData) do
			if ToolFolder:FindFirstChild(v) and BackPack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
				ToolFolder[v]:Clone().Parent = BackPack
				ToolFolder[v]:Clone().Parent = StarterGear
			end
		end
	end
	
	Player.CharacterRemoving:Connect(function(Character)
		Character:WaitForChild("Humanoid"):UnequipTools()
	end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local ToolTable = {}
	
	for i, v in pairs(Player.Backpack:GetChildren()) do
		table.insert(ToolTable, v.Name)
	end
	if ToolTable ~= nil then
		SaveData:SetAsync(Player.UserId, ToolTable)
	end
end)
1 Like

I think you might have forgotten to load the tools. You’re not loading the tools unless it’s inside another script. Therefore, the backpack is most likely empty and saves an empty table.

image
all there
image
second script

1 Like

I didn’t see it sorry, I’ll quickly check it out.

hm fine its the second time its happening to me its soo anoyying and i have problem with rank of search of my game why all the problems come to me

I’ve tested it and also checked the datastore via a plugin and it seems to work perfectly fine. The only reason I could think the data disappearing is the fact that the player might leave faster than what the server can save. Try adding a quick script at the end.

game:BindToClose(function()
    wait(2)
end)

what is bindtoclose?
[max charrrrr]

it’s a function that will force the game to stay open a little bit longer. This is commonly used to help data save properly.

but at all i cant get back the data? cuz its reset to all players

Unfortunately no, unless you have a backup then you can revert everyone to that datastore.

backup ? how its work?
[max charrr[

Basically, there are a few ways to save backups. Some in-studio command bar. The other will run automatically.

For the automatic one, Save the data every few minutes on another datastore. If the value is 0 or nil then it won’t save. This is a good way to get data back from disappearing data sets.

You can show example if you can?

lua````
local ToolFolder = game:GetService(“ServerStorage”):FindFirstChild(“SavedTools”)
local DataStoreService = game:GetService(“DataStoreService”)
local SaveData = DataStoreService:GetDataStore(“SieDffsagtfa32”)

game.Players.PlayerAdded:Connect(function(Player)
local ToolData = SaveData:GetAsync(Player.UserId)

local BackPack = Player:WaitForChild("Backpack")
local StarterGear = Player:WaitForChild("StarterGear")

if ToolData ~= nil then
	for i, v in pairs(ToolData) do
		if ToolFolder:FindFirstChild(v) and BackPack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
			ToolFolder[v]:Clone().Parent = BackPack
			ToolFolder[v]:Clone().Parent = StarterGear
		end
	end
end

Player.CharacterRemoving:Connect(function(Character)
	Character:WaitForChild("Humanoid"):UnequipTools()
end)

end)

game.Players.PlayerRemoving:Connect(function(Player)
local ToolTable = {}

for i, v in pairs(Player.Backpack:GetChildren()) do
	table.insert(ToolTable, v.Name)
end
if ToolTable ~= nil then
	SaveData:SetAsync(Player.UserId, ToolTable)
	game:BindToClose(function()
		wait(2)
	end)
end

end)````
that’s ok btw?

I’m not sure if that BindToClose will work, you can try but it’s best to keep it outside.

Main - Script


local BackupHandler = task.spawn(function()
	local Backup = game:GetService("DataStoreService"):GetDataStore("SieDffsagtfa32_Backup")

	local SaveBackups = game.Players.PlayerAdded:Connect(function(Player)
		local cooldown = 300 -- 5 minutes
		while task.wait(cooldown) do if Player == nil then return end
			local ToolTable = {}

			for i, v in pairs(Player.Backpack:GetChildren()) do
				table.insert(ToolTable, v.Name)
			end
			if ToolTable ~= nil or #ToolTable ~= 0 then
				Backup:SetAsync(Player.UserId, ToolTable)
			end
		end
	end)
end)

Full - Script


local BackupHandler = task.spawn(function()
	local Backup = game:GetService("DataStoreService"):GetDataStore("SieDffsagtfa32_Backup")

	local SaveBackups = game.Players.PlayerAdded:Connect(function(Player)
		local cooldown = 5 -- 5 minutes
		while task.wait(cooldown) do if Player == nil then return end
			local ToolTable = {}

			for i, v in pairs(Player.Backpack:GetChildren()) do
				table.insert(ToolTable, v.Name)
			end
			if ToolTable ~= nil or #ToolTable ~= 0 then
				Backup:SetAsync(Player.UserId, ToolTable)
			end
		end
	end)
end)

local SaveDataHandler = task.spawn(function()
	local ToolFolder = game:GetService("ServerStorage"):FindFirstChild("SavedTools")
	local DataStoreService = game:GetService("DataStoreService")
	local SaveData = DataStoreService:GetDataStore("SieDffsagtfa32")

	game.Players.PlayerAdded:Connect(function(Player)
		local ToolData = SaveData:GetAsync(Player.UserId)

		local BackPack = Player:WaitForChild("Backpack")
		local StarterGear = Player:WaitForChild("StarterGear")

		if ToolData ~= nil then
			for i, v in pairs(ToolData) do
				if ToolFolder:FindFirstChild(v) and BackPack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
					ToolFolder[v]:Clone().Parent = BackPack
					ToolFolder[v]:Clone().Parent = StarterGear
				end
			end
		end

		Player.CharacterRemoving:Connect(function(Character)
			Character:WaitForChild("Humanoid"):UnequipTools()
		end)
	end)

	game.Players.PlayerRemoving:Connect(function(Player)
		local ToolTable = {}

		for i, v in pairs(Player.Backpack:GetChildren()) do
			table.insert(ToolTable, v.Name)
		end
		if ToolTable ~= nil then
			SaveData:SetAsync(Player.UserId, ToolTable)
		end
	end)
	
	game:BindToClose(function()
		task.wait(2)
	end)
end)

1 Like

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