Data is not saving

Everything works fine except the data is not saving.

`

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("TimeStats")
local service = game:GetService("MarketplaceService")




game.Players.PlayerAdded:Connect(function(Player)


	

	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	
	
	local Expp = Instance.new("Folder")
	Expp.Name = "Expp"
	Expp.Parent = Player

	local Level = Instance.new("NumberValue")
	Level.Name="Level"
	Level.Value= 1
	Level.Parent = Leaderstats

	local Cash = Instance.new("IntValue")
	Cash.Name = "Leaf"
	Cash.Value = 0
	Cash.Parent = Leaderstats
	
	

	local exp = Instance.new("NumberValue",Expp)
	exp.Name="Exp"
	exp.Value= 0

	local requiredexp = Instance.new("NumberValue",Player)
	requiredexp.Name ="RequiredExp"
	requiredexp.Value=Level.Value*1000





	exp.Changed:Connect(function(Changed)
		if exp.Value>=requiredexp.Value then
			exp.Value = exp.Value-requiredexp.Value 
			Level.Value+=1
			requiredexp.Value =Level.Value*1000
		end
		
		end)
	
	




	local Data = DataStore:GetAsync(Player.UserId) 

	if type(Data) ~= "table" then
		Data = nil
	end

	if Data then
		exp.Value = Data.exp
		Level.Value = Data.Level
		Cash.Value = Data.Cash
		end
		


end)




game.Players.PlayerRemoving:Connect(function(Player) 

	

	DataStore:SetAsync(Player.UserId, { 
		Level = Player.Level.Value,
		Expp = Player.Expp.Value,
		Cash = Player.Cash.Value
	})

	end)
`

Solved: I did not set the right data directory.

game.Players.PlayerRemoving:Connect(function(Player) -- save function here

	--[{ DATA STORE SAVING }]--

	DataStore:SetAsync(Player.UserId, { -- gets data
		Level = Player.leaderstats.Level.Value,
		exp = Player.Expp.Exp.Value,
		Cash = Player.leaderstats.Leaf.Value
	})

	end)
1 Like

Try this code, You didn’t add the right path to you leaderstats when you are saving.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("TimeStats")
local service = game:GetService("MarketplaceService")

game.Players.PlayerAdded:Connect(function(Player)




	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player


	local Expp = Instance.new("Folder")
	Expp.Name = "Expp"
	Expp.Parent = Player

	local Level = Instance.new("NumberValue")
	Level.Name="Level"
	Level.Value= 1
	Level.Parent = Leaderstats

	local Cash = Instance.new("IntValue")
	Cash.Name = "Leaf"
	Cash.Value = 0
	Cash.Parent = Leaderstats



	local exp = Instance.new("NumberValue",Expp)
	exp.Name="Exp"
	exp.Value= 0

	local requiredexp = Instance.new("NumberValue",Player)
	requiredexp.Name ="RequiredExp"
	requiredexp.Value=Level.Value*1000





	exp.Changed:Connect(function(Changed)
		if exp.Value>=requiredexp.Value then
			exp.Value = exp.Value-requiredexp.Value 
			Level.Value+=1
			requiredexp.Value =Level.Value*1000
		end

	end)






	local Data = DataStore:GetAsync(Player.UserId) 

	if type(Data) ~= "table" then
		Data = nil
	end

	if Data then
		exp.Value = Data.exp
		Level.Value = Data.Level
		Cash.Value = Data.Cash
	end



end)




game.Players.PlayerRemoving:Connect(function(Player) 



	DataStore:SetAsync(Player.UserId, { 
		Level = Player.leaderstats.Level.Value,
		Expp = Player.Expp.Exp.Value,
		Cash = Player.leaderstats.Leaf.Value
	})

end)
1 Like