Data saving does not work

  1. What do you want to achieve? Data saving

  2. What is the issue? Data does not save

  3. What solutions have you tried so far? Tried doing “game:GetService(“Players”).PlayerAdded”, doing a BindToClose that kicks everyone after 3 seconds, using code people recommend on the DevForum, and a lot of my own debugging

It works sometimes while making a local test server in Studio, but it does not work while using my main account (even while in a published game)

Code:

local hdMain = require(game:GetService("ReplicatedStorage"):WaitForChild("HDAdminSetup")):GetMain()
local hd = hdMain:GetModule("API")

local DataStore = game:GetService("DataStoreService"):GetDataStore("DStore")
local players = game:GetService("Players")
local r = game:GetService("ReplicatedStorage")
local s = game:GetService("ServerStorage")
local t = game:GetService("Teams")
local m = game:GetService("MarketplaceService")

local function playerAdded(p)

	game:GetService("ServerStorage").Tools.Sword:Clone().Parent
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = p
	
	local MakingDough = Instance.new("BoolValue")
	MakingDough.Name = "MakingDough"	
	MakingDough.Value = false
	MakingDough.Parent = p

	local MakingCake = Instance.new("BoolValue")
	MakingCake.Name = "MakingCake"	
	MakingCake.Value = false
	MakingCake.Parent = p

	local MakingBread = Instance.new("BoolValue")
	MakingBread.Name = "MakingBread"
	MakingBread.Value = false
	MakingBread.Parent = p
	
	local currency = Instance.new("NumberValue")
	currency.Name = "Coins"
	currency.Parent = folder

	local ID = "Coins".."-"..p.UserId
	local savedData = nil	

	pcall(function()

		savedData = DataStore:GetAsync(ID)

	end)

	if savedData ~= nil then

		currency.Value = savedData
		print("Data loaded")

	else
		-- New player
		currency.Value = 0
		print("New player to the game")

	end	

	local level = Instance.new("NumberValue")
	level.Name = "Level"
	level.Parent = folder

	local ID = "Level".."-"..p.UserId
	local savedData = nil	

	pcall(function()

		savedData = DataStore:GetAsync(ID)

	end)

	if savedData ~= nil then

		level.Value = savedData
		print("Data loaded")

	else
		-- New player
		level.Value = 1
		print("New player to the game")

	end

	local EXP = Instance.new("NumberValue")
	EXP.Name = "EXP"
	EXP.Parent = p

	local ID = "EXP".."-"..p.UserId
	local savedData = nil	

	pcall(function()

		savedData = DataStore:GetAsync(ID)

	end)

	if savedData ~= nil then

		EXP.Value = savedData
		print("Data loaded")

	else
		-- New player
		EXP.Value = 0
		print("New player to the game")

	end

	local EXPMax = Instance.new("NumberValue")
	EXPMax.Name = "EXPMax"
	EXPMax.Parent = p

	local ID = "EXPMax".."-"..p.UserId
	local savedData = nil	

	pcall(function()

		savedData = DataStore:GetAsync(ID)

	end)

	if savedData ~= nil then

		EXPMax.Value = savedData
		print("Data loaded")

	else
		-- New player
		EXPMax.Value = 30
		print("New player to the game")

	end

	local BreadAndCakeTime = Instance.new("NumberValue")
	BreadAndCakeTime.Name = "BreadAndCakeTime"
	BreadAndCakeTime.Parent = p

	local ID = "BreadAndCakeTime".."-"..p.UserId
	local savedData = nil	

	pcall(function()

		savedData = DataStore:GetAsync(ID)

	end)

	if savedData ~= nil then

		BreadAndCakeTime.Value = savedData
		print("Data loaded")

	else
		-- New player
		BreadAndCakeTime.Value = 10
		print("New player to the game")

	end

	local OvenTime = Instance.new("NumberValue")
	OvenTime.Name = "OvenTime"
	OvenTime.Parent = p

	local ID = "OvenTime".."-"..p.UserId
	local savedData = nil	

	pcall(function()

		savedData = DataStore:GetAsync(ID)

	end)

	if savedData ~= nil then

		OvenTime.Value = savedData
		print("Data loaded")

	else
		-- New player
		OvenTime.Value = 7
		print("New player to the game")

	end

	local BigOvenTime = Instance.new("NumberValue")
	BigOvenTime.Name = "BigOvenTime"
	BigOvenTime.Parent = p

	local ID = "BigOvenTime".."-"..p.UserId
	local savedData = nil	

	pcall(function()

		savedData = DataStore:GetAsync(ID)

	end)

	if savedData ~= nil then

		BigOvenTime.Value = savedData
		print("Data loaded")

	else
		-- New player
		BigOvenTime.Value = 12
		print("New player to the game")

	end
	
	local tools = Instance.new("Folder")
	tools.Name = "Inventory"
	tools.Parent = p

	local savedData = nil	

	pcall(function()

		savedData = DataStore:GetAsync(p.UserId)

	end)

	if savedData ~= nil then
		
		for i,v in pairs(savedData) do
							
			s.Tools:FindFirstChild(v):Clone().Parent = tools
			print(v.." stored")
			
		end
		
		print("Data loaded")

	else
		-- New player
		print("No data")

	end

