Datastore Saving Problem

so, i make a leaderboard and every player that is about to leave its save all the values inside of the leaderboard (or its supposed to be)

however, when the players leaves, it save all the values in a table. and when its the time to save in datastore, it does not save

i tried to build a pcall function, it didn,t work

local LeaderstatsValues = {["Floors"] = 0, ["Coins"] = 0}

local Datastore = game.DataStoreService

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

	local PlrDatastore = Datastore:GetDataStore("Leaderstats"):GetAsync(p.UserId)

	for Name, Value in pairs(LeaderstatsValues) do
		local Value2 = Instance.new("IntValue")

		if PlrDatastore ~= nil then
			if table.find(PlrDatastore, Name) then
				Value2.Value = PlrDatastore[Name]
			else
				Value2.Value = Value
			end
		else
			Value2.Value = Value
		end
		
		Value2.Name = Name
		Value2.Parent = Leaderstats
	end
end)

game.Players.PlayerRemoving:Connect(function(p)
	local LeaderstatsSave = {}
	local Leaderstats = p:FindFirstChild("leaderstats")

	
	local PlrDatastore = Datastore:GetDataStore("Leaderstats")
	if Leaderstats then
		for i, Inst in pairs(Leaderstats:GetChildren()) do
			LeaderstatsSave[Inst.Name] = Inst.Value
		end
print(LeaderstatsSave)
			PlrDatastore:SetAsync(p.UserId, LeaderstatsSave)
			print(PlrDatastore:GetAsync(p.UserId))
	end
end)
1 Like

before I even READ the script, I’ll tell you this: Datastore DOES NOT RUN in Roblox studio. You can really only test datastore from the game itself

You can use datastores in studio. I have done it many times before.

  1. i typically say this on every single datastore post but add this to the end of your script and see if it works
game:BindToClose(function()
task.wait(3)                          --This gives the game time to save your data
end)
  1. Add pcalls around your GetAsync and SetAsync, even if that isn’t the cause of your issue. If someones data fails to load/save, it will break the script for everyone else.

  2. Make sure that you are saving an array. Datastores can’t save tables(i think).

  3. Try this in the table part of the script

		for i, Inst in ipairs(Leaderstats:GetChildren()) do
			table.insert(Leaderstatssave, Inst.Value)
		end

I’ve had this problem before as well. Sometimes studio glitches out and, when leaving, it doesn’t save information to the DataStore. My fix was that, every time I wanted to stop testing in studio, I used the escape menu and the leave button. But there might be a different issue.

You might as well want to use pcall() when getting data from or setting data to DataStores.

no, it continues in the same error aswell, i tried to print the tables in datastore after saving it, it didn,t saved

also datastores can yes, save tables. a previous game that i worked it uses tables to save to datastore. and it worked well

the problem of the script its not the table itself, and the saving part

adding a wait script didn,t worked

and thanks for the pcall advice

1 Like

thanks for advice too, i have put the pcall() function

1 Like