Data not saving

Hello my data is not saving. And Im not getting errors.

local datastoreservice = game:GetService("DataStoreService")
local data1 = datastoreservice:GetDataStore("data2")
local data2 = datastoreservice:GetDataStore("data3")




game.Players.PlayerAdded:Connect(function(Player)
	
	--  Crush Points
	-- Top Crush Points
	local NonUsingItemsFolder = Instance.new("Folder")
	NonUsingItemsFolder.Parent = Player
	NonUsingItemsFolder.Name = "NonUsingItemsFolder"
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = Player
	leaderstats.Name = "leaderstats"
	

	local EnabledGive = Instance.new("BoolValue")
	EnabledGive.Parent = Player
	EnabledGive.Value = false
	EnabledGive.Name = "EnabledGive"
	
	local Crush_Points = Instance.new("IntValue",leaderstats)
	Crush_Points.Name = "Crush Points"
	Crush_Points.Value = data1:GetAsync(Player.UserId,Crush_Points) or 0

	
	local Top_Points = Instance.new("IntValue",leaderstats)
	Top_Points.Name = "Top Crush Points"
	Top_Points.Value = data2:GetAsync(Player.UserId,Top_Points) or 0 
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	Player.CharacterAdded:Connect(function(chr)
	local Billboard = game.ReplicatedStorage.Billboard.BillboardGui:Clone()
	
	Billboard.Parent = chr.Head
		
		while true do
			wait(0.1)
			Billboard.TextLabel.Text = Player.leaderstats["Crush Points"].Value
			
			
			
		end
	
	end)
end)







while true do
	wait(1)
	for i,v in pairs(game.Players:GetChildren()) do
		
	if v.EnabledGive.Value == true then
		v.leaderstats["Crush Points"].Value += 1
		
		if v.leaderstats["Top Crush Points"].Value <= v.leaderstats["Crush Points"].Value then
			
				v.leaderstats["Top Crush Points"].Value = v.leaderstats["Crush Points"].Value
				end
		end

	end
	
end





game.Players.PlayerRemoving:Connect(function(player)
	
	
	data1:SetAsync(player.UserId,player.leaderstats["Crush Points"].Value)
	data2:SetAsync(player.UserId,player.leaderstats["Top Crush Points"].Value)
	
end)

Please help.

Do a debug.

local success, err = pcall(function()
data1:SetAsync(player.UserId,player.leaderstats["Crush Points"].Value)
	data2:SetAsync(player.UserId,player.leaderstats["Top Crush Points"].Value)
end)
if success then
print("Successfully saved "..Player.Name.."'s Data!")
else
warn(err)
end
1 Like

It doesn’t even print something when player leaves…

try doing:

    data1:SetAsync(player.UserId.."-Points",player.leaderstats["Crush Points"].Value)
    	data2:SetAsync(player.UserId.."-TopPoints",player.leaderstats["Top Crush Points"].Value)

You would also have to change the “GetAsync”

1 Like

There are more proper ways to do a datastore script, try watching this video, hopefully it solves your problem. Additionally it’s a good idea to add a game:BindToClose and kick the player to make sure the player’s data will save even if the server is shutting down.

And if you want to save multiple values using one datastore, you will want to simply use a table.

1 Like

It doesn’t cause the PlayerRemoving event isn’t even being fired. That’s because the while loop is yielding everything after it.
Wrap the while loop in a coroutine instead, or put the PlayerRemoving event before the while loop.

1 Like