game:GetService("Players").PlayerAdded:Connect(playerAdded)

for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
	
	playerAdded(player)
	
end

local function save(p)
	
	r.PlayerLeft:FireAllClients(p)
	
	for i,v in pairs(p.Backpack:GetChildren()) do

		if v.Name ~= "BunchOfBalloons" 
			and v.Name ~= "FluffyUnicorn" 
			and v.Name ~= "Sword" 
			and v.Name ~= "RainbowSword" 
			
		then

			v.Parent = p:FindFirstChild("Inventory")
			print(v.Name.." stored")

		end				
	end

	if p.Character:FindFirstChildOfClass("Tool") 
		and p.Character:FindFirstChildOfClass("Tool") ~= "BunchOfBalloons" 
		and p.Character:FindFirstChildOfClass("Tool") ~= "FluffyUnicorn"
		and p.Character:FindFirstChildOfClass("Tool") ~= "Sword"
		and p.Character:FindFirstChildOfClass("Tool") ~= "RainbowSword"
		
	then
		
		local the = script.Parent:FindFirstChildOfClass("Tool")
		the.Parent = p:FindFirstChild("Inventory")
		print(the.Name.." stored")

	end
	
	local ToolTable = {}

	for i,v in pairs(p:FindFirstChild("Inventory"):GetChildren()) do
		
		if v.Name ~= "GravityCoil" and v.Name ~= "SpeedCoil" then
			
			table.insert(ToolTable, v.Name)

		end	
	end

	if ToolTable ~= nil then

		print(ToolTable)
		DataStore:SetAsync(p.UserId, ToolTable)
		print("Tool storage saved successfully")

	else

		print("Error storing tools")

	end	

	local ID = "Coins".."-"..p.UserId

	DataStore:SetAsync(ID,p:FindFirstChild("leaderstats"):FindFirstChild("Coins").Value)

	local ID = "Level".."-"..p.UserId

	DataStore:SetAsync(ID,p:FindFirstChild("leaderstats"):FindFirstChild("Level").Value)

	local ID = "EXP".."-"..p.UserId

	DataStore:SetAsync(ID,p:FindFirstChild("EXP").Value)

	local ID = "EXPMax".."-"..p.UserId

	DataStore:SetAsync(ID,p:FindFirstChild("EXPMax").Value)

	local ID = "BreadAndCakeTime".."-"..p.UserId

	DataStore:SetAsync(ID,p:FindFirstChild("BreadAndCakeTime").Value)

	local ID = "OvenTime".."-"..p.UserId

	DataStore:SetAsync(ID,p:FindFirstChild("OvenTime").Value)

	local ID = "BigOvenTime".."-"..p.UserId

	DataStore:SetAsync(ID,p:FindFirstChild("BigOvenTime").Value)
	
end

game:GetService("Players").PlayerRemoving:Connect(save)
	
game:BindToClose(function()
	
	if game:GetService("RunService"):IsStudio() then
		
		wait(3)
		
	else
		
		for i,v in ipairs(game:GetService("Players"):GetPlayers()) do
			
			coroutine.wrap(save)(v)
			
		end
	end
end)

What happens when saving without waiting for PlayerRemoving?

here is a video that might help teach you how to save and load player data

Sorry for replying late, but my solution is to just use DataStore2

May I ask, what is DataStore2? I haven’t heard about that.

You can read about it here: