Problem with Datastore

Hi guys I have written a small leaderboard saving here which should normally store the data and output it again. Problem is that it does not do this and does not give an error message. It would be a dream if you could help me with this problem.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("Datalore")

Players.PlayerAdded:Connect(function(player)
	local Data = nil
	local success, errormessage = pcall(function()
		Data = Saver:GetAsync(tostring(player.UserId))
	end)

	if success then
		if Data then
			for i, v in pairs(Data) do
				player:WaitForChild("leaderstats"):WaitForChild(i).Value = v
			end
		end
	else
		error(errormessage)
	end
end)

local function Save(player)
	local SavedData = {}
	for _, v in pairs(player.leaderstats:GetChildren()) do
		SavedData[v.Name] = v.Value
	end

	local success, errormessage = pcall(function()
		Saver:SetAsync(tostring(player.UserId), SavedData)
	end)
	if not success then
		error(errormessage)
	end
end

Players.PlayerRemoving:Connect(Save)

game:BindToClose(function()
	for _, v in pairs(Players:GetPlayers()) do
		Save(v)
	end
end)

Unless you are doing so in another script, you aren’t actually creating the leaderstats for the player. Roblox will not save the leaderstats folder when the player leaves, so you need to manually create the instances each time they join.

Mostly fine, but there are a few things:

  1. DataStore Access: Make sure you have DataStore access enabled in your game’s settings.
  2. Leaderstats Initialization: Make sure your leaderstats are initialized for each player before trying to access them.

if not working then Try this code:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("Datalore")

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

	-- Initialize leaderstats with default values if necessary
	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Value = 0
	points.Parent = leaderstats

	local Data = nil
	local success, errormessage = pcall(function()
		Data = Saver:GetAsync(tostring(player.UserId))
	end)

	if success then
		if Data then
			for i, v in pairs(Data) do
				player.leaderstats:WaitForChild(i).Value = v
			end
		end
	else
		warn("Error loading data for player " .. player.Name .. ": " .. errormessage)
	end
end)

local function Save(player)
	local SavedData = {}
	for _, v in pairs(player.leaderstats:GetChildren()) do
		if v:IsA("IntValue") or v:IsA("NumberValue") then
			SavedData[v.Name] = v.Value
		end
	end

	local success, errormessage = pcall(function()
		Saver:SetAsync(tostring(player.UserId), SavedData)
	end)
	if not success then
		warn("Error saving data for player " .. player.Name .. ": " .. errormessage)
	end
end

Players.PlayerRemoving:Connect(Save)

game:BindToClose(function()
	for _, v in pairs(Players:GetPlayers()) do
		Save(v)
	end
end)
1 Like

Unfortunately, this code does not work for me either. :frowning:
But at the end, when I close the game, there is an error code → DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key =

Whatever the issue might be, its outdated to use SetAsync consider visiting this page Data Stores | Documentation - Roblox Creator Hub and update your code

Try removing tostring, maybe it can lead to weird behaviour but I doubt it

Now that I have changed the SetAsync to an UpdateAsync, this error message appears.

Error saving data for player OpaBohne: Unable to cast value to function (x2)

Some devs have mentioned that datastores only work in game, not in Studio. I’d look into this first if I were you.

Also, it does not matter if you call tostring() prior to saving - datastores automatically cast all types to strings before they store data anyways.

Do you mind providing the updated code that you are using now?

This error probably happens because you put in the value as the second parameter

:UpdateAsync() takes a function that returns the new value as the second parameter

:SetAsync(id, "test")

-- is the same as

:UpdateAsync(id, function(oldValue) return "test" end)