DataStore is not saving sometime?

i’m new on Datastore but i was able to make script work its for inventory table:

local InvChecks = require(game:GetService("ReplicatedStorage").LootSys)
local RunService = game:GetService("RunService")
local ToolData = game:GetService("DataStoreService"):GetDataStore("ToolData")
local Repl = game:GetService("ReplicatedStorage")

local ToolLocation = game.ServerStorage:WaitForChild("Items") -- The location where all your tools are saved

function GetToolByName(toolName)
	for _, tool in pairs(ToolLocation:GetChildren()) do
		if tool.Name == toolName then return tool end
	end
end

function IsRegisteredTool(tool)
	for _, playerTool in pairs(ToolLocation:GetChildren()) do
		if playerTool.Name == tool.Name then return true end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	repeat wait() until player.Character
	loadData(player)
	print("Load")
end)

game.Players.PlayerRemoving:Connect(function(player)
--	saveData(player)
--	print("Saveing...")
end)


Repl:WaitForChild("Sav").OnServerEvent:Connect(function(player)
	print("Saveing...")
	saveData(player)
end)


function saveData(player)
	local plrdata = {}
	local Ctools = player:WaitForChild("PlayerGui").Inventory:GetDescendants()

	
	local playerinv = {}

	--	wait(1)
	--	InvChecks.checkInvStarter(player.Character) --//Start Rest The Save so it will not duble
	--	ToolData:SetAsync(player.userId, {}) --//End Rest

--	plrdata.Cash = player:WaitForChild("Cash").Value --//Cash Save Here

	for i,v in pairs(Ctools) do
		if v.ClassName == "Tool" then
			if v.Name ~= "" or v.Name ~= nil then 
			--		table.insert(playerinv,{v.Name,v.Value})
				if playerinv[v.Name] then
					print("There is Already item with name: ".. v.Name)
				local maths = i
				playerinv[v.Name..maths] = {}
				local lb = playerinv[v.Name..maths]
				lb.Na = v.Name
				lb.Stat = v.Stat.Value
				lb.Qty = v.Qty.Value
				else
				playerinv[v.Name] = {}
				local lb = playerinv[v.Name]
				lb.Na = v.Name
				lb.Stat = v.Stat.Value
				lb.Qty = v.Qty.Value
				end
			end
		end
		
		if v.ClassName == "BoolValue" then
			if v.Parent.Name == "PrimWeapn" or v.Parent.Name == "SecWeapn" then
				if v.Name == "Act" and v.Value == true then
					if playerinv[v.Parent.WName.Value] then
					print("There is Already item with name: ".. v.Parent.WName.Value)
					local maths = i
					playerinv[v.Parent.WName.Value..maths] = {}
					local lb = playerinv[v.Parent.WName.Value..maths]
					lb.Na = v.Parent.WName.Value
					lb.Stat = "Equip"
					lb.Qty = 1
					else
					playerinv[v.Parent.WName.Value] = {}
					local lb = playerinv[v.Parent.WName.Value]
					lb.Na = v.Parent.WName.Value
					lb.Stat = "Equip"
					lb.Qty = 1
					end
				end
			end
		end


	end
	
--	plrdata = playerinv
		for i,v in pairs(playerinv) do
			print(v.Na.." - "..v.Stat)
		end

local Succeded, wrng = pcall(function()
		ToolData:SetAsync(player.userId, playerinv) --// sometime this part will not work and the game will not save???!
	end)
if Succeded then print("Saved") else print("Didn't Save") end

end

function loadData(player)
	repeat wait() until player.Character
	local Ctools = player.PlayerGui:WaitForChild("Inventory").BK.Backpack
	if ToolData:GetAsync(player.userId) then
local getAsy = ToolData:GetAsync(player.userId)
wait(getAsy)
		for i, toolName in pairs(getAsy) do
			print(toolName.Na)
