DataStore not saving when leaving

Hey I’m trying to save the data stores once the player leaves, however it doesn’t work, it doesn’t even print anything.

Here’s the code:

game.Players.PlayerRemoving:Connect(function(plr)
	local ID = plr.UserId
	local PlayerStats = plr.PlayerStats
	local PlayerProjects = plr.PlayerProjects
	local ToSave = {}
	for i, v in pairs(PlayerStats:GetChildren()) do
		ToSave[v.Name] = v.Value
	end
	ToSave["Projects"] = {}
	if #PlayerProjects:GetChildren() > 0 then
		for _, p in pairs(PlayerProjects:GetChildren()) do
			ToSave["Projects"][p.Name] = {}
			for i, v in pairs(p:GetChildren()) do
				if v.ClassName ~= "Folder" then
					ToSave["Projects"][p.Name][v.Name] = v.Value
				else
					ToSave["Projects"][p.Name][v.Name] = {}
					if #v:GetChildren() > 0 then
						for index, feature in pairs(v:GetChildren()) do
							ToSave["Projects"][p.Name][v.Name][feature.Name] = feature.Value
						end
					end
				end
			end
		end
	end
	local success, err = pcall(function()
		DSService:SetAsync(ID, ToSave)
	end)
	if success then
		print("success")
	else
		warn(err)
	end
end)

Are you running this in studio?

Both studio and game not working.

That is quite lot of work to do when player leaves. Consider reading my tutorial.

2 Likes

I read it a few hours ago (awesome tutorial by the way),
added this earlier just didn’t include in the post yet still doesn’t work:

game:BindToClose(function()
	if game:GetService("RunService"):IsStudio() then return end
	local Players = game:GetService("Players"):GetPlayers()
	for _, plr in pairs(Players) do
		local ID = plr.UserId
		local PlayerStats = plr.PlayerStats
		local PlayerProjects = plr.PlayerProjects
		local ToSave = {}
		for i, v in pairs(PlayerStats:GetChildren()) do
			ToSave[v.Name] = v.Value
		end
		ToSave["Projects"] = {}
		if #PlayerProjects:GetChildren() > 0 then
			for _, p in pairs(PlayerProjects:GetChildren()) do
				ToSave["Projects"][p.Name] = {}
				for i, v in pairs(p:GetChildren()) do
					if v.ClassName ~= "Folder" then
						ToSave["Projects"][p.Name][v.Name] = v.Value
					else
						ToSave["Projects"][p.Name][v.Name] = {}
						if #v:GetChildren() > 0 then
							for index, feature in pairs(v:GetChildren()) do
								ToSave["Projects"][p.Name][v.Name][feature.Name] = feature.Value
							end
						end
					end
				end
			end
		end
		DSService:SetAsync(ID, ToSave)
	end
end)

I also have this problem 95% of the time don’t know how to fix it

Simple Saving Code Like this

game.Players.PlayerRemoving:Connect(function(plr)
local data = PlayerData[plr]
local key = plr.UserId; print(“start”)

local success, err = pcall(function()
DS:SetAsync(key,data)
end)

if success then
print(“Saved”)
else
warn(“Err Didn’t Save”)
end
end)

Doesn’t save most of the time nor does it warn me

What seemed to be the problem for me is switching the key and the data parameters in the DS:SaveAsync function, maybe check that.

@IX_AQUA I would use Learn Datastore2 have not had any saving problem yet

And my own tutorial:

1 Like

I did start using it. Thank you for the recommendation anyway :).