My DataStore Wont Save My Leaderstats

my DataStore wont save my leaderstats for some reason

local datastore = game:GetService("DataStoreService"):GetDataStore("Data")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"

	local Gold = Instance.new("NumberValue")
	Gold.Parent = leaderstats
	Gold.Name = "Gold"

	local Rebirths = Instance.new("NumberValue")
	Rebirths.Parent = leaderstats
	Rebirths.Name = "Rebirths"
	
	local MegaCoins = Instance.new("NumberValue")
	MegaCoins.Parent = leaderstats
	MegaCoins.Name = "MegaCoins"
	MegaCoins.Value = 0

	local debounce = Instance.new("BoolValue")
	debounce.Value = false
	debounce.Name = "Debounce"
	debounce.Parent = player

	local key = "user-" .. player.userId

	local storeditems = datastore:GetAsync(key)

	if storeditems then
		Gold.Value = storeditems[1]
		Rebirths.Value = storeditems[2]
		MegaCoins.Value = storeditems[3]
	else
		local items = {Gold.Value, Rebirths.Value,MegaCoins.Value}
		datastore:SetAsync(key, items)
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local items = {player.leaderstats.Gold.Value, player.leaderstats.Rebirths.Value,player.leaderstats.MegaCoins.Value}
	local key = "user-" .. player.userId
	local success, response = pcall(function() datastore:SetAsync(key, items) end)

	if not success then
		warn("Could not save the data for: " .. player.Name)
	end
end)
2 Likes

…does it output anything?

might want to look into using bindtoclose for when the only player in the server leaves.
https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose

I see a lot of people having the same issues with this issue, including myself sometimes.

local datastore = game:GetService("DataStoreService"):GetDataStore("Data")

game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder", plr)
leaderstats.Name = "leaderstats"

local Gold = Instance.new("NumberValue",leaderstats)
	Gold.Name = "Gold"

	local Rebirths = Instance.new("NumberValue",leaderstats)
	Rebirths.Name = "Rebirths"
	
	local MegaCoins = Instance.new("NumberValue",leaderstats)
	MegaCoins.Name = "MegaCoins"
	MegaCoins.Value = 0

	local debounce = Instance.new("BoolValue")
	debounce.Value = false
	debounce.Name = "Debounce"
	debounce.Parent = player
	
	local key = plr.UserId -- to keep it simple
	
	local storeditems = datastore:GetAsync(key)
	
	if storeditems ~= nil then -- anything, BUT nil.
	print("Found data for " .. plr.Name)
		Gold.Value = storeditems[1]
		Rebirths.Value = storeditems[2]
		MegaCoins.Value = storeditems[3]
		else
		print("Could not find data for " .. plr.Name .. " replacing no data with data")
		Gold.Value = 0
		Rebirths.Value = 0
		MegaCoins.Value = 0
	end
end)

game.Players.PlayerRemoving:connect(function(plr)

	local datasave = {}
	table.insert(datasave, plr:WaitForChild("leaderstats":WaitForChild("Gold").Value)
	table.insert(datasave, plr:WaitForChild("leaderstats":WaitForChild("Rebirths").Value)
	table.insert(datasave, plr:WaitForChild("leaderstats":WaitForChild("MegaCoins").Value)
	local success, resp = pcall(function()
	datastore:SetAsync(plr.UserId, datasave) -- key & then the data
	end)
	
	if success then
	print("Succesfully saved data of " .. plr.Name)
	else
	warn(resp) -- basically if there is an error it will say what it is.
	end
end)

It said it was saved but it did not actually save

Have you enabled studio access to API services? Also, where is the script placed?

Yes, Replicated Storage And The Leaderstats Folder is in the player

Try putting the script in ServerScriptService.

it still did not work for me :frowning:

Are you changing the values in a server script?

What? My Data Store is in a ServerScript

Okay I have had this problem before have you only tried to save on studio? if so maybe you can add a line ---->

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