if toolName.Na ~= "" or nil then
			local newTempTool = ToolLocation[toolName.Na]:Clone()
			newTempTool.Parent = Ctools
			newTempTool.Qty.Value = toolName.Qty
			newTempTool.Stat.Value = toolName.Stat
				if newTempTool.Stat.Value == "Inv" then
					Ctools.Parent.Parent.CurWgh.Value = Ctools.Parent.Parent.CurWgh.Value + newTempTool.wght.Value
				end
				if newTempTool.Stat.Value == "Equip" and newTempTool.Type.Value == "PrimW" then
					print(newTempTool.Name.." Equipped")
				local startT = ToolLocation[toolName.Na]:Clone()
					newTempTool.Parent = player.Backpack
					startT.Parent = game.StarterPack
				end
				if newTempTool.Stat.Value == "Equip" and newTempTool.Type.Value == "SecW" then
					print(newTempTool.Name.." Equipped")
				local startT = ToolLocation[toolName.Na]:Clone()
					newTempTool.Parent = player.Backpack
					startT.Parent = game.StarterPack
				end
				if newTempTool.Stat.Value == "Equip" and newTempTool.Type.Value == "Helm" then
					print(newTempTool.Name.." Equipped")
					newTempTool.Parent = Ctools.Parent.EqupFrame.Head.HBack
				end
				if newTempTool.Stat.Value == "Equip" and newTempTool.Type.Value == "Chest" then
					print(newTempTool.Name.." Equipped")
					newTempTool.Parent = Ctools.Parent.EqupFrame.Chest.CBack
				end
				if newTempTool.Stat.Value == "Equip" and newTempTool.Type.Value == "Back" then
					print(newTempTool.Name.." Equipped")
					Ctools.Parent.Parent.MaxWgh.Value = Ctools.Parent.Parent.MaxWgh.Value + newTempTool.Use.Value
					newTempTool.Parent = Ctools.Parent.EqupFrame.Back.BBack
				end
end
		end
		wait(1)
		InvChecks.checkInvStarter(player.Character)
	--	ToolData:SetAsync(player.userId, {})
	end
end

but sometime it save and sometime it don’t any idea
any advice will be great?

thank you

1 Like

Are you testing your code in TC?

Team create doesn’t support databases atm, so that may be the issue.

no i already published the game and test it but still have the same problem only save for 1:3 time ?

Hi.

Datastores typically should save, however on the off chance that they don’t, considered placing together one or two few backups.

Example: On error, backup to X, and if X fails, back up to Y.

That might fix it, but other than that, your script is good.

What you could do is along with saving when a player leaves, you can implement a BindToClose function to save the data before the server shuts down, because it will sometimes shut down before saving the data. You could try saving the data as they change, but you’d need to be aware of the limits datastores have, which is right here

These could help reduce data loss

2 Likes

Your best bet is to print to the output in each function to see where the error is occurring and why the save isn’t being initiated, continue to use the PlayerRemoving event as well as game:BindToClose as @Raretendoblox stated, BindToClose will initiate a save whenever the game server is shutdown, this would help with data loss and saving complications on your side.

Summing it up, just pinpoint the issue by printing to the output in each function to see where the save isn’t being initiated. If you still can’t figure out where the issue is occurring, please post some more in-depth information here and I’ll try my best to help.

i will try that thank you
i have the game link in the post if you want to check it

Don’t remember to Allow API Services.
After this, insert a script to workspace. Then write this:
local DSService = game:GetService(‘DataStoreService’):GetDataStore(‘noob123’) – Don’t change noob123
game.Players.PlayerAdded:connect(function(plr)
– Define variables
local uniquekey = ‘id-’…plr.userId
local leaderstats = Instance.new(‘IntValue’, plr)
local savevalue = Instance.new(‘IntValue’)
leaderstats.Name = ‘leaderstats’
savevalue.Parent = leaderstats
savevalue.Name = ‘Cash’ – You can change here

-- GetAsync
local GetSaved = DSService:GetAsync(uniquekey)
if GetSaved then
	savevalue.Value = GetSaved[1]
else
	local NumbersForSaving = {savevalue.Value}
	DSService:SetAsync(uniquekey, NumbersForSaving)
end

end)

game.Players.PlayerRemoving:connect(function(plr)
local uniquekey = ‘id-’…plr.userId
local Savetable = {plr.leaderstats.Cash.Value}
DSService:SetAsync(uniquekey, Savetable)

end)

i already have a script but there is problem sometime with saving
your script is not even what i need
and the part for the first line: GetDataStore(‘noob123’) – this name “noob123” is the name of your Data Store that can be changed to anything the developer want and for the money system i already have working script

Following your advice i was able to recreate the script using DataStore2
i made a new script so it will save when the player pick up the item or buy sell item
thank you for every thing :slight_smile:

1 Like

I thinks its from your internet.If problems occur on the internet, this increases the delay. the maximum delay should be 5 seconds. When you exit roblox, your connection will be disconnected from the game in 1-5 seconds. From the game. You know there are events like PlayerAdded in the code. For such events to take place, you need to have a good connection with Roblox servers. So in a nutshell, because the game is delaying the save process, it’s like a bug because you still don’t get disconnected while it’s called a glitch. And because of this error, the game is heavily loaded into the memory, so your game or your computer may crash. Let me show you something like this

You left the game --------- wait for delay time --------- Player has no connection

that is, the connection is lost because of the player’s internet and can not load data into the game’s memory.

1 Like