Values not saving

so Im trying to create a simple way to make leaderstats and values in the player and saving them both but the values arent saving can someone help me?

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local leaderstatsTable = {
		["Steps"] = 0,
		["Money"] = 0,
	}
	
	local valuesTable = {
		["LuckUpgrades"] = 5,
		["MaxLuckUpgrades"] = 10,
	}
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local valuesFolder = Instance.new("Folder")
	valuesFolder.Name = "Values"
	valuesFolder.Parent = player

	local data
	local success, err = pcall(function()
		data = myDataStore:GetAsync(player.UserId)
	end)
	
	for name, value in pairs(leaderstatsTable) do
		local newValue = Instance.new("IntValue")
		newValue.Name = name
		newValue.Value = value
		newValue.Parent = leaderstats

		if success and data and data[name] then
			newValue.Value = data[name]
		end
	end

	for name, value in pairs(valuesTable) do
		local newValue = Instance.new("IntValue")
		newValue.Name = name
		newValue.Value = value
		newValue.Parent = valuesFolder

		if success and data and data[name] then
			newValue.Value = data[name]
		end
	end
end)

function leaderstatsToTable(leaderstatsTable)
	local tbl = {}

	for _, intValue in pairs(leaderstatsTable:GetChildren()) do
		tbl[intValue.Name] = intValue.Value
	end

	return tbl
end

function valuesToTable(valuesFolder)
	local tbl = {}

	for _, intValue in pairs(valuesFolder:GetChildren()) do
		tbl[intValue.Name] = intValue.Value
	end

	return tbl
end

players.PlayerRemoving:Connect(function(player)
	local leaderstatsFolder = player:FindFirstChild("leaderstats")
	local valuesFolder = player:FindFirstChild("Values")

	if valuesFolder and leaderstatsFolder then
		local dataToSave = valuesToTable(valuesFolder) and leaderstatsToTable(leaderstatsFolder)

		local success, err
		local attempt = 1

		repeat
			success, err = pcall(function()
				myDataStore:SetAsync(player.UserId, dataToSave)
			end)

			attempt += 1
			task.wait(2)
		until success or attempt > 4

		if success then
			print(player.Name .. "'s data was success fully saved")
		else
			print(player.Name .. "'s data failed to save")
		end
	end
end)

game:BindToClose(function()
	task.wait(2)
end)
1 Like

I usually make the delay 5 seconds in the BindToClose() function.
That’s all I know.

1 Like

Hey! I suggest trying to go to game settings, then security and Enable Studio Access to API Services! I used your code and it worked for me.

1 Like

I have that but only the leaderstats save is that not with yours

1 Like

I thought you meant all of it wasn’t saving, I’ll have another look.

1 Like

Alright, I’ve figured out the issue. You’re not actually combining the to data tables for values and leaderstats. What this line of code does:

local dataToSave = valuesToFolder() and leaderstatsToFolder() 

does is only return only one datatype if the other one exists. What you need to do is merge the two data tables by making another function:

local function dataToTable(data1,data2) -- data1 and data2 are the valuesToTable and leaderstatsToTable
	local data = {}
	
	for i,v in pairs(data1) do
		data[i] = v
	end
	
	for i,v in pairs(data2) do
		data[i] = v
	end
	
	return data
end

This worked like a charm for me. Tell me if you don’t understand something and I’ll try to clear it up.

1 Like

can you please put it in the script because I’m new to scripting and idk where to put or what to remove?

I have no idea how to put it in the script can you please tell me how this doesnt work

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local leaderstatsTable = {
		["Steps"] = 0,
		["Money"] = 0,
	}
	
	local valuesTable = {
		["LuckUpgrades"] = 5,
		["MaxLuckUpgrades"] = 10,
	}
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local valuesFolder = Instance.new("Folder")
	valuesFolder.Name = "Values"
	valuesFolder.Parent = player

	local data
	local success, err = pcall(function()
		data = myDataStore:GetAsync(player.UserId)
	end)
	
	for name, value in pairs(leaderstatsTable) do
		local newValue = Instance.new("IntValue")
		newValue.Name = name
		newValue.Value = value
		newValue.Parent = leaderstats

		if success and data and data[name] then
			newValue.Value = data[name]
		end
	end

	for name, value in pairs(valuesTable) do
		local newValue = Instance.new("IntValue")
		newValue.Name = name
		newValue.Value = value
		newValue.Parent = valuesFolder

		if success and data and data[name] then
			newValue.Value = data[name]
		end
	end
end)

function leaderstatsToTable(leaderstatsTable)
	local tbl = {}

	for _, intValue in pairs(leaderstatsTable:GetChildren()) do
		tbl[intValue.Name] = intValue.Value
	end

	return tbl
end

function valuesToTable(valuesFolder)
	local tbl = {}

	for _, intValue in pairs(valuesFolder:GetChildren()) do
		tbl[intValue.Name] = intValue.Value
	end

	return tbl
end

players.PlayerRemoving:Connect(function(player)
	local leaderstatsFolder = player:FindFirstChild("leaderstats")
	local valuesFolder = player:FindFirstChild("Values")

	if valuesFolder and leaderstatsFolder then
		local function dataToSave(data1,data2) -- data1 and data2 are the valuesToTable and leaderstatsToTable
			local data = {}

			for i,v in pairs(data1) do
				data[i] = v
			end

			for i,v in pairs(data2) do
				data[i] = v
			end

			return data
		end

		local success, err
		local attempt = 1

		repeat
			success, err = pcall(function()
				myDataStore:SetAsync(player.UserId, dataToSave)
			end)

			attempt += 1
			task.wait(2)
		until success or attempt > 4

		if success then
			print(player.Name .. "'s data was success fully saved")
		else
			print(player.Name .. "'s data failed to save")
		end
	end
end)

game:BindToClose(function()
	task.wait(5)
end)

Don’t put the dataToSave function inside of the PlayerRemoving, its a separate one just like the other two. You’d turn dataToSave into:

dataToSave = dataToSave(leaderstatsToTable(leaderstatsFolder),valuesToTable(valuesFolder))

try not to copy it and try to figure it out yourself too. But that’s the gist of it. Come back to me if you need more help and I’ll provide the place file with what I used.

ty so much it worked but it takse a really long time before I get back in studio after I hit stop and wich one do I put a solved?

doesn’t matter which one, any would do, but I recommend the one where I showed you first how to fix the issue with the dataToTable function. Thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.