DataStore issue

DataStore doesnt work, i tried a diffrent method but i ran into some overwriting issues.

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


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
				local leaderstat = player:WaitForChild("leaderstats"):WaitForChild(i)
				if leaderstat then
					if leaderstat.Value == "None" or leaderstat.Value == 0 then
						leaderstat.Value = v
					end
				end
			end
		else
			print("No saved data found for player.")
		end
	else
		warn("Error loading data for player:", errormessage)
	end
end)

-- Save all leaderstats data
local function Save(player)
	local SavedData = {}
	for _, v in pairs(player.leaderstats:GetChildren()) do
		-- Only save leaderstats with meaningful data, not empty ones like "None"
		if v.Value ~= "None" and v.Value ~= 0 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("Failed to save player data:", errormessage)
	else
		print("Player data successfully saved.")
	end
end


Players.PlayerRemoving:Connect(Save)
game:BindToClose(function()
	for _, player in ipairs(Players:GetPlayers()) do
		Save(player)
	end
end)

What is it exactly you are saving?

What are you specifically doing in your script?
Have you tried debugging? If so, where is your script failing?
There isn’t a lot of information provided in this post; I would help you if I had a clear vision on what you are trying.

1 Like
  • Please provide console logs.
  • Its impossible to know what issue you have without you providing it.
  • Please avoid using ipairs/pairs and put value dirrectly instead.

So for making it unclear, im trying to save my leaderboard stats, the script runs without any errors, however it doesnt actually save any of the data. the data store below worked fine however there was issues when i tried to change the actual data values. The datastore script I pasted into my original script doesn’t work.

image

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("SaveLeaderstats")
 
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)

A value object cant be both a number and a string.

Make sure you have a global flag for the player (something like “hasDataLoaded”) to make sure you’re not ending up saving no data. (Cases where players leave right before their data loads in)

Verify that your SavedData table is not missing entries with a few checks.

In the case where you’d rather not waste time making a datastore solution by yourself check out ProfileStore - Save your player data easy (DataStore Module)